Skip to content

Commit db51cd8

Browse files
author
Vitaliy
authored
Merge pull request #101 from melnikovi/specify-module-dependencies
Allow to specify module dependencies when creating a new module
2 parents 1ce0ccb + 89188be commit db51cd8

File tree

7 files changed

+157
-13
lines changed

7 files changed

+157
-13
lines changed

resources/fileTemplates/internal/Magento Module Xml.xml.ft

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
33
#if (${SEQUENCES})
44
<module name="${PACKAGE}_${MODULE_NAME}">
5-
${SEQUENCES}
5+
<sequence>
6+
${SEQUENCES}
7+
</sequence>
68
</module>
79
#else
810
<module name="${PACKAGE}_${MODULE_NAME}" />

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public class ModuleComposerJsonData {
1414
private final String moduleDescription;
1515
private final String composerPackageName;
1616
private final String moduleVersion;
17-
private final List moduleLicense;
17+
private final List<String> moduleLicense;
18+
private final List<String> moduleDependencies;
1819

1920
public ModuleComposerJsonData(
2021
String packageName,
@@ -23,7 +24,8 @@ public ModuleComposerJsonData(
2324
String moduleDescription,
2425
String composerPackageName,
2526
String moduleVersion,
26-
List moduleLicense
27+
List<String> moduleLicense,
28+
List<String> moduleDependencies
2729
) {
2830
this.packageName = packageName;
2931
this.moduleName = moduleName;
@@ -32,6 +34,7 @@ public ModuleComposerJsonData(
3234
this.composerPackageName = composerPackageName;
3335
this.moduleVersion = moduleVersion;
3436
this.moduleLicense = moduleLicense;
37+
this.moduleDependencies = moduleDependencies;
3538
}
3639

3740
public String getPackageName() {
@@ -58,7 +61,11 @@ public String getModuleVersion() {
5861
return this.moduleVersion;
5962
}
6063

61-
public List getModuleLicense() {
64+
public List<String> getModuleLicense() {
6265
return this.moduleLicense;
6366
}
67+
68+
public List<String> getModuleDependencies() {
69+
return moduleDependencies;
70+
}
6471
}

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="4" column-count="2" 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="48" y="54" width="462" height="431"/>
6+
<xy x="48" y="54" width="462" height="496"/>
77
</constraints>
88
<properties>
9-
<preferredSize width="350" height="391"/>
9+
<preferredSize width="450" height="591"/>
1010
</properties>
1111
<border type="none"/>
1212
<children>
@@ -51,7 +51,7 @@
5151
</grid>
5252
</children>
5353
</grid>
54-
<grid id="e3588" layout-manager="GridLayoutManager" row-count="5" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
54+
<grid id="e3588" layout-manager="GridLayoutManager" row-count="6" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
5555
<margin top="0" left="0" bottom="0" right="0"/>
5656
<constraints>
5757
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@@ -141,7 +141,7 @@
141141
<scrollpane id="a545e" binding="moduleLicenseScrollPanel">
142142
<constraints>
143143
<grid row="3" column="1" row-span="1" col-span="2" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
144-
<preferred-size width="150" height="100"/>
144+
<preferred-size width="250" height="100"/>
145145
<maximum-size width="-1" height="100"/>
146146
</grid>
147147
</constraints>
@@ -159,6 +159,32 @@
159159
</component>
160160
</children>
161161
</scrollpane>
162+
<component id="bda0f" class="javax.swing.JLabel" binding="moduleDependenciesLabel">
163+
<constraints>
164+
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="9" fill="0" indent="0" use-parent-layout="false"/>
165+
</constraints>
166+
<properties>
167+
<text value="Module Dependencies"/>
168+
</properties>
169+
</component>
170+
<scrollpane id="90d78" binding="moduleDependenciesScrollPanel">
171+
<constraints>
172+
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
173+
<preferred-size width="250" height="200"/>
174+
<maximum-size width="-1" height="200"/>
175+
</grid>
176+
</constraints>
177+
<properties/>
178+
<border type="none"/>
179+
<children>
180+
<component id="12327" class="javax.swing.JList" binding="moduleDependencies">
181+
<constraints/>
182+
<properties>
183+
<enabled value="true"/>
184+
</properties>
185+
</component>
186+
</children>
187+
</scrollpane>
162188
</children>
163189
</grid>
164190
<component id="4c5ac" class="javax.swing.JTextArea" binding="moduleDescription">

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.intellij.openapi.project.Project;
1010
import com.intellij.psi.PsiDirectory;
1111
import com.intellij.psi.PsiFile;
12+
import com.intellij.util.indexing.FileBasedIndex;
1213
import com.magento.idea.magento2plugin.actions.generation.NewModuleAction;
1314
import com.magento.idea.magento2plugin.actions.generation.data.ModuleComposerJsonData;
1415
import com.magento.idea.magento2plugin.actions.generation.data.ModuleRegistrationPhpData;
@@ -20,14 +21,18 @@
2021
import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator;
2122
import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator;
2223
import com.magento.idea.magento2plugin.actions.generation.util.NavigateToCreatedFile;
24+
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
2325
import com.magento.idea.magento2plugin.magento.packages.Package;
26+
import com.magento.idea.magento2plugin.stubs.indexes.ModuleNameIndex;
2427
import com.magento.idea.magento2plugin.util.CamelCaseToHyphen;
2528
import org.jetbrains.annotations.NotNull;
2629
import org.jetbrains.annotations.Nullable;
2730
import javax.swing.*;
2831
import javax.swing.event.ListSelectionEvent;
2932
import javax.swing.event.ListSelectionListener;
3033
import java.awt.event.*;
34+
import java.util.ArrayList;
35+
import java.util.Collection;
3136
import java.util.List;
3237
import java.util.Vector;
3338

@@ -62,9 +67,19 @@ public class NewModuleDialog extends AbstractDialog implements ListSelectionList
6267
private JLabel moduleLicenseLabel;
6368
private JTextField moduleLicenseCustom;
6469
private JScrollPane moduleLicenseScrollPanel;
70+
private JList moduleDependencies;
71+
private JLabel moduleDependenciesLabel;
72+
private JScrollPane moduleDependenciesScrollPanel;
6573
private String detectedPackageName;
66-
67-
public NewModuleDialog(@NotNull Project project, @NotNull PsiDirectory initialBaseDir, @Nullable PsiFile file, @Nullable IdeView view, @Nullable Editor editor) {
74+
private ModuleIndex moduleIndex;
75+
76+
public NewModuleDialog(
77+
@NotNull Project project,
78+
@NotNull PsiDirectory initialBaseDir,
79+
@Nullable PsiFile file,
80+
@Nullable IdeView view,
81+
@Nullable Editor editor
82+
) {
6883
this.project = project;
6984
this.initialBaseDir = initialBaseDir;
7085
this.file = file;
@@ -75,12 +90,14 @@ public NewModuleDialog(@NotNull Project project, @NotNull PsiDirectory initialBa
7590
this.camelCaseToHyphen = CamelCaseToHyphen.getInstance();
7691
this.validator = NewModuleDialogValidator.getInstance(this);
7792
this.navigateToCreatedFile = NavigateToCreatedFile.getInstance();
93+
this.moduleIndex = ModuleIndex.getInstance(project);
7894
detectPackageName(initialBaseDir);
7995
setContentPane(contentPane);
8096
setModal(true);
8197
getRootPane().setDefaultButton(buttonOK);
8298
pushToMiddle();
8399
setLicenses();
100+
setModuleDependencies();
84101

85102
moduleLicenseCustom.setToolTipText("Custom License Name");
86103
moduleLicenseCustom.setText("proprietary");
@@ -149,7 +166,8 @@ private PsiFile generateComposerJson() {
149166
getModuleDescription(),
150167
getComposerPackageName(),
151168
getModuleVersion(),
152-
getModuleLicense()
169+
getModuleLicense(),
170+
getModuleDependencies()
153171
), project).generate(NewModuleAction.ACTION_NAME);
154172
}
155173

@@ -204,6 +222,10 @@ public List getModuleLicense() {
204222
return selectedLicenses;
205223
}
206224

225+
public List<String> getModuleDependencies() {
226+
return moduleDependencies.getSelectedValuesList();
227+
}
228+
207229
public static void open(@NotNull Project project, @NotNull PsiDirectory initialBaseDir, @Nullable PsiFile file, @Nullable IdeView view, @Nullable Editor editor) {
208230
NewModuleDialog dialog = new NewModuleDialog(project, initialBaseDir, file, view, editor);
209231
dialog.pack();
@@ -230,6 +252,17 @@ private void setLicenses() {
230252
moduleLicense.addListSelectionListener(this);
231253
}
232254

255+
private void setModuleDependencies() {
256+
List<String> moduleNames = moduleIndex.getModuleNames();
257+
Vector<String> licenseNames = new Vector<>(moduleNames.size());
258+
for (String name : moduleNames) {
259+
licenseNames.add(name);
260+
}
261+
moduleDependencies.setListData(licenseNames);
262+
moduleDependencies.setSelectedIndex(0);
263+
moduleDependencies.addListSelectionListener(this);
264+
}
265+
233266
private void handleModuleCustomLicenseInputVisibility () {
234267
boolean isCustomLicenseSelected = false;
235268

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@
44
*/
55
package com.magento.idea.magento2plugin.actions.generation.generator;
66

7+
import com.google.gson.JsonElement;
8+
import com.google.gson.JsonObject;
9+
import com.google.gson.JsonParser;
710
import com.intellij.openapi.project.Project;
11+
import com.intellij.openapi.vfs.VirtualFile;
812
import com.intellij.psi.PsiFile;
913
import com.magento.idea.magento2plugin.actions.generation.data.ModuleComposerJsonData;
1014
import com.magento.idea.magento2plugin.actions.generation.generator.data.ModuleDirectoriesData;
1115
import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator;
1216
import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator;
17+
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
1318
import com.magento.idea.magento2plugin.magento.files.ComposerJson;
19+
import com.magento.idea.magento2plugin.util.CamelCaseToHyphen;
1420
import org.jetbrains.annotations.NotNull;
21+
22+
import java.io.FileNotFoundException;
23+
import java.io.FileReader;
24+
import java.util.ArrayList;
1525
import java.util.Properties;
1626
import java.util.List;
1727

@@ -20,12 +30,18 @@ public class ModuleComposerJsonGenerator extends FileGenerator {
2030
private final ModuleComposerJsonData moduleComposerJsonData;
2131
private final FileFromTemplateGenerator fileFromTemplateGenerator;
2232
private final DirectoryGenerator directoryGenerator;
33+
private final CamelCaseToHyphen camelCaseToHyphen;
34+
private final Project project;
35+
private final ModuleIndex moduleIndex;
2336

2437
public ModuleComposerJsonGenerator(@NotNull ModuleComposerJsonData moduleComposerJsonData, Project project) {
2538
super(project);
2639
this.moduleComposerJsonData = moduleComposerJsonData;
2740
this.fileFromTemplateGenerator = FileFromTemplateGenerator.getInstance(project);
2841
this.directoryGenerator = DirectoryGenerator.getInstance();
42+
this.camelCaseToHyphen = CamelCaseToHyphen.getInstance();
43+
this.project = project;
44+
this.moduleIndex = ModuleIndex.getInstance(project);
2945
}
3046

3147
public PsiFile generate(String actionName) {
@@ -40,6 +56,7 @@ protected void fillAttributes(Properties attributes) {
4056
attributes.setProperty("COMPOSER_PACKAGE_NAME", moduleComposerJsonData.getComposerPackageName());
4157
attributes.setProperty("MODULE_VERSION", moduleComposerJsonData.getModuleVersion());
4258
attributes.setProperty("LICENSE", this.getLicensesString(moduleComposerJsonData.getModuleLicense()));
59+
attributes.setProperty("DEPENDENCIES", this.getDependenciesString(moduleComposerJsonData.getModuleDependencies()));
4360
}
4461

4562
protected String getLicensesString(List licensesList) {
@@ -60,4 +77,50 @@ protected String getLicensesString(List licensesList) {
6077

6178
return license;
6279
}
80+
81+
private String getDependenciesString(List dependenciesList) {
82+
String result = "";
83+
Object[] dependencies = dependenciesList.toArray();
84+
85+
for (int i = 0; i < dependencies.length; i++) {
86+
String dependency = dependencies[i].toString();
87+
result = result.concat("\"");
88+
result = result.concat(
89+
camelCaseToHyphen.convert(dependency).replace("_-", "/")
90+
);
91+
result = result.concat("\"");
92+
result = result.concat(": \"" + getDependencyVersion(dependency) + "\"");
93+
94+
if (dependencies.length != (i + 1)) {
95+
result = result.concat(",");
96+
}
97+
98+
result = result.concat("\n");
99+
}
100+
101+
return result;
102+
}
103+
104+
private String getDependencyVersion(String dependency) {
105+
String version = "*";
106+
try {
107+
VirtualFile virtualFile = moduleIndex.getModuleDirectoryByModuleName(dependency)
108+
.findFile("composer.json")
109+
.getVirtualFile();
110+
if (virtualFile.exists()) {
111+
JsonElement jsonElement = new JsonParser().parse(new FileReader(virtualFile.getPath()));
112+
JsonElement versionJsonElement = jsonElement.getAsJsonObject().get("version");
113+
if (versionJsonElement != null) {
114+
version = versionJsonElement.getAsString();
115+
int minorVersionSeparator = version.lastIndexOf(".");
116+
version = new StringBuilder(version)
117+
.replace(minorVersionSeparator + 1, version.length(),"*")
118+
.toString();
119+
}
120+
}
121+
} catch (FileNotFoundException e) {
122+
// It's fine
123+
}
124+
return version;
125+
}
63126
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator;
1313
import com.magento.idea.magento2plugin.magento.files.ModuleXml;
1414
import org.jetbrains.annotations.NotNull;
15+
16+
import java.util.List;
1517
import java.util.Properties;
1618

1719
public class ModuleXmlGenerator extends FileGenerator {

src/com/magento/idea/magento2plugin/indexes/ModuleIndex.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.Collection;
1919
import java.util.Collections;
2020
import java.util.List;
21+
import java.util.regex.Matcher;
22+
import java.util.regex.Pattern;
2123

2224
public class ModuleIndex {
2325

@@ -38,10 +40,19 @@ public static ModuleIndex getInstance(Project project) {
3840
}
3941

4042
public List<String> getEditableModuleNames() {
43+
return getModuleNames(Package.VENDOR);
44+
}
45+
46+
public List<String> getModuleNames() {
47+
return getModuleNames("/tests/|/test/");
48+
}
49+
50+
public List<String> getModuleNames(String filterPattern) {
4151
FileBasedIndex index = FileBasedIndex
4252
.getInstance();
4353
List<String> allModulesList = new ArrayList<>();
4454
Collection<String> allModules = index.getAllKeys(ModuleNameIndex.KEY, project);
55+
Pattern p = Pattern.compile(filterPattern);
4556
for (String moduleName : allModules) {
4657
if (!moduleName.matches(RegExUtil.Magento.MODULE_NAME)) {
4758
continue;
@@ -54,10 +65,10 @@ public List<String> getEditableModuleNames() {
5465
continue;
5566
}
5667
VirtualFile virtualFile = files.iterator().next();
57-
if (virtualFile.getPath().contains(Package.VENDOR)) {
68+
Matcher m = p.matcher(virtualFile.getPath());
69+
if (m.find()) {
5870
continue;
5971
}
60-
6172
allModulesList.add(moduleName);
6273
}
6374
Collections.sort(allModulesList);

0 commit comments

Comments
 (0)