Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1016: Add optional generation of README.md during module creation #1110

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@
<internalFileTemplate name="Magento Sections XML"/>
<internalFileTemplate name="Magento Module Email Templates Xml"/>
<internalFileTemplate name="Magento Page Types XML"/>
<internalFileTemplate name="Magento Module Readme File MD"/>

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# ${PACKAGE}_${MODULE_NAME} module

<!---
You can describe your module here.
We recommend that you add the following information:
- implementation details: why and how to use the module, preferably with some example scenarios
- any dependencies (usually other modules but could be any important dependencies, libraries, etc)
- extension points, APIs, plug-ins, etc
- any introduced events
-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<html>
<body>
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
<tr>
<td><font face="verdana" size="-1">Magento readme file for module.</font><br>
</td>
</tr>
</table>
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
<tr>
<td colspan="3"><font face="verdana" size="-1">Predefined variables will take the following values:</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${PACKAGE}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">Module's package name. Also known as Vendor name.</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${MODULE_NAME}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">Module's name.</font></td>
</tr>
</table>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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;
import org.jetbrains.annotations.NotNull;

public class ModuleReadmeMdData {

private final String packageName;
private final String moduleName;
private final PsiDirectory baseDir;

/**
* Constructor.
*
* @param packageName String
* @param moduleName String
* @param baseDir PsiDirectory
*/
public ModuleReadmeMdData(
final @NotNull String packageName,
final @NotNull String moduleName,
final @NotNull PsiDirectory baseDir
) {
this.packageName = packageName;
this.moduleName = moduleName;
this.baseDir = baseDir;
}

public @NotNull String getPackageName() {
return packageName;
}

public @NotNull String getModuleName() {
return moduleName;
}

public @NotNull PsiDirectory getBaseDir() {
return baseDir;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<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">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="746" height="596"/>
<xy x="48" y="54" width="746" height="642"/>
</constraints>
<properties>
<preferredSize width="450" height="591"/>
<preferredSize width="455" height="600"/>
</properties>
<border type="none"/>
<children>
Expand Down Expand Up @@ -51,7 +51,7 @@
</grid>
</children>
</grid>
<grid id="e3588" layout-manager="GridLayoutManager" row-count="6" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="e3588" layout-manager="GridLayoutManager" row-count="7" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<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"/>
Expand Down Expand Up @@ -199,6 +199,14 @@
</component>
</children>
</scrollpane>
<component id="56467" class="javax.swing.JCheckBox" binding="moduleReadmeMdCheckbox" default-binding="true">
<constraints>
<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"/>
</constraints>
<properties>
<text value="Generate README.md file"/>
</properties>
</component>
</children>
</grid>
<component id="4c5ac" class="javax.swing.JTextArea" binding="moduleDescription">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -191,6 +195,11 @@ private void generateFiles() {
if (registrationPhp == null) {
return;
}

if (isCreateModuleReadme()) {
generateReadmeMd();
}

generateModuleXml();
}

Expand Down Expand Up @@ -228,6 +237,14 @@ private void generateModuleXml() {
), project).generate(NewModuleAction.actionName, true);
}

private void generateReadmeMd() {
new ModuleReadmeMdGenerator(new ModuleReadmeMdData(
getPackageName(),
getModuleName(),
getBaseDir()
), project).generate(NewModuleAction.actionName);
}

private PsiDirectory getBaseDir() {
if (detectedPackageName != null) {
return this.initialBaseDir.getParent();
Expand Down Expand Up @@ -319,6 +336,15 @@ public List<String> getModuleDependencies() {
return moduleDependencies.getSelectedValuesList();
}

/**
* Getter for Module Readme Md Checkbox.
*
* @return Boolean
*/
public Boolean isCreateModuleReadme() {
return this.moduleReadmeMdCheckbox.isSelected();
}

/**
* Open dialog.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.magento.idea.magento2plugin.actions.generation.data.ModuleReadmeMdData;
import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator;
import com.magento.idea.magento2plugin.magento.files.ModuleReadmeMdFile;
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;

/**
* 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);
}

/**
* Generate file.
*
* @param actionName String
*
* @return PsiFile
*/
@Override
public PsiFile generate(final String actionName) {
final PsiDirectory moduleDir = resolveModuleRoot(moduleReadmeMdData);

return fileFromTemplateGenerator.generate(
new ModuleReadmeMdFile(),
getAttributes(),
moduleDir == null ? moduleReadmeMdData.getBaseDir() : moduleDir,
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());
}

private @Nullable PsiDirectory resolveModuleRoot(final @NotNull ModuleReadmeMdData data) {
final PsiDirectory packageDir = data.getBaseDir().findSubdirectory(data.getPackageName());

return packageDir == null ? null : packageDir.findSubdirectory(data.getModuleName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 ModuleReadmeMdFile implements ModuleFileInterface {

public static final String FILE_NAME = "README.md";
public static final String TEMPLATE = "Magento Module Readme File MD";

@Override
public String getFileName() {
return FILE_NAME;
}

@Override
public String getTemplate() {
return TEMPLATE;
}

@Override
public Language getLanguage() {
return HTMLLanguage.INSTANCE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Foo_Bar module

<!---
You can describe your module here.
We recommend that you add the following information:
- implementation details: why and how to use the module, preferably with some example scenarios
- any dependencies (usually other modules but could be any important dependencies, libraries, etc)
- extension points, APIs, plug-ins, etc
- any introduced events
-->
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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
);
}
}