From 398dabdb24488944228292817da676c2bb4a81f2 Mon Sep 17 00:00:00 2001 From: makzef Date: Wed, 8 Jun 2022 09:41:53 +0300 Subject: [PATCH 1/4] 1016: Add optional generation of README.md during module creation --- resources/META-INF/plugin.xml | 1 + .../Magento Module Readme File MD.md.ft | 10 +++ .../Magento Module Readme File MD.md.html | 31 +++++++ .../generation/data/ModuleReadmeMdData.java | 51 ++++++++++++ .../generation/dialog/NewModuleDialog.form | 12 ++- .../generation/dialog/NewModuleDialog.java | 27 +++++++ .../generator/ModuleReadmeMdGenerator.java | 80 +++++++++++++++++++ .../magento/files/ModuleReadmeMd.java | 40 ++++++++++ 8 files changed, 250 insertions(+), 2 deletions(-) create mode 100644 resources/fileTemplates/internal/Magento Module Readme File MD.md.ft create mode 100644 resources/fileTemplates/internal/Magento Module Readme File MD.md.html create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/data/ModuleReadmeMdData.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGenerator.java create mode 100644 src/com/magento/idea/magento2plugin/magento/files/ModuleReadmeMd.java diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index 86467690c..666db2c7f 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -651,6 +651,7 @@ + diff --git a/resources/fileTemplates/internal/Magento Module Readme File MD.md.ft b/resources/fileTemplates/internal/Magento Module Readme File MD.md.ft new file mode 100644 index 000000000..ecdb72f2d --- /dev/null +++ b/resources/fileTemplates/internal/Magento Module Readme File MD.md.ft @@ -0,0 +1,10 @@ +# ${PACKAGE}_${MODULE_NAME} module + + diff --git a/resources/fileTemplates/internal/Magento Module Readme File MD.md.html b/resources/fileTemplates/internal/Magento Module Readme File MD.md.html new file mode 100644 index 000000000..144eaf961 --- /dev/null +++ b/resources/fileTemplates/internal/Magento Module Readme File MD.md.html @@ -0,0 +1,31 @@ + + + + + + + +
Magento readme file for module.
+
+ + + + + + + + + + + + + + +
Predefined variables will take the following values:
${PACKAGE} Module's package name. Also known as Vendor name.
${MODULE_NAME} Module's name.
+ + diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleReadmeMdData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleReadmeMdData.java new file mode 100644 index 000000000..d1fdbc2f0 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleReadmeMdData.java @@ -0,0 +1,51 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.data; + +import com.intellij.psi.PsiDirectory; + +public class ModuleReadmeMdData { + private final String packageName; + private final String moduleName; + private final PsiDirectory baseDir; + private final boolean createModuleDirs; + + /** + * Constructor. + * + * @param packageName String + * @param moduleName String + * @param baseDir PsiDirectory + * @param createModuleDirs boolean + */ + public ModuleReadmeMdData( + final String packageName, + final String moduleName, + final PsiDirectory baseDir, + final boolean createModuleDirs + ) { + this.packageName = packageName; + this.moduleName = moduleName; + this.baseDir = baseDir; + this.createModuleDirs = createModuleDirs; + } + + public String getPackageName() { + return this.packageName; + } + + public String getModuleName() { + return this.moduleName; + } + + public PsiDirectory getBaseDir() { + return this.baseDir; + } + + public boolean hasCreateModuleDirs() { + return this.createModuleDirs; + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.form b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.form index 2ee53342c..a2fe0cbe2 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.form +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.form @@ -3,7 +3,7 @@ - + @@ -51,7 +51,7 @@ - + @@ -199,6 +199,14 @@ + + + + + + + + diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.java index 407e4d99c..7d1028a24 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.java @@ -11,6 +11,7 @@ import com.intellij.psi.PsiFile; import com.magento.idea.magento2plugin.actions.generation.NewModuleAction; import com.magento.idea.magento2plugin.actions.generation.data.ModuleComposerJsonData; +import com.magento.idea.magento2plugin.actions.generation.data.ModuleReadmeMdData; import com.magento.idea.magento2plugin.actions.generation.data.ModuleRegistrationPhpData; import com.magento.idea.magento2plugin.actions.generation.data.ModuleXmlData; import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation; @@ -19,6 +20,7 @@ import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule; import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.StartWithNumberOrCapitalLetterRule; import com.magento.idea.magento2plugin.actions.generation.generator.ModuleComposerJsonGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.ModuleReadmeMdGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.ModuleRegistrationPhpGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.ModuleXmlGenerator; import com.magento.idea.magento2plugin.indexes.ModuleIndex; @@ -35,6 +37,7 @@ import java.util.List; import java.util.Vector; import javax.swing.JButton; +import javax.swing.JCheckBox; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JList; @@ -101,6 +104,7 @@ public class NewModuleDialog extends AbstractDialog implements ListSelectionList private JButton buttonOK; private JButton buttonCancel; + private JCheckBox moduleReadmeMdCheckbox; @NotNull private final Project project; @@ -191,6 +195,11 @@ private void generateFiles() { if (registrationPhp == null) { return; } + + if (isCreateModuleReadme()) { + generateReadmeMd(); + } + generateModuleXml(); } @@ -228,6 +237,15 @@ private void generateModuleXml() { ), project).generate(NewModuleAction.actionName, true); } + private void generateReadmeMd() { + new ModuleReadmeMdGenerator(new ModuleReadmeMdData( + getPackageName(), + getModuleName(), + getBaseDir(), + true + ), project).generate(NewModuleAction.actionName); + } + private PsiDirectory getBaseDir() { if (detectedPackageName != null) { return this.initialBaseDir.getParent(); @@ -319,6 +337,15 @@ public List getModuleDependencies() { return moduleDependencies.getSelectedValuesList(); } + /** + * Getter for Module Readme Md Checkbox. + * + * @return Boolean + */ + public Boolean isCreateModuleReadme() { + return this.moduleReadmeMdCheckbox.isSelected(); + } + /** * Open dialog. * diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGenerator.java new file mode 100644 index 000000000..a440ee608 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGenerator.java @@ -0,0 +1,80 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator; + +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiFile; +import com.magento.idea.magento2plugin.actions.generation.data.ModuleReadmeMdData; +import com.magento.idea.magento2plugin.actions.generation.generator.data.ModuleDirectoriesData; +import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator; +import com.magento.idea.magento2plugin.magento.files.ModuleReadmeMd; +import com.magento.idea.magento2plugin.magento.files.RegistrationPhp; +import java.util.Properties; +import org.jetbrains.annotations.NotNull; + +public class ModuleReadmeMdGenerator extends FileGenerator { + private final ModuleReadmeMdData moduleReadmeMdData; + private final FileFromTemplateGenerator fileFromTemplateGenerator; + private final DirectoryGenerator directoryGenerator; + + /** + * Construct generator. + * + * @param moduleReadmeMdData ModuleReadmeFileData + * @param project Project + */ + public ModuleReadmeMdGenerator( + final @NotNull ModuleReadmeMdData moduleReadmeMdData, + final Project project + ) { + super(project); + this.moduleReadmeMdData = moduleReadmeMdData; + this.fileFromTemplateGenerator = new FileFromTemplateGenerator(project); + this.directoryGenerator = DirectoryGenerator.getInstance(); + } + + /** + * Generate file. + * + * @param actionName String + * @return PsiFile + */ + @Override + public PsiFile generate(final String actionName) { + if (moduleReadmeMdData.hasCreateModuleDirs()) { + final ModuleDirectoriesData moduleDirectoriesData = directoryGenerator + .createOrFindModuleDirectories( + moduleReadmeMdData.getPackageName(), + moduleReadmeMdData.getModuleName(), + moduleReadmeMdData.getBaseDir() + ); + return fileFromTemplateGenerator.generate( + ModuleReadmeMd.getInstance(), + getAttributes(), + moduleDirectoriesData.getModuleDirectory(), + actionName + ); + } + return fileFromTemplateGenerator.generate( + RegistrationPhp.getInstance(), + getAttributes(), + moduleReadmeMdData.getBaseDir(), + actionName + ); + } + + /** + * Fill template properties. + * + * @param attributes Properties + */ + @Override + protected void fillAttributes(final Properties attributes) { + attributes.setProperty("PACKAGE", moduleReadmeMdData.getPackageName()); + attributes.setProperty("MODULE_NAME", moduleReadmeMdData.getModuleName()); + } +} diff --git a/src/com/magento/idea/magento2plugin/magento/files/ModuleReadmeMd.java b/src/com/magento/idea/magento2plugin/magento/files/ModuleReadmeMd.java new file mode 100644 index 000000000..9b188b299 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/files/ModuleReadmeMd.java @@ -0,0 +1,40 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.magento.files; + +import com.intellij.lang.Language; +import com.intellij.lang.html.HTMLLanguage; + +public class ModuleReadmeMd implements ModuleFileInterface { + public static final String FILE_NAME = "README.md"; + public static final String TEMPLATE = "Magento Module Readme File MD"; + private static final ModuleReadmeMd INSTANCE = new ModuleReadmeMd(); + + /** + * Getter for singleton instance of class. + */ + public static ModuleReadmeMd getInstance() { + return INSTANCE; + } + + @Override + public String getFileName() { + return FILE_NAME; + } + + @Override + public String getTemplate() { + return TEMPLATE; + } + + @Override + public Language getLanguage() { + return HTMLLanguage.INSTANCE; + } +} + + + From aa6f920321ce25b18d265169da82d18e10fdd69d Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 17 Jun 2022 21:08:09 +0300 Subject: [PATCH 2/4] 1016: Code refactoring --- .../generation/data/ModuleReadmeMdData.java | 28 ++++++--------- .../generation/dialog/NewModuleDialog.form | 2 +- .../generation/dialog/NewModuleDialog.java | 3 +- .../generator/ModuleReadmeMdGenerator.java | 34 +++++++++---------- .../magento/files/ModuleReadmeMd.java | 4 +-- 5 files changed, 31 insertions(+), 40 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleReadmeMdData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleReadmeMdData.java index d1fdbc2f0..f7d383cf4 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleReadmeMdData.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleReadmeMdData.java @@ -6,12 +6,13 @@ package com.magento.idea.magento2plugin.actions.generation.data; import com.intellij.psi.PsiDirectory; +import org.jetbrains.annotations.NotNull; public class ModuleReadmeMdData { + private final String packageName; private final String moduleName; private final PsiDirectory baseDir; - private final boolean createModuleDirs; /** * Constructor. @@ -19,33 +20,26 @@ public class ModuleReadmeMdData { * @param packageName String * @param moduleName String * @param baseDir PsiDirectory - * @param createModuleDirs boolean */ public ModuleReadmeMdData( - final String packageName, - final String moduleName, - final PsiDirectory baseDir, - final boolean createModuleDirs + final @NotNull String packageName, + final @NotNull String moduleName, + final @NotNull PsiDirectory baseDir ) { this.packageName = packageName; this.moduleName = moduleName; this.baseDir = baseDir; - this.createModuleDirs = createModuleDirs; - } - - public String getPackageName() { - return this.packageName; } - public String getModuleName() { - return this.moduleName; + public @NotNull String getPackageName() { + return packageName; } - public PsiDirectory getBaseDir() { - return this.baseDir; + public @NotNull String getModuleName() { + return moduleName; } - public boolean hasCreateModuleDirs() { - return this.createModuleDirs; + public @NotNull PsiDirectory getBaseDir() { + return baseDir; } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.form b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.form index a2fe0cbe2..6a811c41e 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.form +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.form @@ -6,7 +6,7 @@ - + diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.java index 7d1028a24..4217a8a5c 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.java @@ -241,8 +241,7 @@ private void generateReadmeMd() { new ModuleReadmeMdGenerator(new ModuleReadmeMdData( getPackageName(), getModuleName(), - getBaseDir(), - true + getBaseDir() ), project).generate(NewModuleAction.actionName); } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGenerator.java index a440ee608..c1166ec48 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGenerator.java @@ -6,17 +6,18 @@ package com.magento.idea.magento2plugin.actions.generation.generator; import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiFile; import com.magento.idea.magento2plugin.actions.generation.data.ModuleReadmeMdData; -import com.magento.idea.magento2plugin.actions.generation.generator.data.ModuleDirectoriesData; import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator; import com.magento.idea.magento2plugin.magento.files.ModuleReadmeMd; -import com.magento.idea.magento2plugin.magento.files.RegistrationPhp; import java.util.Properties; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public class ModuleReadmeMdGenerator extends FileGenerator { + private final ModuleReadmeMdData moduleReadmeMdData; private final FileFromTemplateGenerator fileFromTemplateGenerator; private final DirectoryGenerator directoryGenerator; @@ -41,28 +42,21 @@ public ModuleReadmeMdGenerator( * Generate file. * * @param actionName String + * * @return PsiFile */ @Override public PsiFile generate(final String actionName) { - if (moduleReadmeMdData.hasCreateModuleDirs()) { - final ModuleDirectoriesData moduleDirectoriesData = directoryGenerator - .createOrFindModuleDirectories( - moduleReadmeMdData.getPackageName(), - moduleReadmeMdData.getModuleName(), - moduleReadmeMdData.getBaseDir() - ); - return fileFromTemplateGenerator.generate( - ModuleReadmeMd.getInstance(), - getAttributes(), - moduleDirectoriesData.getModuleDirectory(), - actionName - ); + final PsiDirectory moduleDir = resolveModuleRoot(moduleReadmeMdData); + + if (moduleDir == null) { + return null; } + return fileFromTemplateGenerator.generate( - RegistrationPhp.getInstance(), + ModuleReadmeMd.getInstance(), getAttributes(), - moduleReadmeMdData.getBaseDir(), + moduleDir, actionName ); } @@ -77,4 +71,10 @@ protected void fillAttributes(final Properties attributes) { attributes.setProperty("PACKAGE", moduleReadmeMdData.getPackageName()); attributes.setProperty("MODULE_NAME", moduleReadmeMdData.getModuleName()); } + + private @Nullable PsiDirectory resolveModuleRoot(final @NotNull ModuleReadmeMdData data) { + final PsiDirectory packageDir = data.getBaseDir().findSubdirectory(data.getPackageName()); + + return packageDir != null ? packageDir.findSubdirectory(data.getModuleName()) : null; + } } diff --git a/src/com/magento/idea/magento2plugin/magento/files/ModuleReadmeMd.java b/src/com/magento/idea/magento2plugin/magento/files/ModuleReadmeMd.java index 9b188b299..fd27ad916 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/ModuleReadmeMd.java +++ b/src/com/magento/idea/magento2plugin/magento/files/ModuleReadmeMd.java @@ -9,6 +9,7 @@ import com.intellij.lang.html.HTMLLanguage; public class ModuleReadmeMd implements ModuleFileInterface { + public static final String FILE_NAME = "README.md"; public static final String TEMPLATE = "Magento Module Readme File MD"; private static final ModuleReadmeMd INSTANCE = new ModuleReadmeMd(); @@ -35,6 +36,3 @@ public Language getLanguage() { return HTMLLanguage.INSTANCE; } } - - - From ad47f80f7fac66a36e00b7c1bbe631e90f530e45 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 17 Jun 2022 21:53:03 +0300 Subject: [PATCH 3/4] 1016: Added test case and code refactoring --- .../generator/ModuleReadmeMdGenerator.java | 10 ++---- ...eReadmeMd.java => ModuleReadmeMdFile.java} | 10 +----- .../generateModuleReadmeMdFile/README.md | 10 ++++++ .../generator/BaseGeneratorTestCase.java | 9 +++++ .../ModuleReadmeMdGeneratorTest.java | 36 +++++++++++++++++++ 5 files changed, 59 insertions(+), 16 deletions(-) rename src/com/magento/idea/magento2plugin/magento/files/{ModuleReadmeMd.java => ModuleReadmeMdFile.java} (69%) create mode 100644 testData/actions/generation/generator/ModuleReadmeMdGenerator/generateModuleReadmeMdFile/README.md create mode 100644 tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGeneratorTest.java diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGenerator.java index c1166ec48..435c6fc86 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGenerator.java @@ -11,7 +11,7 @@ import com.magento.idea.magento2plugin.actions.generation.data.ModuleReadmeMdData; import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator; -import com.magento.idea.magento2plugin.magento.files.ModuleReadmeMd; +import com.magento.idea.magento2plugin.magento.files.ModuleReadmeMdFile; import java.util.Properties; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -49,14 +49,10 @@ public ModuleReadmeMdGenerator( public PsiFile generate(final String actionName) { final PsiDirectory moduleDir = resolveModuleRoot(moduleReadmeMdData); - if (moduleDir == null) { - return null; - } - return fileFromTemplateGenerator.generate( - ModuleReadmeMd.getInstance(), + new ModuleReadmeMdFile(), getAttributes(), - moduleDir, + moduleDir != null ? moduleDir : moduleReadmeMdData.getBaseDir(), actionName ); } diff --git a/src/com/magento/idea/magento2plugin/magento/files/ModuleReadmeMd.java b/src/com/magento/idea/magento2plugin/magento/files/ModuleReadmeMdFile.java similarity index 69% rename from src/com/magento/idea/magento2plugin/magento/files/ModuleReadmeMd.java rename to src/com/magento/idea/magento2plugin/magento/files/ModuleReadmeMdFile.java index fd27ad916..cd0d27b8f 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/ModuleReadmeMd.java +++ b/src/com/magento/idea/magento2plugin/magento/files/ModuleReadmeMdFile.java @@ -8,18 +8,10 @@ import com.intellij.lang.Language; import com.intellij.lang.html.HTMLLanguage; -public class ModuleReadmeMd implements ModuleFileInterface { +public class ModuleReadmeMdFile implements ModuleFileInterface { public static final String FILE_NAME = "README.md"; public static final String TEMPLATE = "Magento Module Readme File MD"; - private static final ModuleReadmeMd INSTANCE = new ModuleReadmeMd(); - - /** - * Getter for singleton instance of class. - */ - public static ModuleReadmeMd getInstance() { - return INSTANCE; - } @Override public String getFileName() { diff --git a/testData/actions/generation/generator/ModuleReadmeMdGenerator/generateModuleReadmeMdFile/README.md b/testData/actions/generation/generator/ModuleReadmeMdGenerator/generateModuleReadmeMdFile/README.md new file mode 100644 index 000000000..084c0b6e3 --- /dev/null +++ b/testData/actions/generation/generator/ModuleReadmeMdGenerator/generateModuleReadmeMdFile/README.md @@ -0,0 +1,10 @@ +# Foo_Bar module + + diff --git a/tests/com/magento/idea/magento2plugin/actions/generation/generator/BaseGeneratorTestCase.java b/tests/com/magento/idea/magento2plugin/actions/generation/generator/BaseGeneratorTestCase.java index e7205e5d9..c713e54ac 100644 --- a/tests/com/magento/idea/magento2plugin/actions/generation/generator/BaseGeneratorTestCase.java +++ b/tests/com/magento/idea/magento2plugin/actions/generation/generator/BaseGeneratorTestCase.java @@ -53,6 +53,15 @@ protected void assertGeneratedFileIsCorrect( assertEquals(expectedFile.getName(), resultFile.getName()); } + @SuppressWarnings({"PMD.JUnitAssertionsShouldIncludeMessage"}) + protected void assertGeneratedFileIsCorrect( + final PsiFile expectedFile, + final PsiFile resultFile + ) { + assertEquals(expectedFile.getText(), resultFile.getText()); + assertEquals(expectedFile.getName(), resultFile.getName()); + } + protected PsiDirectory getProjectDirectory() { return myFixture.getPsiManager().findDirectory( GetProjectBasePath.execute(myFixture.getProject()) diff --git a/tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGeneratorTest.java b/tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGeneratorTest.java new file mode 100644 index 000000000..d4c130cf8 --- /dev/null +++ b/tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGeneratorTest.java @@ -0,0 +1,36 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator; + +import com.intellij.psi.PsiFile; +import com.magento.idea.magento2plugin.actions.generation.data.ModuleReadmeMdData; +import com.magento.idea.magento2plugin.magento.files.ModuleReadmeMdFile; + +public final class ModuleReadmeMdGeneratorTest extends BaseGeneratorTestCase { + + /** + * Test module README.md file generation. + */ + public void testGenerateModuleReadmeMdFile() { + final PsiFile expectedFile = myFixture.configureByFile( + getFixturePath(ModuleReadmeMdFile.FILE_NAME) + ); + final ModuleReadmeMdGenerator generator = new ModuleReadmeMdGenerator( + new ModuleReadmeMdData( + "Foo", + "Bar", + getProjectDirectory() + ), + myFixture.getProject() + ); + final PsiFile generatedFile = generator.generate("test"); + + assertGeneratedFileIsCorrect( + expectedFile, + generatedFile + ); + } +} From d118c2144ee73e76fa197926c6fb0414aa8e2aaa Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 17 Jun 2022 21:59:43 +0300 Subject: [PATCH 4/4] 1016: code style fixes --- .../generation/generator/ModuleReadmeMdGenerator.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGenerator.java index 435c6fc86..72c07614c 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleReadmeMdGenerator.java @@ -9,7 +9,6 @@ import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiFile; import com.magento.idea.magento2plugin.actions.generation.data.ModuleReadmeMdData; -import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator; import com.magento.idea.magento2plugin.magento.files.ModuleReadmeMdFile; import java.util.Properties; @@ -20,7 +19,6 @@ public class ModuleReadmeMdGenerator extends FileGenerator { private final ModuleReadmeMdData moduleReadmeMdData; private final FileFromTemplateGenerator fileFromTemplateGenerator; - private final DirectoryGenerator directoryGenerator; /** * Construct generator. @@ -35,7 +33,6 @@ public ModuleReadmeMdGenerator( super(project); this.moduleReadmeMdData = moduleReadmeMdData; this.fileFromTemplateGenerator = new FileFromTemplateGenerator(project); - this.directoryGenerator = DirectoryGenerator.getInstance(); } /** @@ -52,7 +49,7 @@ public PsiFile generate(final String actionName) { return fileFromTemplateGenerator.generate( new ModuleReadmeMdFile(), getAttributes(), - moduleDir != null ? moduleDir : moduleReadmeMdData.getBaseDir(), + moduleDir == null ? moduleReadmeMdData.getBaseDir() : moduleDir, actionName ); } @@ -71,6 +68,6 @@ protected void fillAttributes(final Properties attributes) { private @Nullable PsiDirectory resolveModuleRoot(final @NotNull ModuleReadmeMdData data) { final PsiDirectory packageDir = data.getBaseDir().findSubdirectory(data.getPackageName()); - return packageDir != null ? packageDir.findSubdirectory(data.getModuleName()) : null; + return packageDir == null ? null : packageDir.findSubdirectory(data.getModuleName()); } }