Skip to content

Commit 59a1d35

Browse files
Merge pull request #59 from magebitcom/release
Release
2 parents 9d25690 + 1306cc8 commit 59a1d35

File tree

6 files changed

+65
-19
lines changed

6 files changed

+65
-19
lines changed

src/command/GenerateXmlCatalogCommand.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default class GenerateXmlCatalogCommand extends Command {
2929
const catalogLocation = Uri.joinPath(workspaceFolder.uri, '.vscode/magento-catalog.xml');
3030

3131
if (!(await FileSystem.fileExists(catalogLocation))) {
32-
const success = await this.generateCatalog(workspaceFolder);
32+
const success = await this.generateCatalog();
3333

3434
if (!success) {
3535
return;
@@ -39,7 +39,9 @@ export default class GenerateXmlCatalogCommand extends Command {
3939
await this.formatAndWriteCatalog(catalogLocation, workspaceFolder.uri);
4040
await this.updateXmlConfig(workspaceFolder, catalogLocation);
4141

42-
window.showInformationMessage('XML URN catalog generated and configured successfully');
42+
window.showInformationMessage(
43+
'XML URN catalog generated and configured successfully. You might need to reload the editor for changes to take effect.'
44+
);
4345
}
4446

4547
private async formatAndWriteCatalog(catalogLocation: Uri, workspaceUri: Uri) {
@@ -83,24 +85,24 @@ export default class GenerateXmlCatalogCommand extends Command {
8385
await FileSystem.writeFile(catalogLocation, formattedCatalog);
8486
}
8587

86-
private async generateCatalog(workspaceFolder: WorkspaceFolder): Promise<boolean> {
87-
const catalogLocation = Uri.joinPath(workspaceFolder.uri, '.vscode/magento-catalog.xml');
88-
88+
private async generateCatalog(): Promise<boolean> {
8989
const magentoCli = new MagentoCli();
9090

9191
try {
92-
await magentoCli.run('dev:urn-catalog:generate', [catalogLocation.fsPath]);
92+
await magentoCli.run('dev:urn-catalog:generate', ['.vscode/magento-catalog.xml']);
9393
} catch (error) {
9494
console.error(error);
9595

9696
window.showErrorMessage(
9797
'Failed to generate URN catalog. Try running this command manually: \n\n' +
98-
`bin/magento dev:urn-catalog:generate ${catalogLocation.fsPath}`
98+
`bin/magento dev:urn-catalog:generate .vscode/magento-catalog.xml`
9999
);
100100

101101
return false;
102102
}
103103

104+
magentoCli.dispose();
105+
104106
return true;
105107
}
106108

src/common/ExtensionState.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
import { ExtensionContext } from 'vscode';
1+
import { ExtensionContext, WorkspaceFolder } from 'vscode';
22

33
export default class ExtensionState {
44
private static _context: ExtensionContext;
5+
private static _magentoWorkspaces: WorkspaceFolder[] = [];
56

6-
public static init(context: ExtensionContext) {
7+
public static init(context: ExtensionContext, magentoWorkspaces: WorkspaceFolder[]) {
78
this._context = context;
9+
this._magentoWorkspaces = magentoWorkspaces;
810
}
911

1012
public static get context() {
1113
return this._context;
1214
}
15+
16+
public static get magentoWorkspaces() {
17+
return this._magentoWorkspaces;
18+
}
1319
}

src/common/MagentoCli.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ export default class MagentoCli {
3636
});
3737
}
3838

39+
public dispose() {
40+
const terminal = vscode.window.terminals.find(t => t.name === MagentoCli.TERMINAL_NAME);
41+
42+
if (terminal) {
43+
terminal.dispose();
44+
}
45+
}
46+
3947
private getOrCreateTerminal(): vscode.Terminal {
4048
const terminal = vscode.window.terminals.find(t => t.name === MagentoCli.TERMINAL_NAME);
4149

src/extension.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import GenerateRoutesXmlFileCommand from 'command/GenerateRoutesXmlFileCommand';
2424
import GenerateAclXmlFileCommand from 'command/GenerateAclXmlFileCommand';
2525
import GenerateDiXmlFileCommand from 'command/GenerateDiXmlFileCommand';
2626
import GeneratePreferenceCommand from 'command/GeneratePreferenceCommand';
27+
import Magento from 'util/Magento';
28+
import { WorkspaceFolder } from 'vscode';
2729

2830
// This method is called when your extension is activated
2931
// Your extension is activated the very first time the command is executed
@@ -46,15 +48,32 @@ export async function activate(context: vscode.ExtensionContext) {
4648
GeneratePreferenceCommand,
4749
];
4850

49-
ExtensionState.init(context);
51+
const magentoWorkspaces: WorkspaceFolder[] = [];
52+
53+
if (vscode.workspace.workspaceFolders) {
54+
for (const folder of vscode.workspace.workspaceFolders) {
55+
if (await Magento.isMagentoWorkspace(folder)) {
56+
magentoWorkspaces.push(folder);
57+
}
58+
}
59+
}
60+
61+
ExtensionState.init(context, magentoWorkspaces);
5062

5163
commands.forEach(command => {
5264
const instance = new command();
5365

5466
Common.log('Registering command', instance.getCommand());
5567

56-
const disposable = vscode.commands.registerCommand(instance.getCommand(), (...args) => {
57-
instance.execute(...args);
68+
const disposable = vscode.commands.registerCommand(instance.getCommand(), async (...args) => {
69+
try {
70+
await instance.execute(...args);
71+
} catch (error) {
72+
console.error(error);
73+
vscode.window.showErrorMessage(
74+
'An error occurred while executing the command: ' + instance.getCommand()
75+
);
76+
}
5877
});
5978

6079
context.subscriptions.push(disposable);

src/util/Common.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ExtensionState from 'common/ExtensionState';
12
import { workspace, WorkspaceFolder, window } from 'vscode';
23

34
export default class Common {
@@ -32,14 +33,14 @@ export default class Common {
3233
throw new Error('Workspace is empty');
3334
}
3435

35-
if (workspace.workspaceFolders.length === 1) {
36-
return workspace.workspaceFolders[0];
36+
if (window.activeTextEditor?.document.uri) {
37+
return workspace.getWorkspaceFolder(window.activeTextEditor.document.uri);
3738
}
3839

39-
if (!window.activeTextEditor?.document.uri) {
40-
throw new Error('No active text editor');
40+
if (ExtensionState.magentoWorkspaces.length > 0) {
41+
return ExtensionState.magentoWorkspaces[0];
4142
}
4243

43-
return workspace.getWorkspaceFolder(window.activeTextEditor.document.uri);
44+
return undefined;
4445
}
4546
}

src/util/Magento.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import PhpNamespace from 'common/PhpNamespace';
22
import lowerFirst from 'lodash-es/lowerFirst';
33
import { MagentoScope } from 'types';
4-
import { Uri } from 'vscode';
5-
4+
import { Uri, WorkspaceFolder } from 'vscode';
5+
import FileSystem from './FileSystem';
66
export default class Magento {
77
public static isPluginMethod(method: string) {
88
return /^around|^before|^after/.test(method);
@@ -40,4 +40,14 @@ export default class Magento {
4040
public static getModuleNamespace(vendor: string, module: string): PhpNamespace {
4141
return PhpNamespace.fromParts([vendor, module]);
4242
}
43+
44+
public static async isMagentoWorkspace(workspaceFolder: WorkspaceFolder): Promise<boolean> {
45+
const diXmlPath = Uri.joinPath(workspaceFolder.uri, 'app/etc/di.xml');
46+
// Check if the signature Magento file exists in the workspace
47+
try {
48+
return await FileSystem.fileExists(diXmlPath);
49+
} catch (error) {
50+
return false;
51+
}
52+
}
4353
}

0 commit comments

Comments
 (0)