Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/command/GenerateXmlCatalogCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -83,24 +85,24 @@ export default class GenerateXmlCatalogCommand extends Command {
await FileSystem.writeFile(catalogLocation, formattedCatalog);
}

private async generateCatalog(workspaceFolder: WorkspaceFolder): Promise<boolean> {
const catalogLocation = Uri.joinPath(workspaceFolder.uri, '.vscode/magento-catalog.xml');

private async generateCatalog(): Promise<boolean> {
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;
}

Expand Down
10 changes: 8 additions & 2 deletions src/common/ExtensionState.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
8 changes: 8 additions & 0 deletions src/common/MagentoCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
25 changes: 22 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions src/util/Common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ExtensionState from 'common/ExtensionState';
import { workspace, WorkspaceFolder, window } from 'vscode';

export default class Common {
Expand Down Expand Up @@ -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;
}
}
14 changes: 12 additions & 2 deletions src/util/Magento.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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<boolean> {
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;
}
}
}