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
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;
}
}
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;
}
}
}