Skip to content

Commit 398dabd

Browse files
author
makzef
committed
1016: Add optional generation of README.md during module creation
1 parent 351281b commit 398dabd

File tree

8 files changed

+250
-2
lines changed

8 files changed

+250
-2
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@
651651
<internalFileTemplate name="Magento Sections XML"/>
652652
<internalFileTemplate name="Magento Module Email Templates Xml"/>
653653
<internalFileTemplate name="Magento Page Types XML"/>
654+
<internalFileTemplate name="Magento Module Readme File MD"/>
654655

655656
<defaultLiveTemplates file="/liveTemplates/MagentoPWA.xml"/>
656657

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# ${PACKAGE}_${MODULE_NAME} module
2+
3+
<!---
4+
You can describe your module here.
5+
We recommend that you add the following information:
6+
- implementation details: why and how to use the module, preferably with some example scenarios
7+
- any dependencies (usually other modules but could be any important dependencies, libraries, etc)
8+
- extension points, APIs, plug-ins, etc
9+
- any introduced events
10+
-->
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!--
2+
/*
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<html>
8+
<body>
9+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
10+
<tr>
11+
<td><font face="verdana" size="-1">Magento readme file for module.</font><br>
12+
</td>
13+
</tr>
14+
</table>
15+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
16+
<tr>
17+
<td colspan="3"><font face="verdana" size="-1">Predefined variables will take the following values:</font></td>
18+
</tr>
19+
<tr>
20+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${PACKAGE}</b></font></nobr></td>
21+
<td width="10">&nbsp;</td>
22+
<td width="100%" valign="top"><font face="verdana" size="-1">Module's package name. Also known as Vendor name.</font></td>
23+
</tr>
24+
<tr>
25+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${MODULE_NAME}</b></font></nobr></td>
26+
<td width="10">&nbsp;</td>
27+
<td width="100%" valign="top"><font face="verdana" size="-1">Module's name.</font></td>
28+
</tr>
29+
</table>
30+
</body>
31+
</html>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.data;
7+
8+
import com.intellij.psi.PsiDirectory;
9+
10+
public class ModuleReadmeMdData {
11+
private final String packageName;
12+
private final String moduleName;
13+
private final PsiDirectory baseDir;
14+
private final boolean createModuleDirs;
15+
16+
/**
17+
* Constructor.
18+
*
19+
* @param packageName String
20+
* @param moduleName String
21+
* @param baseDir PsiDirectory
22+
* @param createModuleDirs boolean
23+
*/
24+
public ModuleReadmeMdData(
25+
final String packageName,
26+
final String moduleName,
27+
final PsiDirectory baseDir,
28+
final boolean createModuleDirs
29+
) {
30+
this.packageName = packageName;
31+
this.moduleName = moduleName;
32+
this.baseDir = baseDir;
33+
this.createModuleDirs = createModuleDirs;
34+
}
35+
36+
public String getPackageName() {
37+
return this.packageName;
38+
}
39+
40+
public String getModuleName() {
41+
return this.moduleName;
42+
}
43+
44+
public PsiDirectory getBaseDir() {
45+
return this.baseDir;
46+
}
47+
48+
public boolean hasCreateModuleDirs() {
49+
return this.createModuleDirs;
50+
}
51+
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
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="746" height="596"/>
6+
<xy x="48" y="54" width="746" height="642"/>
77
</constraints>
88
<properties>
99
<preferredSize width="450" height="591"/>
@@ -51,7 +51,7 @@
5151
</grid>
5252
</children>
5353
</grid>
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">
54+
<grid id="e3588" layout-manager="GridLayoutManager" row-count="7" 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"/>
@@ -199,6 +199,14 @@
199199
</component>
200200
</children>
201201
</scrollpane>
202+
<component id="56467" class="javax.swing.JCheckBox" binding="moduleReadmeMdCheckbox" default-binding="true">
203+
<constraints>
204+
<grid row="6" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
205+
</constraints>
206+
<properties>
207+
<text value="Generate README.md file"/>
208+
</properties>
209+
</component>
202210
</children>
203211
</grid>
204212
<component id="4c5ac" class="javax.swing.JTextArea" binding="moduleDescription">

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.intellij.psi.PsiFile;
1212
import com.magento.idea.magento2plugin.actions.generation.NewModuleAction;
1313
import com.magento.idea.magento2plugin.actions.generation.data.ModuleComposerJsonData;
14+
import com.magento.idea.magento2plugin.actions.generation.data.ModuleReadmeMdData;
1415
import com.magento.idea.magento2plugin.actions.generation.data.ModuleRegistrationPhpData;
1516
import com.magento.idea.magento2plugin.actions.generation.data.ModuleXmlData;
1617
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation;
@@ -19,6 +20,7 @@
1920
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
2021
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.StartWithNumberOrCapitalLetterRule;
2122
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleComposerJsonGenerator;
23+
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleReadmeMdGenerator;
2224
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleRegistrationPhpGenerator;
2325
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleXmlGenerator;
2426
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
@@ -35,6 +37,7 @@
3537
import java.util.List;
3638
import java.util.Vector;
3739
import javax.swing.JButton;
40+
import javax.swing.JCheckBox;
3841
import javax.swing.JComponent;
3942
import javax.swing.JLabel;
4043
import javax.swing.JList;
@@ -101,6 +104,7 @@ public class NewModuleDialog extends AbstractDialog implements ListSelectionList
101104

102105
private JButton buttonOK;
103106
private JButton buttonCancel;
107+
private JCheckBox moduleReadmeMdCheckbox;
104108

105109
@NotNull
106110
private final Project project;
@@ -191,6 +195,11 @@ private void generateFiles() {
191195
if (registrationPhp == null) {
192196
return;
193197
}
198+
199+
if (isCreateModuleReadme()) {
200+
generateReadmeMd();
201+
}
202+
194203
generateModuleXml();
195204
}
196205

@@ -228,6 +237,15 @@ private void generateModuleXml() {
228237
), project).generate(NewModuleAction.actionName, true);
229238
}
230239

240+
private void generateReadmeMd() {
241+
new ModuleReadmeMdGenerator(new ModuleReadmeMdData(
242+
getPackageName(),
243+
getModuleName(),
244+
getBaseDir(),
245+
true
246+
), project).generate(NewModuleAction.actionName);
247+
}
248+
231249
private PsiDirectory getBaseDir() {
232250
if (detectedPackageName != null) {
233251
return this.initialBaseDir.getParent();
@@ -319,6 +337,15 @@ public List<String> getModuleDependencies() {
319337
return moduleDependencies.getSelectedValuesList();
320338
}
321339

340+
/**
341+
* Getter for Module Readme Md Checkbox.
342+
*
343+
* @return Boolean
344+
*/
345+
public Boolean isCreateModuleReadme() {
346+
return this.moduleReadmeMdCheckbox.isSelected();
347+
}
348+
322349
/**
323350
* Open dialog.
324351
*
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.generator;
7+
8+
import com.intellij.openapi.project.Project;
9+
import com.intellij.psi.PsiFile;
10+
import com.magento.idea.magento2plugin.actions.generation.data.ModuleReadmeMdData;
11+
import com.magento.idea.magento2plugin.actions.generation.generator.data.ModuleDirectoriesData;
12+
import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator;
13+
import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator;
14+
import com.magento.idea.magento2plugin.magento.files.ModuleReadmeMd;
15+
import com.magento.idea.magento2plugin.magento.files.RegistrationPhp;
16+
import java.util.Properties;
17+
import org.jetbrains.annotations.NotNull;
18+
19+
public class ModuleReadmeMdGenerator extends FileGenerator {
20+
private final ModuleReadmeMdData moduleReadmeMdData;
21+
private final FileFromTemplateGenerator fileFromTemplateGenerator;
22+
private final DirectoryGenerator directoryGenerator;
23+
24+
/**
25+
* Construct generator.
26+
*
27+
* @param moduleReadmeMdData ModuleReadmeFileData
28+
* @param project Project
29+
*/
30+
public ModuleReadmeMdGenerator(
31+
final @NotNull ModuleReadmeMdData moduleReadmeMdData,
32+
final Project project
33+
) {
34+
super(project);
35+
this.moduleReadmeMdData = moduleReadmeMdData;
36+
this.fileFromTemplateGenerator = new FileFromTemplateGenerator(project);
37+
this.directoryGenerator = DirectoryGenerator.getInstance();
38+
}
39+
40+
/**
41+
* Generate file.
42+
*
43+
* @param actionName String
44+
* @return PsiFile
45+
*/
46+
@Override
47+
public PsiFile generate(final String actionName) {
48+
if (moduleReadmeMdData.hasCreateModuleDirs()) {
49+
final ModuleDirectoriesData moduleDirectoriesData = directoryGenerator
50+
.createOrFindModuleDirectories(
51+
moduleReadmeMdData.getPackageName(),
52+
moduleReadmeMdData.getModuleName(),
53+
moduleReadmeMdData.getBaseDir()
54+
);
55+
return fileFromTemplateGenerator.generate(
56+
ModuleReadmeMd.getInstance(),
57+
getAttributes(),
58+
moduleDirectoriesData.getModuleDirectory(),
59+
actionName
60+
);
61+
}
62+
return fileFromTemplateGenerator.generate(
63+
RegistrationPhp.getInstance(),
64+
getAttributes(),
65+
moduleReadmeMdData.getBaseDir(),
66+
actionName
67+
);
68+
}
69+
70+
/**
71+
* Fill template properties.
72+
*
73+
* @param attributes Properties
74+
*/
75+
@Override
76+
protected void fillAttributes(final Properties attributes) {
77+
attributes.setProperty("PACKAGE", moduleReadmeMdData.getPackageName());
78+
attributes.setProperty("MODULE_NAME", moduleReadmeMdData.getModuleName());
79+
}
80+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.magento.files;
7+
8+
import com.intellij.lang.Language;
9+
import com.intellij.lang.html.HTMLLanguage;
10+
11+
public class ModuleReadmeMd implements ModuleFileInterface {
12+
public static final String FILE_NAME = "README.md";
13+
public static final String TEMPLATE = "Magento Module Readme File MD";
14+
private static final ModuleReadmeMd INSTANCE = new ModuleReadmeMd();
15+
16+
/**
17+
* Getter for singleton instance of class.
18+
*/
19+
public static ModuleReadmeMd getInstance() {
20+
return INSTANCE;
21+
}
22+
23+
@Override
24+
public String getFileName() {
25+
return FILE_NAME;
26+
}
27+
28+
@Override
29+
public String getTemplate() {
30+
return TEMPLATE;
31+
}
32+
33+
@Override
34+
public Language getLanguage() {
35+
return HTMLLanguage.INSTANCE;
36+
}
37+
}
38+
39+
40+

0 commit comments

Comments
 (0)