Skip to content

Commit 40a6d9f

Browse files
committed
Provide required xml generation for CLI initialization
1 parent 570ff8b commit 40a6d9f

File tree

13 files changed

+289
-38
lines changed

13 files changed

+289
-38
lines changed

resources/fileTemplates/code/Magento CLI Command DI Xml.xml.ft

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#if (${CLI_COMMAND_INTERFACE})
2+
<type name="${CLI_COMMAND_INTERFACE}">
3+
<arguments>
4+
<argument name="commands" xsi:type="array">
5+
#end
6+
<item name="${CLI_COMMAND_DI_NAME}" xsi:type="object">${CLI_COMMAND_CLASS}</item>
7+
#if (${CLI_COMMAND_INTERFACE})
8+
</argument>
9+
</arguments>
10+
</type>
11+
#end

resources/magento2/common.properties

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ common.notAvailable=N/A
2727
common.classInheritance=Inherit Class
2828
common.license.proprietary=Proprietary
2929
common.description=Description
30-
common.cli.class.name=Class Name
31-
common.cli.parent.directory=Parent Directory
32-
common.cli.cli.description=CLI Command Description
33-
common.cli.cli.name=CLI Command Name
30+
common.parentDirectory=Parent Directory
31+
common.cliCommandName=CLI Command Name
3432
common.cli.create.new.cli.command.title=Create a new Magento 2 CLI Command
3533
common.cli.generate.error=New CLI Command Generation Error
3634
common.cli.class.title=CLI Command Class

src/com/magento/idea/magento2plugin/actions/generation/NewCronjobAction.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,3 @@ public boolean isDumbAware() {
4747
return false;
4848
}
4949
}
50-
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.actions.generation.data;
6+
7+
import com.jetbrains.php.lang.psi.elements.PhpClass;
8+
9+
public class CLICommandXmlData {
10+
private final String cliCommandModule;
11+
private final String cliCommandClass;
12+
private final String cliCommandDiName;
13+
14+
public CLICommandXmlData(
15+
String cliCommandModule,
16+
String cliCommandClass,
17+
String cliCommandDiName
18+
) {
19+
this.cliCommandModule = cliCommandModule;
20+
this.cliCommandClass = cliCommandClass;
21+
this.cliCommandDiName = cliCommandDiName;
22+
}
23+
24+
public String getCLICommandModule() {
25+
return cliCommandModule;
26+
}
27+
public String getCliCommandClass() { return cliCommandClass; }
28+
public String getCliCommandDiName() {
29+
return cliCommandDiName;
30+
}
31+
}

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewCLICommandDialog.form

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
<grid id="27dc6" binding="contentPane" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="10" left="10" bottom="10" right="10"/>
55
<constraints>
6-
<xy x="20" y="20" width="678" height="289"/>
6+
<xy x="20" y="20" width="904" height="497"/>
77
</constraints>
8-
<properties/>
8+
<properties>
9+
<minimumSize width="597" height="497"/>
10+
<opaque value="false"/>
11+
</properties>
912
<border type="none"/>
1013
<children>
1114
<grid id="bce24" layout-manager="GridLayoutManager" row-count="6" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
@@ -55,7 +58,7 @@
5558
</constraints>
5659
<properties>
5760
<labelFor value="61353"/>
58-
<text resource-bundle="magento2/common" key="common.cli.class.name"/>
61+
<text resource-bundle="magento2/common" key="common.className"/>
5962
</properties>
6063
</component>
6164
<component id="f6c65" class="javax.swing.JLabel">
@@ -64,7 +67,7 @@
6467
</constraints>
6568
<properties>
6669
<labelFor value="82342"/>
67-
<text resource-bundle="magento2/common" key="common.cli.parent.directory"/>
70+
<text resource-bundle="magento2/common" key="common.parentDirectory"/>
6871
</properties>
6972
</component>
7073
<component id="8696b" class="javax.swing.JLabel">
@@ -73,7 +76,7 @@
7376
</constraints>
7477
<properties>
7578
<labelFor value="a06d9"/>
76-
<text resource-bundle="magento2/common" key="common.cli.cli.name"/>
79+
<text resource-bundle="magento2/common" key="common.cliCommandName"/>
7780
</properties>
7881
</component>
7982
<component id="abeb0" class="javax.swing.JLabel">
@@ -82,7 +85,7 @@
8285
</constraints>
8386
<properties>
8487
<labelFor value="d786e"/>
85-
<text resource-bundle="magento2/common" key="common.cli.cli.description"/>
88+
<text resource-bundle="magento2/common" key="common.description"/>
8689
</properties>
8790
</component>
8891
<grid id="5f3c" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
@@ -127,7 +130,9 @@
127130
<preferred-size width="150" height="50"/>
128131
</grid>
129132
</constraints>
130-
<properties/>
133+
<properties>
134+
<wrapStyleWord value="false"/>
135+
</properties>
131136
</component>
132137
</children>
133138
</grid>

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewCLICommandDialog.java

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@
66

77
import com.intellij.openapi.project.Project;
88
import com.intellij.psi.PsiDirectory;
9+
import com.jetbrains.php.lang.psi.elements.PhpClass;
910
import com.magento.idea.magento2plugin.actions.generation.NewCLICommandAction;
1011
import com.magento.idea.magento2plugin.actions.generation.data.CLICommandClassData;
12+
import com.magento.idea.magento2plugin.actions.generation.data.CLICommandXmlData;
1113
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.NewCLICommandValidator;
1214
import com.magento.idea.magento2plugin.actions.generation.generator.CLICommandClassGenerator;
15+
import com.magento.idea.magento2plugin.actions.generation.generator.CLICommandDiXmlGenerator;
1316
import com.magento.idea.magento2plugin.actions.generation.generator.util.NamespaceBuilder;
17+
import com.magento.idea.magento2plugin.magento.packages.File;
18+
import com.magento.idea.magento2plugin.magento.packages.Package;
19+
import com.magento.idea.magento2plugin.util.CamelCaseToSnakeCase;
20+
import com.magento.idea.magento2plugin.util.GetPhpClassByFQN;
1421
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectory;
1522

1623
import javax.swing.*;
@@ -28,11 +35,13 @@ public class NewCLICommandDialog extends AbstractDialog {
2835
private final Project project;
2936
private final String moduleName;
3037
private final NewCLICommandValidator validator;
38+
private final CamelCaseToSnakeCase camelCaseToSnakeCase;
3139

3240
public NewCLICommandDialog(Project project, PsiDirectory directory) {
3341
this.project = project;
3442
this.moduleName = GetModuleNameByDirectory.getInstance(project).execute(directory);
3543
this.validator = NewCLICommandValidator.getInstance();
44+
this.camelCaseToSnakeCase = CamelCaseToSnakeCase.getInstance();
3645

3746
setContentPane(contentPane);
3847
setModal(true);
@@ -80,10 +89,25 @@ public String getCLICommandDescription() {
8089
return this.cliCommandDescriptionField.getText().trim();
8190
}
8291

83-
private String getCLICommandModule() {
92+
public String getDIXmlItemName() {
93+
String cliCommandClassNameToSnakeCase = this.camelCaseToSnakeCase.convert(this.getCLICommandClassName());
94+
95+
return this.moduleName.toLowerCase() + "_" + cliCommandClassNameToSnakeCase;
96+
}
97+
98+
public String getCLICommandModule() {
8499
return this.moduleName;
85100
}
86101

102+
public String getCLICommandClassFqn() {
103+
NamespaceBuilder namespaceBuilder = new NamespaceBuilder(
104+
moduleName,
105+
this.getCLICommandClassName(),
106+
this.getCLICommandParentDirectory()
107+
);
108+
return namespaceBuilder.getClassFqn();
109+
}
110+
87111
private void onOK() {
88112
if (!validator.validate(this.project,this)) {
89113
return;
@@ -94,9 +118,8 @@ private void onOK() {
94118

95119
private void generate() {
96120
try {
97-
CLICommandClassData cliCommandClassData = this.initializeCLICommandClassData();
98-
CLICommandClassGenerator cliCommandClassGenerator = new CLICommandClassGenerator(project, cliCommandClassData);
99-
cliCommandClassGenerator.generate(NewCLICommandAction.ACTION_NAME, true);
121+
this.generateCLICommandClass();
122+
this.generateCLICommandDiXmlRegistration();
100123
} catch (Exception exception) {
101124
JOptionPane.showMessageDialog(
102125
null,
@@ -107,6 +130,26 @@ private void generate() {
107130
}
108131
}
109132

133+
private void generateCLICommandClass() {
134+
CLICommandClassData cliCommandClassData = this.initializeCLICommandClassData();
135+
CLICommandClassGenerator cliCommandClassGenerator = new CLICommandClassGenerator(project, cliCommandClassData);
136+
cliCommandClassGenerator.generate(NewCLICommandAction.ACTION_NAME, true);
137+
}
138+
139+
private void generateCLICommandDiXmlRegistration() {
140+
CLICommandXmlData cliCommandXmlData = this.initializeCLICommandDIXmlData();
141+
CLICommandDiXmlGenerator cliCommandDiXmlGenerator = new CLICommandDiXmlGenerator(project, cliCommandXmlData);
142+
cliCommandDiXmlGenerator.generate(NewCLICommandAction.ACTION_NAME);
143+
}
144+
145+
private CLICommandXmlData initializeCLICommandDIXmlData() {
146+
return new CLICommandXmlData(
147+
this.getCLICommandModule(),
148+
this.getCLICommandClassFqn(),
149+
this.getDIXmlItemName()
150+
);
151+
}
152+
110153
private CLICommandClassData initializeCLICommandClassData() {
111154
return new CLICommandClassData(
112155
this.getCLICommandClassName(),

src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/NewCLICommandValidator.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
package com.magento.idea.magento2plugin.actions.generation.dialog.validator;
66

77
import com.intellij.openapi.project.Project;
8+
import com.jetbrains.php.lang.psi.elements.PhpClass;
89
import com.magento.idea.magento2plugin.actions.generation.dialog.NewCLICommandDialog;
10+
import com.magento.idea.magento2plugin.actions.generation.generator.util.NamespaceBuilder;
911
import com.magento.idea.magento2plugin.bundles.ValidatorBundle;
1012
import com.magento.idea.magento2plugin.bundles.CommonBundle;
13+
import com.magento.idea.magento2plugin.util.GetPhpClassByFQN;
1114
import com.magento.idea.magento2plugin.util.RegExUtil;
1215

1316
import javax.swing.*;
@@ -36,11 +39,12 @@ public boolean validate(Project project, NewCLICommandDialog dialog) {
3639
String cliCommandParentDirectory = dialog.getCLICommandParentDirectory();
3740
String cliCommandName = dialog.getCLICommandName();
3841
String cliCommandDescription = dialog.getCLICommandDescription();
42+
String moduleName = dialog.getCLICommandModule();
3943

4044
if (cliCommandClassName.length() == 0) {
4145
String errorMessage = validatorBundle.message(
4246
"validator.notEmpty",
43-
this.commonBundle.message("common.cli.class.name")
47+
this.commonBundle.message("common.className")
4448
);
4549
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
4650

@@ -49,7 +53,7 @@ public boolean validate(Project project, NewCLICommandDialog dialog) {
4953
if (!cliCommandClassName.matches(RegExUtil.ALPHANUMERIC)) {
5054
String errorMessage = validatorBundle.message(
5155
"validator.alphaNumericCharacters",
52-
this.commonBundle.message("common.cli.class.name")
56+
this.commonBundle.message("common.className")
5357
);
5458
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
5559

@@ -58,7 +62,7 @@ public boolean validate(Project project, NewCLICommandDialog dialog) {
5862
if (!Character.isUpperCase(cliCommandClassName.charAt(0)) && !Character.isDigit(cliCommandClassName.charAt(0))) {
5963
String errorMessage = validatorBundle.message(
6064
"validator.startWithNumberOrCapitalLetter",
61-
this.commonBundle.message("common.cli.class.name")
65+
this.commonBundle.message("common.className")
6266
);
6367
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
6468

@@ -68,7 +72,7 @@ public boolean validate(Project project, NewCLICommandDialog dialog) {
6872
if (cliCommandParentDirectory.length() == 0) {
6973
String errorMessage = validatorBundle.message(
7074
"validator.notEmpty",
71-
this.commonBundle.message("common.cli.parent.directory")
75+
this.commonBundle.message("common.parentDirectory")
7276
);
7377
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
7478

@@ -77,7 +81,7 @@ public boolean validate(Project project, NewCLICommandDialog dialog) {
7781
if (!cliCommandParentDirectory.matches(RegExUtil.DIRECTORY)) {
7882
String errorMessage = validatorBundle.message(
7983
"validator.directory.isNotValid",
80-
this.commonBundle.message("common.cli.parent.directory")
84+
this.commonBundle.message("common.parentDirectory")
8185
);
8286
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
8387

@@ -87,16 +91,16 @@ public boolean validate(Project project, NewCLICommandDialog dialog) {
8791
if (cliCommandName.length() == 0) {
8892
String errorMessage = validatorBundle.message(
8993
"validator.notEmpty",
90-
this.commonBundle.message("common.cli.cli.name")
94+
this.commonBundle.message("common.cliCommandName")
9195
);
9296
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
9397

9498
return false;
9599
}
96100
if (!cliCommandName.matches(RegExUtil.CLI_COMMAND_NAME)) {
97101
String errorMessage = validatorBundle.message(
98-
"validator.identifier",
99-
this.commonBundle.message("common.cli.cli.name")
102+
"validator.directory.isNotValid",
103+
this.commonBundle.message("common.cliCommandName")
100104
);
101105
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
102106

@@ -106,7 +110,23 @@ public boolean validate(Project project, NewCLICommandDialog dialog) {
106110
if (cliCommandDescription.length() == 0) {
107111
String errorMessage = validatorBundle.message(
108112
"validator.notEmpty",
109-
this.commonBundle.message("common.cli.cli.description")
113+
this.commonBundle.message("common.description")
114+
);
115+
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
116+
117+
return false;
118+
}
119+
120+
NamespaceBuilder namespaceBuilder = new NamespaceBuilder(
121+
moduleName,
122+
cliCommandClassName,
123+
cliCommandParentDirectory
124+
);
125+
PhpClass cliCommandPHPClass = GetPhpClassByFQN.getInstance(project).execute(namespaceBuilder.getClassFqn());
126+
if (cliCommandPHPClass != null) {
127+
String errorMessage = validatorBundle.message(
128+
"validator.file.alreadyExists",
129+
"CLI Command Class"
110130
);
111131
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
112132

src/com/magento/idea/magento2plugin/actions/generation/generator/CLICommandClassGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public PsiFile generate(String actionName) {
4343

4444
if (cliCommandFile == null) {
4545
String errorMessage = validatorBundle.message(
46-
"validator.file.cantBeCreated",
47-
commonBundle.message("common.cli.class.title")
46+
"validator.file.cantBeCreated",
47+
commonBundle.message("common.cli.class.title")
4848
);
4949

5050
throw new RuntimeException(errorMessage);

0 commit comments

Comments
 (0)