Skip to content

Commit ac10090

Browse files
committed
Refactoring and add test for the CLI class generation
1 parent 07bcd41 commit ac10090

File tree

10 files changed

+185
-45
lines changed

10 files changed

+185
-45
lines changed

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,43 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.actions.generation;
67

78
import com.intellij.ide.IdeView;
8-
import com.intellij.openapi.actionSystem.*;
9+
import com.intellij.openapi.actionSystem.AnAction;
10+
import com.intellij.openapi.actionSystem.AnActionEvent;
11+
import com.intellij.openapi.actionSystem.CommonDataKeys;
12+
import com.intellij.openapi.actionSystem.DataContext;
13+
import com.intellij.openapi.actionSystem.LangDataKeys;
914
import com.intellij.openapi.project.Project;
1015
import com.intellij.psi.PsiDirectory;
1116
import com.magento.idea.magento2plugin.MagentoIcons;
1217
import com.magento.idea.magento2plugin.actions.generation.dialog.NewCLICommandDialog;
1318
import org.jetbrains.annotations.NotNull;
1419

1520
public class NewCLICommandAction extends AnAction {
16-
public static String ACTION_NAME = "Magento 2 CLI Command";
17-
public static String ACTION_DESCRIPTION = "Create a new Magento 2 CLI Command";
21+
public static String ACTION_NAME = "Magento 2 CLI Command";//NOPMD
22+
public static String ACTION_DESCRIPTION = "Create a new Magento 2 CLI Command";//NOPMD
1823

19-
NewCLICommandAction() {
24+
public NewCLICommandAction() {
2025
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
2126
}
2227

2328
@Override
24-
public void actionPerformed(@NotNull AnActionEvent e) {
25-
DataContext dataContext = e.getDataContext();
26-
IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
27-
29+
public void actionPerformed(@NotNull final AnActionEvent event) {
30+
final DataContext context = event.getDataContext();
31+
final IdeView view = LangDataKeys.IDE_VIEW.getData(context);
2832
if (view == null) {
29-
return;
33+
return;//NOPMD
3034
}
3135

32-
Project project = CommonDataKeys.PROJECT.getData(dataContext);
36+
final Project project = CommonDataKeys.PROJECT.getData(context);
3337
if (project == null) {
34-
return;
38+
return;//NOPMD
3539
}
3640

37-
PsiDirectory directory = view.getOrChooseDirectory();
41+
final PsiDirectory directory = view.getOrChooseDirectory();
3842
if (directory == null) {
3943
return;
4044
}

src/com/magento/idea/magento2plugin/actions/generation/data/CLICommandClassData.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,39 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.actions.generation.data;
67

7-
public class CLICommandClassData {
8+
public class CLICommandClassData { //NOPMD
89
private final String className;
910
private final String parentDirectory;
1011
private final String commandName;
11-
private final String commandDescription;
12+
private final String description;
1213
private final String namespace;
1314
private final String moduleName;
1415

16+
/**
17+
* CLI Command PHP class data class.
18+
*
19+
* @param cliClassName PHP class name
20+
* @param parentDirectory Prent directory of a PHP class
21+
* @param cliCommandName CLI Command name
22+
* @param description CLI Command description
23+
* @param namespace namespace of a PHP class
24+
* @param moduleName CLI Command module name
25+
*/
1526
public CLICommandClassData(
16-
String cliClassName,
17-
String cliParentDirectory,
18-
String cliCommandName,
19-
String cliCommandDescription,
20-
String namespace,
21-
String moduleName
27+
final String cliClassName,
28+
final String parentDirectory,
29+
final String cliCommandName,
30+
final String description,
31+
final String namespace,
32+
final String moduleName
2233
) {
2334
this.className = cliClassName;
24-
this.parentDirectory = cliParentDirectory;
35+
this.parentDirectory = parentDirectory;
2536
this.commandName = cliCommandName;
26-
this.commandDescription = cliCommandDescription;
37+
this.description = description;
2738
this.namespace = namespace;
2839
this.moduleName = moduleName;
2940
}
@@ -41,7 +52,7 @@ public String getCommandName() {
4152
}
4253

4354
public String getCommandDescription() {
44-
return commandDescription;
55+
return description;
4556
}
4657

4758
public String getNamespace() {

src/com/magento/idea/magento2plugin/actions/generation/data/CLICommandXmlData.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5-
package com.magento.idea.magento2plugin.actions.generation.data;
65

7-
import com.jetbrains.php.lang.psi.elements.PhpClass;
6+
package com.magento.idea.magento2plugin.actions.generation.data;
87

98
public class CLICommandXmlData {
109
private final String cliCommandModule;
1110
private final String cliCommandClass;
1211
private final String cliCommandDiName;
1312

13+
/**
14+
* CLI Command xml data class.
15+
*
16+
* @param cliCommandModule CLI Command module
17+
* @param cliCommandClass CLI Command PHP class
18+
* @param cliCommandDiName CLI Command di.xml item name
19+
*/
1420
public CLICommandXmlData(
15-
String cliCommandModule,
16-
String cliCommandClass,
17-
String cliCommandDiName
21+
final String cliCommandModule,
22+
final String cliCommandClass,
23+
final String cliCommandDiName
1824
) {
1925
this.cliCommandModule = cliCommandModule;
2026
this.cliCommandClass = cliCommandClass;
@@ -24,7 +30,11 @@ public CLICommandXmlData(
2430
public String getCLICommandModule() {
2531
return cliCommandModule;
2632
}
27-
public String getCliCommandClass() { return cliCommandClass; }
33+
34+
public String getCliCommandClass() {
35+
return cliCommandClass;
36+
}
37+
2838
public String getCliCommandDiName() {
2939
return cliCommandDiName;
3040
}

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

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.actions.generation.dialog;
67

78
import com.intellij.openapi.project.Project;
@@ -15,8 +16,17 @@
1516
import com.magento.idea.magento2plugin.actions.generation.generator.util.NamespaceBuilder;
1617
import com.magento.idea.magento2plugin.util.CamelCaseToSnakeCase;
1718
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectory;
18-
import javax.swing.*;
19-
import java.awt.event.*;
19+
import java.awt.event.KeyEvent;
20+
import java.awt.event.WindowAdapter;
21+
import java.awt.event.WindowEvent;
22+
import java.util.Locale;
23+
import javax.swing.JButton;
24+
import javax.swing.JComponent;
25+
import javax.swing.JOptionPane;
26+
import javax.swing.JPanel;
27+
import javax.swing.JTextArea;
28+
import javax.swing.JTextField;
29+
import javax.swing.KeyStroke;
2030

2131
public class NewCLICommandDialog extends AbstractDialog {
2232
private JPanel contentPane;
@@ -32,6 +42,12 @@ public class NewCLICommandDialog extends AbstractDialog {
3242
private final NewCLICommandValidator validator;
3343
private final CamelCaseToSnakeCase toSnakeCase;
3444

45+
/**
46+
* Open new dialog for adding a new CLI Command.
47+
*
48+
* @param project Project
49+
* @param directory PsiDirectory
50+
*/
3551
public NewCLICommandDialog(final Project project, final PsiDirectory directory) {
3652
super();
3753
this.project = project;
@@ -55,16 +71,15 @@ public void windowClosing(final WindowEvent event) {
5571
}
5672
});
5773

58-
contentPane.registerKeyboardAction(new ActionListener() {
59-
@Override
60-
public void actionPerformed(final ActionEvent event) {
61-
onCancel();
62-
}
63-
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
74+
contentPane.registerKeyboardAction(
75+
event -> onCancel(),
76+
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
77+
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
78+
);
6479
}
6580

6681
/**
67-
* Open a new CLI command dialog
82+
* Open a new CLI command dialog.
6883
*
6984
* @param project Project
7085
* @param directory Directory
@@ -93,16 +108,32 @@ public String getCLICommandDescription() {
93108
return this.cliCommandDescriptionField.getText().trim();
94109
}
95110

111+
/**
112+
* Get di.xml item nme for CLI command based on the module name and class name.
113+
* @return di.xml item name
114+
*/
96115
public String getDIXmlItemName() {
97116
final String diItemName = this.toSnakeCase.convert(this.getCLICommandClassName());
98117

99-
return this.moduleName.toLowerCase() + "_" + diItemName;
118+
return this.moduleName.toLowerCase(new Locale("en","EN"))
119+
+ "_"
120+
+ diItemName;
100121
}
101122

123+
/**
124+
* Get current module name for which a CLI Command is being added.
125+
*
126+
* @return module name
127+
*/
102128
public String getCLICommandModule() {
103129
return this.moduleName;
104130
}
105131

132+
/**
133+
* Get a CLI Command PHP class namespace.
134+
*
135+
* @return PHP class namespace
136+
*/
106137
public String getCLICommandClassFqn() {
107138
final NamespaceBuilder namespaceBuilder = new NamespaceBuilder(
108139
moduleName,
@@ -124,7 +155,7 @@ private void generate() {
124155
try {
125156
this.generateCLICommandClass();
126157
this.generateCLICommandDiXmlRegistration();
127-
} catch (Exception exception) {
158+
} catch (Exception exception) { //NOPMD
128159
JOptionPane.showMessageDialog(
129160
null,
130161
exception.getMessage(),
@@ -136,13 +167,15 @@ private void generate() {
136167

137168
private void generateCLICommandClass() {
138169
final CLICommandClassData dataClass = this.initializeCLICommandClassData();
139-
final CLICommandClassGenerator classGenerator = new CLICommandClassGenerator(project, dataClass);
170+
final CLICommandClassGenerator classGenerator =
171+
new CLICommandClassGenerator(project, dataClass);
140172
classGenerator.generate(NewCLICommandAction.ACTION_NAME, true);
141173
}
142174

143175
private void generateCLICommandDiXmlRegistration() {
144176
final CLICommandXmlData cliCommandXmlData = this.initializeCLICommandDIXmlData();
145-
final CLICommandDiXmlGenerator diGenerator = new CLICommandDiXmlGenerator(project, cliCommandXmlData);
177+
final CLICommandDiXmlGenerator diGenerator =
178+
new CLICommandDiXmlGenerator(project, cliCommandXmlData);
146179
diGenerator.generate(NewCLICommandAction.ACTION_NAME);
147180
}
148181

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ public class NewCLICommandValidator {
2626
* @return NewCLICommandValidator
2727
*/
2828
public static NewCLICommandValidator getInstance() {
29-
if (null != INSTANCE) return INSTANCE;//NOPMD
29+
if (null != INSTANCE) { //NOPMD
30+
return INSTANCE;
31+
}
3032
INSTANCE = new NewCLICommandValidator();
3133

3234
return INSTANCE;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class CLICommandClassGenerator extends FileGenerator {
3030
/**
3131
* Generates new CLI Command PHP Class based on provided data.
3232
*
33-
* @param project Project
33+
* @param project Project
3434
* @param phpClassData CLICommandClassData
3535
*/
3636
public CLICommandClassGenerator(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public PsiFile generate(final String actionName) {
9696
}
9797

9898
@Override
99-
final protected void fillAttributes(final Properties attributes) {
99+
protected final void fillAttributes(final Properties attributes) {
100100
if (!this.isDeclared) {
101101
attributes.setProperty("CLI_COMMAND_INTERFACE", ModuleDiXml.CLI_COMMAND_INTERFACE);
102102
}

src/com/magento/idea/magento2plugin/magento/files/CLICommandTemplate.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.magento.files;
67

78
import com.intellij.lang.Language;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Foo\Bar\Console;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
9+
class TestCLICommandPHPClass extends Command
10+
{
11+
/**
12+
* @inheritDoc
13+
*/
14+
protected function configure()
15+
{
16+
$this->setName('bar:test-command');
17+
$this->setDescription('This is the test command');
18+
parent::configure();
19+
}
20+
21+
/**
22+
* CLI command description
23+
*
24+
* @param InputInterface $input
25+
* @param OutputInterface $output
26+
*
27+
* @return void
28+
*/
29+
protected function execute(InputInterface $input, OutputInterface $output): void
30+
{
31+
// todo: implement CLI command logic here
32+
}
33+
}

0 commit comments

Comments
 (0)