Skip to content

Commit 42aec5a

Browse files
Merge pull request #53 from magebitcom/release
v1.1.0
2 parents 612a368 + 29493ba commit 42aec5a

26 files changed

+381
-79
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Main
1+
name: Create Release
22

33
on:
44
push:

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Magento 2 code generation, inspection and utility tools",
55
"publisher": "magebit",
66
"icon": "assets/logo.png",
7-
"version": "1.0.1",
7+
"version": "1.1.0",
88
"engines": {
99
"vscode": "^1.93.1"
1010
},
@@ -106,6 +106,10 @@
106106
{
107107
"command": "magento-toolbox.generateWebapiXmlFile",
108108
"title": "Magento Toolbox: Generate Webapi XML"
109+
},
110+
{
111+
"command": "magento-toolbox.generatePreference",
112+
"title": "Magento Toolbox: Generate Preference"
109113
}
110114
],
111115
"menus": {
@@ -135,6 +139,10 @@
135139
{
136140
"command": "magento-toolbox.generateContextPlugin",
137141
"when": "magento-toolbox.canGeneratePlugin"
142+
},
143+
{
144+
"command": "magento-toolbox.generatePreference",
145+
"when": "magento-toolbox.canGeneratePreference"
138146
}
139147
],
140148
"magento-toolbox.explorer-submenu": [

src/command/GenerateContextPluginCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export default class GenerateContextPluginCommand extends Command {
7676
await manager.generate(workspaceFolder.uri);
7777
await manager.writeFiles();
7878
await manager.refreshIndex(workspaceFolder);
79-
manager.openFirstFile();
79+
manager.openAllFiles();
8080
}
8181

8282
private resolvePluginMethod(pluginSubjectInfo: PluginSubjectInfo): PhpMethod | undefined {

src/command/GenerateObserverCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ export default class GenerateObserverCommand extends Command {
5959
await manager.generate(workspaceFolder.uri);
6060
await manager.writeFiles();
6161
await manager.refreshIndex(workspaceFolder);
62-
manager.openFirstFile();
62+
manager.openAllFiles();
6363
}
6464
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}

src/common/Context.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import CopyMagentoPathCommand from 'command/CopyMagentoPathCommand';
44

55
export interface EditorContext {
66
canGeneratePlugin: boolean;
7+
canGeneratePreference: boolean;
78
supportedMagentoPathExtensions: string[];
89
}
910

@@ -25,6 +26,7 @@ class Context {
2526
await this.setContext({
2627
...this.editorContext,
2728
canGeneratePlugin: await this.canGeneratePlugin(editor),
29+
canGeneratePreference: await this.canGeneratePreference(editor),
2830
});
2931
}
3032

@@ -43,6 +45,7 @@ class Context {
4345
public getDefaultContext(): EditorContext {
4446
return {
4547
canGeneratePlugin: false,
48+
canGeneratePreference: false,
4649
supportedMagentoPathExtensions: [
4750
...CopyMagentoPathCommand.TEMPLATE_EXTENSIONS,
4851
...CopyMagentoPathCommand.WEB_EXTENSIONS,
@@ -83,6 +86,15 @@ class Context {
8386

8487
return false;
8588
}
89+
90+
private async canGeneratePreference(editor: TextEditor): Promise<boolean> {
91+
if (editor.document.languageId !== 'php') {
92+
return false;
93+
}
94+
95+
const phpFile = await PhpDocumentParser.parse(editor.document);
96+
return phpFile.classes.length > 0 || phpFile.interfaces.length > 0;
97+
}
8698
}
8799

88100
export default new Context();

src/common/PhpNamespace.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ export default class PhpNamespace {
2525
return this.parts.join(PhpNamespace.NS_SEPARATOR);
2626
}
2727

28-
public append(part: string | PhpNamespace): PhpNamespace {
29-
if (typeof part === 'string') {
30-
return new PhpNamespace([...this.parts, part]);
31-
}
32-
33-
return new PhpNamespace([...this.parts, ...part.getParts()]);
28+
public append(...parts: (string | PhpNamespace)[]): PhpNamespace {
29+
return new PhpNamespace([
30+
...this.parts,
31+
...parts.flatMap(part => {
32+
if (typeof part === 'string') {
33+
return [part];
34+
}
35+
36+
return part.getParts();
37+
}),
38+
]);
3439
}
3540

3641
public prepend(part: string | PhpNamespace): PhpNamespace {

src/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import GenerateGraphqlSchemaFileCommand from 'command/GenerateGraphqlSchemaFile'
2323
import GenerateRoutesXmlFileCommand from 'command/GenerateRoutesXmlFileCommand';
2424
import GenerateAclXmlFileCommand from 'command/GenerateAclXmlFileCommand';
2525
import GenerateDiXmlFileCommand from 'command/GenerateDiXmlFileCommand';
26+
import GeneratePreferenceCommand from 'command/GeneratePreferenceCommand';
2627

2728
// This method is called when your extension is activated
2829
// Your extension is activated the very first time the command is executed
@@ -42,6 +43,7 @@ export async function activate(context: vscode.ExtensionContext) {
4243
GenerateRoutesXmlFileCommand,
4344
GenerateAclXmlFileCommand,
4445
GenerateDiXmlFileCommand,
46+
GeneratePreferenceCommand,
4547
];
4648

4749
ExtensionState.init(context);

src/generator/ModuleFileGenerator.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)