|
| 1 | +import { Command } from 'command/Command'; |
| 2 | +import WizzardClosedError from 'webview/error/WizzardClosedError'; |
| 3 | +import FileGeneratorManager from 'generator/FileGeneratorManager'; |
| 4 | +import Common from 'util/Common'; |
| 5 | +import { TextDocument, Uri, window } from 'vscode'; |
| 6 | +import IndexManager from 'indexer/IndexManager'; |
| 7 | +import ModuleIndexer from 'indexer/module/ModuleIndexer'; |
| 8 | +import PreferenceWizard, { PreferenceWizardData } from 'wizard/PreferenceWizard'; |
| 9 | +import PreferenceClassGenerator from 'generator/preference/PreferenceClassGenerator'; |
| 10 | +import PhpDocumentParser from 'common/php/PhpDocumentParser'; |
| 11 | +import { ClasslikeInfo } from 'common/php/ClasslikeInfo'; |
| 12 | +import PreferenceDiGenerator from 'generator/preference/PreferenceDiGenerator'; |
| 13 | + |
| 14 | +export default class GeneratePreferenceCommand extends Command { |
| 15 | + constructor() { |
| 16 | + super('magento-toolbox.generatePreference'); |
| 17 | + } |
| 18 | + |
| 19 | + public async execute(uri?: Uri): Promise<void> { |
| 20 | + const moduleIndex = IndexManager.getIndexData(ModuleIndexer.KEY); |
| 21 | + let contextModule: string | undefined; |
| 22 | + |
| 23 | + const contextUri = uri || window.activeTextEditor?.document.uri; |
| 24 | + |
| 25 | + if (moduleIndex && contextUri) { |
| 26 | + const module = moduleIndex.getModuleByUri(contextUri); |
| 27 | + |
| 28 | + if (module) { |
| 29 | + contextModule = module.name; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + const parentClassName = await this.getParentClassName(window.activeTextEditor?.document); |
| 34 | + |
| 35 | + const preferenceWizard = new PreferenceWizard(); |
| 36 | + |
| 37 | + let data: PreferenceWizardData; |
| 38 | + |
| 39 | + try { |
| 40 | + data = await preferenceWizard.show(parentClassName, contextModule); |
| 41 | + } catch (error) { |
| 42 | + if (error instanceof WizzardClosedError) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + throw error; |
| 47 | + } |
| 48 | + |
| 49 | + const manager = new FileGeneratorManager([ |
| 50 | + new PreferenceClassGenerator(data), |
| 51 | + new PreferenceDiGenerator(data), |
| 52 | + ]); |
| 53 | + |
| 54 | + const workspaceFolder = Common.getActiveWorkspaceFolder(); |
| 55 | + |
| 56 | + if (!workspaceFolder) { |
| 57 | + window.showErrorMessage('No active workspace folder'); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + await manager.generate(workspaceFolder.uri); |
| 62 | + await manager.writeFiles(); |
| 63 | + await manager.refreshIndex(workspaceFolder); |
| 64 | + manager.openAllFiles(); |
| 65 | + } |
| 66 | + |
| 67 | + private async getParentClassName( |
| 68 | + document: TextDocument | undefined |
| 69 | + ): Promise<string | undefined> { |
| 70 | + if (!document) { |
| 71 | + return undefined; |
| 72 | + } |
| 73 | + |
| 74 | + const phpFile = await PhpDocumentParser.parse(document); |
| 75 | + const info = new ClasslikeInfo(phpFile); |
| 76 | + |
| 77 | + return info.getNamespace(); |
| 78 | + } |
| 79 | +} |
0 commit comments