Skip to content

Commit 6cb0b5f

Browse files
Merge pull request #1110 from makzef/1016-generation-of-readme-file-option
1016: Add optional generation of README.md during module creation
2 parents 8ecd5c9 + d118c21 commit 6cb0b5f

File tree

11 files changed

+282
-3
lines changed

11 files changed

+282
-3
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@
657657
<internalFileTemplate name="Magento Sections XML"/>
658658
<internalFileTemplate name="Magento Module Email Templates Xml"/>
659659
<internalFileTemplate name="Magento Page Types XML"/>
660+
<internalFileTemplate name="Magento Module Readme File MD"/>
660661

661662
<defaultLiveTemplates file="/liveTemplates/MagentoPWA.xml"/>
662663

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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
import org.jetbrains.annotations.NotNull;
10+
11+
public class ModuleReadmeMdData {
12+
13+
private final String packageName;
14+
private final String moduleName;
15+
private final PsiDirectory baseDir;
16+
17+
/**
18+
* Constructor.
19+
*
20+
* @param packageName String
21+
* @param moduleName String
22+
* @param baseDir PsiDirectory
23+
*/
24+
public ModuleReadmeMdData(
25+
final @NotNull String packageName,
26+
final @NotNull String moduleName,
27+
final @NotNull PsiDirectory baseDir
28+
) {
29+
this.packageName = packageName;
30+
this.moduleName = moduleName;
31+
this.baseDir = baseDir;
32+
}
33+
34+
public @NotNull String getPackageName() {
35+
return packageName;
36+
}
37+
38+
public @NotNull String getModuleName() {
39+
return moduleName;
40+
}
41+
42+
public @NotNull PsiDirectory getBaseDir() {
43+
return baseDir;
44+
}
45+
}

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

Lines changed: 11 additions & 3 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="746" height="596"/>
6+
<xy x="48" y="54" width="746" height="642"/>
77
</constraints>
88
<properties>
9-
<preferredSize width="450" height="591"/>
9+
<preferredSize width="455" height="600"/>
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="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: 26 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,14 @@ 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+
), project).generate(NewModuleAction.actionName);
246+
}
247+
231248
private PsiDirectory getBaseDir() {
232249
if (detectedPackageName != null) {
233250
return this.initialBaseDir.getParent();
@@ -319,6 +336,15 @@ public List<String> getModuleDependencies() {
319336
return moduleDependencies.getSelectedValuesList();
320337
}
321338

339+
/**
340+
* Getter for Module Readme Md Checkbox.
341+
*
342+
* @return Boolean
343+
*/
344+
public Boolean isCreateModuleReadme() {
345+
return this.moduleReadmeMdCheckbox.isSelected();
346+
}
347+
322348
/**
323349
* Open dialog.
324350
*
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.PsiDirectory;
10+
import com.intellij.psi.PsiFile;
11+
import com.magento.idea.magento2plugin.actions.generation.data.ModuleReadmeMdData;
12+
import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator;
13+
import com.magento.idea.magento2plugin.magento.files.ModuleReadmeMdFile;
14+
import java.util.Properties;
15+
import org.jetbrains.annotations.NotNull;
16+
import org.jetbrains.annotations.Nullable;
17+
18+
public class ModuleReadmeMdGenerator extends FileGenerator {
19+
20+
private final ModuleReadmeMdData moduleReadmeMdData;
21+
private final FileFromTemplateGenerator fileFromTemplateGenerator;
22+
23+
/**
24+
* Construct generator.
25+
*
26+
* @param moduleReadmeMdData ModuleReadmeFileData
27+
* @param project Project
28+
*/
29+
public ModuleReadmeMdGenerator(
30+
final @NotNull ModuleReadmeMdData moduleReadmeMdData,
31+
final Project project
32+
) {
33+
super(project);
34+
this.moduleReadmeMdData = moduleReadmeMdData;
35+
this.fileFromTemplateGenerator = new FileFromTemplateGenerator(project);
36+
}
37+
38+
/**
39+
* Generate file.
40+
*
41+
* @param actionName String
42+
*
43+
* @return PsiFile
44+
*/
45+
@Override
46+
public PsiFile generate(final String actionName) {
47+
final PsiDirectory moduleDir = resolveModuleRoot(moduleReadmeMdData);
48+
49+
return fileFromTemplateGenerator.generate(
50+
new ModuleReadmeMdFile(),
51+
getAttributes(),
52+
moduleDir == null ? moduleReadmeMdData.getBaseDir() : moduleDir,
53+
actionName
54+
);
55+
}
56+
57+
/**
58+
* Fill template properties.
59+
*
60+
* @param attributes Properties
61+
*/
62+
@Override
63+
protected void fillAttributes(final Properties attributes) {
64+
attributes.setProperty("PACKAGE", moduleReadmeMdData.getPackageName());
65+
attributes.setProperty("MODULE_NAME", moduleReadmeMdData.getModuleName());
66+
}
67+
68+
private @Nullable PsiDirectory resolveModuleRoot(final @NotNull ModuleReadmeMdData data) {
69+
final PsiDirectory packageDir = data.getBaseDir().findSubdirectory(data.getPackageName());
70+
71+
return packageDir == null ? null : packageDir.findSubdirectory(data.getModuleName());
72+
}
73+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 ModuleReadmeMdFile implements ModuleFileInterface {
12+
13+
public static final String FILE_NAME = "README.md";
14+
public static final String TEMPLATE = "Magento Module Readme File MD";
15+
16+
@Override
17+
public String getFileName() {
18+
return FILE_NAME;
19+
}
20+
21+
@Override
22+
public String getTemplate() {
23+
return TEMPLATE;
24+
}
25+
26+
@Override
27+
public Language getLanguage() {
28+
return HTMLLanguage.INSTANCE;
29+
}
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Foo_Bar 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+
-->

tests/com/magento/idea/magento2plugin/actions/generation/generator/BaseGeneratorTestCase.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ protected void assertGeneratedFileIsCorrect(
5353
assertEquals(expectedFile.getName(), resultFile.getName());
5454
}
5555

56+
@SuppressWarnings({"PMD.JUnitAssertionsShouldIncludeMessage"})
57+
protected void assertGeneratedFileIsCorrect(
58+
final PsiFile expectedFile,
59+
final PsiFile resultFile
60+
) {
61+
assertEquals(expectedFile.getText(), resultFile.getText());
62+
assertEquals(expectedFile.getName(), resultFile.getName());
63+
}
64+
5665
protected PsiDirectory getProjectDirectory() {
5766
return myFixture.getPsiManager().findDirectory(
5867
GetProjectBasePath.execute(myFixture.getProject())
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.psi.PsiFile;
9+
import com.magento.idea.magento2plugin.actions.generation.data.ModuleReadmeMdData;
10+
import com.magento.idea.magento2plugin.magento.files.ModuleReadmeMdFile;
11+
12+
public final class ModuleReadmeMdGeneratorTest extends BaseGeneratorTestCase {
13+
14+
/**
15+
* Test module README.md file generation.
16+
*/
17+
public void testGenerateModuleReadmeMdFile() {
18+
final PsiFile expectedFile = myFixture.configureByFile(
19+
getFixturePath(ModuleReadmeMdFile.FILE_NAME)
20+
);
21+
final ModuleReadmeMdGenerator generator = new ModuleReadmeMdGenerator(
22+
new ModuleReadmeMdData(
23+
"Foo",
24+
"Bar",
25+
getProjectDirectory()
26+
),
27+
myFixture.getProject()
28+
);
29+
final PsiFile generatedFile = generator.generate("test");
30+
31+
assertGeneratedFileIsCorrect(
32+
expectedFile,
33+
generatedFile
34+
);
35+
}
36+
}

0 commit comments

Comments
 (0)