diff --git a/src/command/GenerateXmlCatalogCommand.ts b/src/command/GenerateXmlCatalogCommand.ts index db7ebb4..0ff72ed 100644 --- a/src/command/GenerateXmlCatalogCommand.ts +++ b/src/command/GenerateXmlCatalogCommand.ts @@ -29,7 +29,7 @@ export default class GenerateXmlCatalogCommand extends Command { const catalogLocation = Uri.joinPath(workspaceFolder.uri, '.vscode/magento-catalog.xml'); if (!(await FileSystem.fileExists(catalogLocation))) { - const success = await this.generateCatalog(workspaceFolder); + const success = await this.generateCatalog(); if (!success) { return; @@ -39,7 +39,9 @@ export default class GenerateXmlCatalogCommand extends Command { await this.formatAndWriteCatalog(catalogLocation, workspaceFolder.uri); await this.updateXmlConfig(workspaceFolder, catalogLocation); - window.showInformationMessage('XML URN catalog generated and configured successfully'); + window.showInformationMessage( + 'XML URN catalog generated and configured successfully. You might need to reload the editor for changes to take effect.' + ); } private async formatAndWriteCatalog(catalogLocation: Uri, workspaceUri: Uri) { @@ -83,24 +85,24 @@ export default class GenerateXmlCatalogCommand extends Command { await FileSystem.writeFile(catalogLocation, formattedCatalog); } - private async generateCatalog(workspaceFolder: WorkspaceFolder): Promise { - const catalogLocation = Uri.joinPath(workspaceFolder.uri, '.vscode/magento-catalog.xml'); - + private async generateCatalog(): Promise { const magentoCli = new MagentoCli(); try { - await magentoCli.run('dev:urn-catalog:generate', [catalogLocation.fsPath]); + await magentoCli.run('dev:urn-catalog:generate', ['.vscode/magento-catalog.xml']); } catch (error) { console.error(error); window.showErrorMessage( 'Failed to generate URN catalog. Try running this command manually: \n\n' + - `bin/magento dev:urn-catalog:generate ${catalogLocation.fsPath}` + `bin/magento dev:urn-catalog:generate .vscode/magento-catalog.xml` ); return false; } + magentoCli.dispose(); + return true; } diff --git a/src/common/ExtensionState.ts b/src/common/ExtensionState.ts index 185e1e3..df57fb1 100644 --- a/src/common/ExtensionState.ts +++ b/src/common/ExtensionState.ts @@ -1,13 +1,19 @@ -import { ExtensionContext } from 'vscode'; +import { ExtensionContext, WorkspaceFolder } from 'vscode'; export default class ExtensionState { private static _context: ExtensionContext; + private static _magentoWorkspaces: WorkspaceFolder[] = []; - public static init(context: ExtensionContext) { + public static init(context: ExtensionContext, magentoWorkspaces: WorkspaceFolder[]) { this._context = context; + this._magentoWorkspaces = magentoWorkspaces; } public static get context() { return this._context; } + + public static get magentoWorkspaces() { + return this._magentoWorkspaces; + } } diff --git a/src/common/MagentoCli.ts b/src/common/MagentoCli.ts index 583e051..3507b27 100644 --- a/src/common/MagentoCli.ts +++ b/src/common/MagentoCli.ts @@ -36,6 +36,14 @@ export default class MagentoCli { }); } + public dispose() { + const terminal = vscode.window.terminals.find(t => t.name === MagentoCli.TERMINAL_NAME); + + if (terminal) { + terminal.dispose(); + } + } + private getOrCreateTerminal(): vscode.Terminal { const terminal = vscode.window.terminals.find(t => t.name === MagentoCli.TERMINAL_NAME); diff --git a/src/extension.ts b/src/extension.ts index 71fe3ea..37e5e7d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -24,6 +24,8 @@ import GenerateRoutesXmlFileCommand from 'command/GenerateRoutesXmlFileCommand'; import GenerateAclXmlFileCommand from 'command/GenerateAclXmlFileCommand'; import GenerateDiXmlFileCommand from 'command/GenerateDiXmlFileCommand'; import GeneratePreferenceCommand from 'command/GeneratePreferenceCommand'; +import Magento from 'util/Magento'; +import { WorkspaceFolder } from 'vscode'; // This method is called when your extension is activated // Your extension is activated the very first time the command is executed @@ -46,15 +48,32 @@ export async function activate(context: vscode.ExtensionContext) { GeneratePreferenceCommand, ]; - ExtensionState.init(context); + const magentoWorkspaces: WorkspaceFolder[] = []; + + if (vscode.workspace.workspaceFolders) { + for (const folder of vscode.workspace.workspaceFolders) { + if (await Magento.isMagentoWorkspace(folder)) { + magentoWorkspaces.push(folder); + } + } + } + + ExtensionState.init(context, magentoWorkspaces); commands.forEach(command => { const instance = new command(); Common.log('Registering command', instance.getCommand()); - const disposable = vscode.commands.registerCommand(instance.getCommand(), (...args) => { - instance.execute(...args); + const disposable = vscode.commands.registerCommand(instance.getCommand(), async (...args) => { + try { + await instance.execute(...args); + } catch (error) { + console.error(error); + vscode.window.showErrorMessage( + 'An error occurred while executing the command: ' + instance.getCommand() + ); + } }); context.subscriptions.push(disposable); diff --git a/src/util/Common.ts b/src/util/Common.ts index b711fea..657b95b 100644 --- a/src/util/Common.ts +++ b/src/util/Common.ts @@ -1,3 +1,4 @@ +import ExtensionState from 'common/ExtensionState'; import { workspace, WorkspaceFolder, window } from 'vscode'; export default class Common { @@ -32,14 +33,14 @@ export default class Common { throw new Error('Workspace is empty'); } - if (workspace.workspaceFolders.length === 1) { - return workspace.workspaceFolders[0]; + if (window.activeTextEditor?.document.uri) { + return workspace.getWorkspaceFolder(window.activeTextEditor.document.uri); } - if (!window.activeTextEditor?.document.uri) { - throw new Error('No active text editor'); + if (ExtensionState.magentoWorkspaces.length > 0) { + return ExtensionState.magentoWorkspaces[0]; } - return workspace.getWorkspaceFolder(window.activeTextEditor.document.uri); + return undefined; } } diff --git a/src/util/Magento.ts b/src/util/Magento.ts index 790944c..7fc5036 100644 --- a/src/util/Magento.ts +++ b/src/util/Magento.ts @@ -1,8 +1,8 @@ import PhpNamespace from 'common/PhpNamespace'; import lowerFirst from 'lodash-es/lowerFirst'; import { MagentoScope } from 'types'; -import { Uri } from 'vscode'; - +import { Uri, WorkspaceFolder } from 'vscode'; +import FileSystem from './FileSystem'; export default class Magento { public static isPluginMethod(method: string) { return /^around|^before|^after/.test(method); @@ -40,4 +40,14 @@ export default class Magento { public static getModuleNamespace(vendor: string, module: string): PhpNamespace { return PhpNamespace.fromParts([vendor, module]); } + + public static async isMagentoWorkspace(workspaceFolder: WorkspaceFolder): Promise { + const diXmlPath = Uri.joinPath(workspaceFolder.uri, 'app/etc/di.xml'); + // Check if the signature Magento file exists in the workspace + try { + return await FileSystem.fileExists(diXmlPath); + } catch (error) { + return false; + } + } }