From 8c7e9a113d8062857b13e321f57a8f6e07be1825 Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Thu, 9 Apr 2020 12:18:13 +0300 Subject: [PATCH 1/6] Added Magento 2 module project template --- resources/META-INF/plugin.xml | 3 + .../actions/generation/NewModuleAction.java | 44 +++- .../data/ModuleComposerJsonData.java | 8 +- .../data/ModuleRegistrationPhpData.java | 7 +- .../generation/data/ModuleXmlData.java | 7 +- .../generation/dialog/NewModuleDialog.java | 9 +- .../ModuleComposerJsonGenerator.java | 19 +- .../ModuleRegistrationPhpGenerator.java | 8 +- .../generator/ModuleXmlGenerator.java | 13 +- .../util/IsClickedDirectoryInsideProject.java | 27 ++ .../actions/groups/NewModuleFileGroup.java | 6 + .../generation/MagentoModuleGenerator.java | 121 +++++++++ .../MagentoProjectGeneratorSettings.java | 65 +++++ .../generation/MagentoProjectPeer.java | 52 ++++ .../generation/MagentoTemplatesFactory.java | 32 +++ .../generation/NewModuleForm.form | 161 ++++++++++++ .../generation/NewModuleForm.java | 221 ++++++++++++++++ .../validator/NewModuleFormValidator.java | 65 +++++ .../magento2plugin/indexes/ModuleIndex.java | 13 +- .../init/ConfigurationManager.java | 247 ++++++++++++++++++ .../magento/files/ComposerJson.java | 1 + .../magento/packages/Package.java | 1 + .../project/ProjectDetector.java | 18 +- .../idea/magento2plugin/project/Settings.java | 179 ++++++++++++- .../magento2plugin/project/SettingsForm.form | 40 ++- .../magento2plugin/project/SettingsForm.java | 58 +++- .../project/util/GetProjectBasePath.java | 21 ++ .../magento/GetModuleNameByDirectory.java | 8 +- .../util/magento/MagentoBasePathUtil.java | 29 ++ 29 files changed, 1408 insertions(+), 75 deletions(-) create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/util/IsClickedDirectoryInsideProject.java create mode 100644 src/com/magento/idea/magento2plugin/generation/MagentoModuleGenerator.java create mode 100644 src/com/magento/idea/magento2plugin/generation/MagentoProjectGeneratorSettings.java create mode 100644 src/com/magento/idea/magento2plugin/generation/MagentoProjectPeer.java create mode 100644 src/com/magento/idea/magento2plugin/generation/MagentoTemplatesFactory.java create mode 100644 src/com/magento/idea/magento2plugin/generation/NewModuleForm.form create mode 100644 src/com/magento/idea/magento2plugin/generation/NewModuleForm.java create mode 100644 src/com/magento/idea/magento2plugin/generation/validator/NewModuleFormValidator.java create mode 100644 src/com/magento/idea/magento2plugin/init/ConfigurationManager.java create mode 100644 src/com/magento/idea/magento2plugin/project/util/GetProjectBasePath.java create mode 100644 src/com/magento/idea/magento2plugin/util/magento/MagentoBasePathUtil.java diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index eea79a860..9279ac4ef 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -79,6 +79,9 @@ + + + moduleLicense; private final List moduleDependencies; + private final boolean createModuleDirs; public ModuleComposerJsonData( String packageName, @@ -25,7 +26,8 @@ public ModuleComposerJsonData( String composerPackageName, String moduleVersion, List moduleLicense, - List moduleDependencies + List moduleDependencies, + boolean createModuleDirs ) { this.packageName = packageName; this.moduleName = moduleName; @@ -35,6 +37,7 @@ public ModuleComposerJsonData( this.moduleVersion = moduleVersion; this.moduleLicense = moduleLicense; this.moduleDependencies = moduleDependencies; + this.createModuleDirs = createModuleDirs; } public String getPackageName() { @@ -68,4 +71,7 @@ public List getModuleLicense() { public List getModuleDependencies() { return moduleDependencies; } + + public boolean getCreateModuleDirs() { return this.createModuleDirs; } + } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleRegistrationPhpData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleRegistrationPhpData.java index ae337ddaf..3014ffdfe 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleRegistrationPhpData.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleRegistrationPhpData.java @@ -10,15 +10,18 @@ public class ModuleRegistrationPhpData { private final String packageName; private final String moduleName; private PsiDirectory baseDir; + private boolean createModuleDirs; public ModuleRegistrationPhpData( String packageName, String moduleName, - PsiDirectory baseDir + PsiDirectory baseDir, + boolean createModuleDirs ) { this.packageName = packageName; this.moduleName = moduleName; this.baseDir = baseDir; + this.createModuleDirs = createModuleDirs; } public String getPackageName() { @@ -32,4 +35,6 @@ public String getModuleName() { public PsiDirectory getBaseDir() { return this.baseDir; } + + public boolean getCreateModuleDirs() { return this.createModuleDirs; } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleXmlData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleXmlData.java index dffdd0bd0..236d371e9 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleXmlData.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleXmlData.java @@ -10,15 +10,18 @@ public class ModuleXmlData { private final String packageName; private final String moduleName; private PsiDirectory baseDir; + private boolean createModuleDirs; public ModuleXmlData( String packageName, String moduleName, - PsiDirectory baseDir + PsiDirectory baseDir, + boolean createModuleDirs ) { this.packageName = packageName; this.moduleName = moduleName; this.baseDir = baseDir; + this.createModuleDirs = createModuleDirs; } public String getPackageName() { @@ -32,4 +35,6 @@ public String getModuleName() { public PsiDirectory getBaseDir() { return this.baseDir; } + + public boolean getCreateModuleDirs() { return this.createModuleDirs; } } 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 b231d71c1..226ba3417 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.java @@ -168,7 +168,8 @@ private PsiFile generateComposerJson() { getComposerPackageName(), getModuleVersion(), getModuleLicense(), - getModuleDependencies() + getModuleDependencies(), + true ), project).generate(NewModuleAction.ACTION_NAME); } @@ -176,7 +177,8 @@ private PsiFile generateRegistrationPhp() { return new ModuleRegistrationPhpGenerator(new ModuleRegistrationPhpData( getPackageName(), getModuleName(), - getBaseDir() + getBaseDir(), + true ), project).generate(NewModuleAction.ACTION_NAME); } @@ -184,7 +186,8 @@ private void generateModuleXml() { new ModuleXmlGenerator(new ModuleXmlData( getPackageName(), getModuleName(), - getBaseDir() + getBaseDir(), + true ), project).generate(NewModuleAction.ACTION_NAME, true); } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleComposerJsonGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleComposerJsonGenerator.java index 051a11bcd..a16056051 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleComposerJsonGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleComposerJsonGenerator.java @@ -5,7 +5,6 @@ package com.magento.idea.magento2plugin.actions.generation.generator; import com.google.gson.JsonElement; -import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; @@ -18,10 +17,8 @@ import com.magento.idea.magento2plugin.magento.files.ComposerJson; import com.magento.idea.magento2plugin.util.CamelCaseToHyphen; import org.jetbrains.annotations.NotNull; - import java.io.FileNotFoundException; import java.io.FileReader; -import java.util.ArrayList; import java.util.Properties; import java.util.List; @@ -45,8 +42,12 @@ public ModuleComposerJsonGenerator(@NotNull ModuleComposerJsonData moduleCompose } public PsiFile generate(String actionName) { - ModuleDirectoriesData moduleDirectoriesData = directoryGenerator.createOrFindModuleDirectories(moduleComposerJsonData.getPackageName(), moduleComposerJsonData.getModuleName(), moduleComposerJsonData.getBaseDir()); - return fileFromTemplateGenerator.generate(ComposerJson.getInstance(), getAttributes(), moduleDirectoriesData.getModuleDirectory(), actionName); + if (moduleComposerJsonData.getCreateModuleDirs()) { + ModuleDirectoriesData moduleDirectoriesData = directoryGenerator.createOrFindModuleDirectories(moduleComposerJsonData.getPackageName(), moduleComposerJsonData.getModuleName(), moduleComposerJsonData.getBaseDir()); + return fileFromTemplateGenerator.generate(ComposerJson.getInstance(), getAttributes(), moduleDirectoriesData.getModuleDirectory(), actionName); + } else { + return fileFromTemplateGenerator.generate(ComposerJson.getInstance(), getAttributes(), moduleComposerJsonData.getBaseDir(), actionName); + } } protected void fillAttributes(Properties attributes) { @@ -81,6 +82,12 @@ protected String getLicensesString(List licensesList) { private String getDependenciesString(List dependenciesList) { String result = ""; Object[] dependencies = dependenciesList.toArray(); + result = result.concat(ComposerJson.DEFAULT_DEPENDENCY); + if (dependencies.length == 0) { + result = result.concat("\n"); + } else { + result = result.concat(",\n"); + } for (int i = 0; i < dependencies.length; i++) { String dependency = dependencies[i].toString(); @@ -105,7 +112,7 @@ private String getDependencyVersion(String dependency) { String version = "*"; try { VirtualFile virtualFile = moduleIndex.getModuleDirectoryByModuleName(dependency) - .findFile("composer.json") + .findFile(ComposerJson.FILE_NAME) .getVirtualFile(); if (virtualFile.exists()) { JsonElement jsonElement = new JsonParser().parse(new FileReader(virtualFile.getPath())); diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleRegistrationPhpGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleRegistrationPhpGenerator.java index 18fa5cc90..c2173ce5a 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleRegistrationPhpGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleRegistrationPhpGenerator.java @@ -28,8 +28,12 @@ public ModuleRegistrationPhpGenerator(@NotNull ModuleRegistrationPhpData moduleR } public PsiFile generate(String actionName) { - ModuleDirectoriesData moduleDirectoriesData = directoryGenerator.createOrFindModuleDirectories(moduleRegistrationPhpData.getPackageName(), moduleRegistrationPhpData.getModuleName(), moduleRegistrationPhpData.getBaseDir()); - return fileFromTemplateGenerator.generate(RegistrationPhp.getInstance(), getAttributes(), moduleDirectoriesData.getModuleDirectory(), actionName); + if (moduleRegistrationPhpData.getCreateModuleDirs()) { + ModuleDirectoriesData moduleDirectoriesData = directoryGenerator.createOrFindModuleDirectories(moduleRegistrationPhpData.getPackageName(), moduleRegistrationPhpData.getModuleName(), moduleRegistrationPhpData.getBaseDir()); + return fileFromTemplateGenerator.generate(RegistrationPhp.getInstance(), getAttributes(), moduleDirectoriesData.getModuleDirectory(), actionName); + } else { + return fileFromTemplateGenerator.generate(RegistrationPhp.getInstance(), getAttributes(), moduleRegistrationPhpData.getBaseDir(), actionName); + } } protected void fillAttributes(Properties attributes) { diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleXmlGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleXmlGenerator.java index 136e927af..2a591ba11 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleXmlGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleXmlGenerator.java @@ -5,15 +5,15 @@ 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.ModuleXmlData; 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.ModuleXml; +import com.magento.idea.magento2plugin.magento.packages.Package; import org.jetbrains.annotations.NotNull; - -import java.util.List; import java.util.Properties; public class ModuleXmlGenerator extends FileGenerator { @@ -31,8 +31,13 @@ public ModuleXmlGenerator(@NotNull ModuleXmlData moduleXmlData, Project project) @Override public PsiFile generate(String actionName) { - ModuleDirectoriesData moduleDirectoriesData = directoryGenerator.createOrFindModuleDirectories(moduleXmlData.getPackageName(), moduleXmlData.getModuleName(), moduleXmlData.getBaseDir()); - return fileFromTemplateGenerator.generate(ModuleXml.getInstance(), getAttributes(), moduleDirectoriesData.getModuleEtcDirectory(), actionName); + if (moduleXmlData.getCreateModuleDirs()) { + ModuleDirectoriesData moduleDirectoriesData = directoryGenerator.createOrFindModuleDirectories(moduleXmlData.getPackageName(), moduleXmlData.getModuleName(), moduleXmlData.getBaseDir()); + return fileFromTemplateGenerator.generate(ModuleXml.getInstance(), getAttributes(), moduleDirectoriesData.getModuleEtcDirectory(), actionName); + } else { + PsiDirectory etcDirectory = directoryGenerator.findOrCreateSubdirectory(moduleXmlData.getBaseDir(), Package.MODULE_BASE_AREA_DIR); + return fileFromTemplateGenerator.generate(ModuleXml.getInstance(), getAttributes(), etcDirectory, actionName); + } } protected void fillAttributes(Properties attributes) { diff --git a/src/com/magento/idea/magento2plugin/actions/generation/util/IsClickedDirectoryInsideProject.java b/src/com/magento/idea/magento2plugin/actions/generation/util/IsClickedDirectoryInsideProject.java new file mode 100644 index 000000000..ffe1f13a1 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/util/IsClickedDirectoryInsideProject.java @@ -0,0 +1,27 @@ +/** + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +package com.magento.idea.magento2plugin.actions.generation.util; + +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VfsUtilCore; +import com.intellij.psi.PsiDirectory; +import com.magento.idea.magento2plugin.project.util.GetProjectBasePath; +import org.jetbrains.annotations.NotNull; + +public class IsClickedDirectoryInsideProject { + + private static IsClickedDirectoryInsideProject INSTANCE = null; + + public static IsClickedDirectoryInsideProject getInstance() { + if (null == INSTANCE) { + INSTANCE = new IsClickedDirectoryInsideProject(); + } + return INSTANCE; + } + + public boolean execute(@NotNull Project project, PsiDirectory directory) { + return VfsUtilCore.isAncestor(GetProjectBasePath.execute(project), directory.getVirtualFile(), false); + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/groups/NewModuleFileGroup.java b/src/com/magento/idea/magento2plugin/actions/groups/NewModuleFileGroup.java index b382b4b98..23c553f0c 100644 --- a/src/com/magento/idea/magento2plugin/actions/groups/NewModuleFileGroup.java +++ b/src/com/magento/idea/magento2plugin/actions/groups/NewModuleFileGroup.java @@ -13,6 +13,7 @@ import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiElement; import com.magento.idea.magento2plugin.MagentoIcons; +import com.magento.idea.magento2plugin.actions.generation.util.IsClickedDirectoryInsideProject; import com.magento.idea.magento2plugin.project.Settings; import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectory; import org.jetbrains.annotations.NotNull; @@ -35,6 +36,11 @@ public void update(AnActionEvent event) { return; } + if(!IsClickedDirectoryInsideProject.getInstance().execute(project, (PsiDirectory) psiElement)) { + event.getPresentation().setVisible(false); + return; + } + GetModuleNameByDirectory getModuleName = GetModuleNameByDirectory.getInstance(project); String moduleName = getModuleName.execute((PsiDirectory) psiElement); if (Settings.isEnabled(project) && moduleName != null) { diff --git a/src/com/magento/idea/magento2plugin/generation/MagentoModuleGenerator.java b/src/com/magento/idea/magento2plugin/generation/MagentoModuleGenerator.java new file mode 100644 index 000000000..2cccf0c37 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/generation/MagentoModuleGenerator.java @@ -0,0 +1,121 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +package com.magento.idea.magento2plugin.generation; + +import com.intellij.ide.util.projectWizard.WebProjectTemplate; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.startup.StartupManager; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.platform.ProjectGeneratorPeer; +import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiManager; +import com.intellij.util.PlatformUtils; +import javax.swing.Icon; +import com.magento.idea.magento2plugin.MagentoIcons; +import com.magento.idea.magento2plugin.actions.generation.data.ModuleComposerJsonData; +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.generator.ModuleComposerJsonGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.ModuleRegistrationPhpGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.ModuleXmlGenerator; +import com.magento.idea.magento2plugin.init.ConfigurationManager; +import com.magento.idea.magento2plugin.project.Settings; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; + +public class MagentoModuleGenerator extends WebProjectTemplate { + public static String ACTION_NAME = "Magento 2 Module"; + + public MagentoModuleGenerator() { + } + + @Nls + @NotNull + public String getName() { + return ACTION_NAME; + } + + public String getDescription() { + return "Create a Magento 2 Module"; + } + + public Icon getIcon() { + return MagentoIcons.MODULE; + } + + @NotNull + public ProjectGeneratorPeer createPeer() { + return new MagentoProjectPeer(); + } + + public boolean isPrimaryGenerator() { + return PlatformUtils.isPhpStorm(); + } + + public void generateProject(@NotNull Project project, @NotNull VirtualFile baseDir, @NotNull MagentoProjectGeneratorSettings settings, @NotNull Module module) { + Settings dataService = Settings.getInstance(project); + dataService.setState(settings.getMagentoState()); + + Runnable generate = () -> { + ApplicationManager.getApplication().runWriteAction(() -> { + PsiDirectory baseDirElement = PsiManager.getInstance(project).findDirectory(baseDir); + if (baseDirElement == null) { + return; + } + + generateComposerJson(project, baseDirElement, settings); + generateRegistrationPhp(project, baseDirElement, settings); + generateModuleXml(project, baseDirElement, settings); + ConfigurationManager.getInstance().refreshIncludePaths(dataService.getState(), project); + }); + }; + StartupManager.getInstance(project).runWhenProjectIsInitialized(generate); + } + + private PsiFile generateComposerJson(@NotNull Project project, @NotNull PsiDirectory baseDir, @NotNull MagentoProjectGeneratorSettings settings) { + return new ModuleComposerJsonGenerator(new ModuleComposerJsonData( + settings.getPackageName(), + settings.getModuleName(), + baseDir, + settings.getModuleDescription(), + settings.getComposerPackageName(), + settings.getModuleVersion(), + settings.getModuleLicenses(), + new ArrayList<>(), + false + ), project).generate(ACTION_NAME); + } + + private PsiFile generateRegistrationPhp(@NotNull Project project, @NotNull PsiDirectory baseDir, @NotNull MagentoProjectGeneratorSettings settings) { + return new ModuleRegistrationPhpGenerator(new ModuleRegistrationPhpData( + settings.getPackageName(), + settings.getModuleName(), + baseDir, + false + ), project).generate(ACTION_NAME); + } + + private void generateModuleXml(@NotNull Project project, @NotNull PsiDirectory baseDir, @NotNull MagentoProjectGeneratorSettings settings) { + ModuleXmlData moduleXmlData = new ModuleXmlData( + settings.getPackageName(), + settings.getModuleName(), + baseDir, + false + ); + ModuleXmlGenerator moduleXmlGenerator = new ModuleXmlGenerator(moduleXmlData, project); + moduleXmlGenerator.generate(ACTION_NAME, true); + } + + @Nullable + public String getHelpId() { + return null; + } +} diff --git a/src/com/magento/idea/magento2plugin/generation/MagentoProjectGeneratorSettings.java b/src/com/magento/idea/magento2plugin/generation/MagentoProjectGeneratorSettings.java new file mode 100644 index 000000000..33b6ede2a --- /dev/null +++ b/src/com/magento/idea/magento2plugin/generation/MagentoProjectGeneratorSettings.java @@ -0,0 +1,65 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +package com.magento.idea.magento2plugin.generation; + +import com.magento.idea.magento2plugin.project.Settings; + +import java.util.List; + +public class MagentoProjectGeneratorSettings { + private final Settings.State myMagentoState; + private final String packageName; + private final String moduleName; + private final String moduleDescription; + private final String composerPackageName; + private final String moduleVersion; + private final List moduleLicenses; + + public MagentoProjectGeneratorSettings( + Settings.State state, + String packageName, + String moduleName, + String moduleDescription, + String composerPackageName, + String moduleVersion, + List moduleLicenses + ) { + this.myMagentoState = state; + this.packageName = packageName; + this.moduleName = moduleName; + this.moduleDescription = moduleDescription; + this.composerPackageName = composerPackageName; + this.moduleVersion = moduleVersion; + this.moduleLicenses = moduleLicenses; + } + + public String getPackageName() { + return packageName; + } + + public String getModuleName() { + return moduleName; + } + + public String getModuleDescription() { + return moduleDescription; + } + + public String getComposerPackageName() { + return composerPackageName; + } + + public String getModuleVersion() { + return moduleVersion; + } + + public List getModuleLicenses() { + return moduleLicenses; + } + + public Settings.State getMagentoState() { + return this.myMagentoState; + } +} diff --git a/src/com/magento/idea/magento2plugin/generation/MagentoProjectPeer.java b/src/com/magento/idea/magento2plugin/generation/MagentoProjectPeer.java new file mode 100644 index 000000000..2f3a731ac --- /dev/null +++ b/src/com/magento/idea/magento2plugin/generation/MagentoProjectPeer.java @@ -0,0 +1,52 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +package com.magento.idea.magento2plugin.generation; + +import com.intellij.ide.util.projectWizard.SettingsStep; +import com.intellij.openapi.ui.ValidationInfo; +import com.intellij.platform.ProjectGeneratorPeer; +import com.intellij.platform.WebProjectGenerator; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import javax.swing.JComponent; + +public class MagentoProjectPeer implements ProjectGeneratorPeer { + private final NewModuleForm myForm = new NewModuleForm(); + + public MagentoProjectPeer() { + } + + @NotNull + public JComponent getComponent() { + return this.myForm.getContentPane(); + } + + public void buildUI(@NotNull SettingsStep settingsStep) { + settingsStep.addSettingsComponent(this.myForm.getContentPane()); + } + + @NotNull + public MagentoProjectGeneratorSettings getSettings() { + return this.myForm.getSettings(); + } + + @Nullable + public ValidationInfo validate() { + return this.myForm.validate(); + } + + public boolean isBackgroundJobRunning() { + return false; + } + + @Override + public void addSettingsListener(@NotNull SettingsListener listener) { + this.myForm.addSettingsStateListener(listener); + } + + @Override + public void addSettingsStateListener(@NotNull WebProjectGenerator.SettingsStateListener listener) { + } +} diff --git a/src/com/magento/idea/magento2plugin/generation/MagentoTemplatesFactory.java b/src/com/magento/idea/magento2plugin/generation/MagentoTemplatesFactory.java new file mode 100644 index 000000000..c22a74292 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/generation/MagentoTemplatesFactory.java @@ -0,0 +1,32 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +package com.magento.idea.magento2plugin.generation; + +import com.intellij.ide.util.projectWizard.WizardContext; +import com.intellij.platform.ProjectTemplate; +import com.intellij.platform.ProjectTemplatesFactory; +import icons.PhpIcons; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import javax.swing.Icon; + +public class MagentoTemplatesFactory extends ProjectTemplatesFactory { + public MagentoTemplatesFactory() { + } + + @NotNull + public String[] getGroups() { + return new String[]{"PHP"}; + } + + public Icon getGroupIcon(String group) { + return PhpIcons.Php_icon; + } + + @NotNull + public ProjectTemplate[] createTemplates(@Nullable String group, WizardContext context) { + return new ProjectTemplate[]{new MagentoModuleGenerator()}; + } +} diff --git a/src/com/magento/idea/magento2plugin/generation/NewModuleForm.form b/src/com/magento/idea/magento2plugin/generation/NewModuleForm.form new file mode 100644 index 000000000..611245000 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/generation/NewModuleForm.form @@ -0,0 +1,161 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/com/magento/idea/magento2plugin/generation/NewModuleForm.java b/src/com/magento/idea/magento2plugin/generation/NewModuleForm.java new file mode 100644 index 000000000..30b72d462 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/generation/NewModuleForm.java @@ -0,0 +1,221 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +package com.magento.idea.magento2plugin.generation; + +import com.intellij.openapi.fileChooser.FileChooserDescriptor; +import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory; +import com.intellij.openapi.ui.ComponentWithBrowseButton; +import com.intellij.openapi.ui.TextComponentAccessor; +import com.intellij.openapi.ui.TextFieldWithBrowseButton; +import com.intellij.openapi.ui.ValidationInfo; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.platform.ProjectGeneratorPeer; +import com.intellij.ui.DocumentAdapter; +import com.magento.idea.magento2plugin.generation.validator.NewModuleFormValidator; +import com.magento.idea.magento2plugin.magento.packages.Package; +import com.magento.idea.magento2plugin.project.Settings; +import com.magento.idea.magento2plugin.util.CamelCaseToHyphen; +import org.jetbrains.annotations.NotNull; +import javax.swing.*; +import javax.swing.event.DocumentEvent; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import java.io.File; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Vector; + +public class NewModuleForm implements ListSelectionListener { + private NewModuleFormValidator newModuleFormValidator; + private JPanel contentPane; + private JTextField packageName; + private JLabel packageNameLabel; + private JTextField moduleName; + private JLabel moduleNameLabel; + private JTextArea moduleDescription; + private JLabel moduleDescriptionLabel; + private JTextField moduleVersion; + private JLabel moduleVersionLabel; + private JList moduleLicense; + private JLabel moduleLicenseLabel; + private JTextField moduleLicenseCustom; + private JScrollPane moduleLicenseScrollPanel; + private JLabel magentoPathLabel; + private TextFieldWithBrowseButton magentoPath; + private List myStateListeners; + private CamelCaseToHyphen camelCaseToHyphen; + + public NewModuleForm( + ) { + this.camelCaseToHyphen = CamelCaseToHyphen.getInstance(); + this.newModuleFormValidator = NewModuleFormValidator.getInstance(this); + this.myStateListeners = new ArrayList(); + Runnable listener = () -> { + this.fireStateChanged(); + }; + addPathListener(); + addComponentChangesListener(listener); + setLicenses(); + } + + private void addPathListener() { + FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor(); + ComponentWithBrowseButton.BrowseFolderActionListener browseFolderListener = new ComponentWithBrowseButton.BrowseFolderActionListener( + "Magento Root Directory", + "Choose Magento root directory", + this.magentoPath, + null, + descriptor, + TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT + ) { + protected VirtualFile getInitialFile() { + String directoryName = this.getComponentText(); + if (!StringUtil.isEmptyOrSpaces(directoryName)) { + String lastSavedPath = Settings.getLastMagentoPath(); + if (!StringUtil.isEmptyOrSpaces(lastSavedPath)) { + lastSavedPath = FileUtil.toSystemIndependentName(lastSavedPath); + return LocalFileSystem.getInstance().findFileByPath(lastSavedPath); + } + } + + return super.getInitialFile(); + } + }; + this.magentoPath.addActionListener(browseFolderListener); + } + + public JComponent getContentPane() { + return this.contentPane; + } + + public MagentoProjectGeneratorSettings getSettings() { + Settings.State state = new Settings.State(); + state.setPluginEnabled(true); + state.setMftfSupportEnabled(true); + state.setDefaultLicenseName(Settings.DEFAULT_LICENSE); + state.setMagentoPathAndUpdateLastUsed(this.magentoPath.getTextField().getText().trim()); + + return new MagentoProjectGeneratorSettings( + state, + getPackageName(), + getModuleName(), + getModuleDescription(), + getComposerPackageName(), + getModuleVersion(), + getModuleLicenses() + ); + } + + public String getPackageName() { + return this.packageName.getText().trim(); + } + + public String getModuleName() { + return this.moduleName.getText().trim(); + } + + public String getModuleDescription() { + return this.moduleDescription.getText().trim(); + } + + public String getModuleVersion() { + return this.moduleVersion.getText().trim(); + } + + public String getMagentoPath() { + return this.magentoPath.getTextField().getText().trim(); + } + + public ValidationInfo validate() { + String message = newModuleFormValidator.validate(); + if (message == null) { + return null; + } + + return new ValidationInfo(message, getContentPane()); + } + + public List getModuleLicenses() { + List selectedLicenses = this.moduleLicense.getSelectedValuesList(); + Package.License customLicense = Package.License.CUSTOM; + + if (selectedLicenses.contains(customLicense.getLicenseName())) { + selectedLicenses.remove(customLicense.getLicenseName()); + selectedLicenses.add(moduleLicenseCustom.getText()); + } + + return selectedLicenses; + } + + public void addSettingsStateListener(ProjectGeneratorPeer.SettingsListener listener) { + this.myStateListeners.add(listener); + } + + public void addComponentChangesListener(final Runnable listener) { + this.magentoPath.getTextField().getDocument().addDocumentListener(new DocumentAdapter() { + protected void textChanged(@NotNull DocumentEvent e) { + listener.run(); + } + }); + } + + private void fireStateChanged() { + boolean validSettings = this.validate() == null; + Iterator iterator = this.myStateListeners.iterator(); + + while(iterator.hasNext()) { + ProjectGeneratorPeer.SettingsListener listener = (ProjectGeneratorPeer.SettingsListener)iterator.next(); + listener.stateChanged(validSettings); + } + } + + private void setLicenses() { + Package.License[] licenses = Package.License.values(); + Vector licenseNames = new Vector<>(licenses.length); + + for (Package.License license: licenses) { + licenseNames.add(license.getLicenseName()); + } + + moduleLicense.setListData(licenseNames); + moduleLicense.setSelectedIndex(0); + moduleLicense.addListSelectionListener(this); + } + + private void handleModuleCustomLicenseInputVisibility () { + boolean isCustomLicenseSelected = false; + + for (Object value: moduleLicense.getSelectedValuesList()) { + if (Package.License.CUSTOM.getLicenseName().equals(value.toString())) { + isCustomLicenseSelected = true; + + break; + } + } + + moduleLicenseCustom.setEnabled(isCustomLicenseSelected); + moduleLicenseCustom.setEditable(isCustomLicenseSelected); + } + + @NotNull + private String getComposerPackageName() { + return camelCaseToHyphen.convert(getPackageName()) + .concat(File.separator) + .concat(camelCaseToHyphen.convert(getModuleName())); + } + + private void createUIComponents() { + this.magentoPath = new TextFieldWithBrowseButton(); + } + + @Override + public void valueChanged(ListSelectionEvent listSelectionEvent) { + handleModuleCustomLicenseInputVisibility(); + } +} diff --git a/src/com/magento/idea/magento2plugin/generation/validator/NewModuleFormValidator.java b/src/com/magento/idea/magento2plugin/generation/validator/NewModuleFormValidator.java new file mode 100644 index 000000000..87b64687a --- /dev/null +++ b/src/com/magento/idea/magento2plugin/generation/validator/NewModuleFormValidator.java @@ -0,0 +1,65 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +package com.magento.idea.magento2plugin.generation.validator; + +import com.magento.idea.magento2plugin.generation.NewModuleForm; +import com.magento.idea.magento2plugin.util.Regex; +import com.magento.idea.magento2plugin.util.magento.MagentoBasePathUtil; + +public class NewModuleFormValidator { + private static NewModuleFormValidator INSTANCE = null; + private NewModuleForm form; + + public static NewModuleFormValidator getInstance(NewModuleForm form) { + if (null == INSTANCE) { + INSTANCE = new NewModuleFormValidator(); + } + INSTANCE.form = form; + return INSTANCE; + } + + public String validate() + { + String packageName = form.getPackageName(); + if (packageName.length() == 0) { + return "Package Name must not be empty."; + } + + if (!packageName.matches(Regex.ALPHANUMERIC)) { + return "Package Name must contain letters and numbers only."; + } + + if (!Character.isUpperCase(packageName.charAt(0)) && !Character.isDigit(packageName.charAt(0))) { + return "Package Name must start from a number or a capital letter"; + } + + String moduleName = form.getModuleName(); + if (moduleName.length() == 0) { + return "Module Name must not be empty."; + } + + if (!moduleName.matches(Regex.ALPHANUMERIC)) { + return "Module Name must contain letters and numbers only."; + } + + if (!Character.isUpperCase(moduleName.charAt(0)) && !Character.isDigit(moduleName.charAt(0))) { + return "Module Name must start from a number or a capital letter"; + } + + if (form.getModuleVersion().length() == 0) { + return "Module Version must not be empty."; + } + + if (form.getModuleDescription().length() == 0) { + return "Module Version must not be empty."; + } + + if (!MagentoBasePathUtil.isMagentoFolderValid(form.getMagentoPath())) { + return "Please specify valid magento installation path!"; + } + + return null; + } +} diff --git a/src/com/magento/idea/magento2plugin/indexes/ModuleIndex.java b/src/com/magento/idea/magento2plugin/indexes/ModuleIndex.java index aa6bda837..12170c584 100644 --- a/src/com/magento/idea/magento2plugin/indexes/ModuleIndex.java +++ b/src/com/magento/idea/magento2plugin/indexes/ModuleIndex.java @@ -5,6 +5,7 @@ package com.magento.idea.magento2plugin.indexes; import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiManager; @@ -12,6 +13,7 @@ import com.intellij.util.indexing.FileBasedIndex; import com.jetbrains.php.lang.PhpFileType; import com.magento.idea.magento2plugin.magento.packages.Package; +import com.magento.idea.magento2plugin.project.util.GetProjectBasePath; import com.magento.idea.magento2plugin.stubs.indexes.ModuleNameIndex; import com.magento.idea.magento2plugin.util.RegExUtil; import java.util.ArrayList; @@ -40,14 +42,14 @@ public static ModuleIndex getInstance(Project project) { } public List getEditableModuleNames() { - return getModuleNames(Package.VENDOR); + return getModuleNames(Package.VENDOR, true); } public List getModuleNames() { - return getModuleNames("/tests/|/test/"); + return getModuleNames("/tests/|/test/", false); } - public List getModuleNames(String filterPattern) { + public List getModuleNames(String filterPattern, boolean withinProject) { FileBasedIndex index = FileBasedIndex .getInstance(); List allModulesList = new ArrayList<>(); @@ -65,6 +67,11 @@ public List getModuleNames(String filterPattern) { continue; } VirtualFile virtualFile = files.iterator().next(); + if (withinProject) { + if (!VfsUtilCore.isAncestor(GetProjectBasePath.execute(project), virtualFile, false)) { + continue; + } + } Matcher m = p.matcher(virtualFile.getPath()); if (m.find()) { continue; diff --git a/src/com/magento/idea/magento2plugin/init/ConfigurationManager.java b/src/com/magento/idea/magento2plugin/init/ConfigurationManager.java new file mode 100644 index 000000000..adc9142e3 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/init/ConfigurationManager.java @@ -0,0 +1,247 @@ +/** + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +package com.magento.idea.magento2plugin.init; + +import com.intellij.notification.Notification; +import com.intellij.notification.NotificationType; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.application.ModalityState; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.module.ModuleManager; +import com.intellij.openapi.options.ShowSettingsUtil; +import com.intellij.openapi.project.DumbAwareAction; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.ContentEntry; +import com.intellij.openapi.roots.ModuleRootManager; +import com.intellij.openapi.roots.ModuleRootModificationUtil; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VfsUtilCore; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.util.PlatformUtils; +import com.jetbrains.php.config.PhpProjectConfigurable; +import com.jetbrains.php.config.library.PhpIncludePathManager; +import com.jetbrains.php.ui.PhpUiUtil; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.function.Function; +import com.magento.idea.magento2plugin.project.Settings; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class ConfigurationManager { + private static final ConfigurationManager INSTANCE = new ConfigurationManager(); + + public static ConfigurationManager getInstance() { + return INSTANCE; + } + + private ConfigurationManager() { + } + + public void refreshIncludePaths(Settings.State newState, Project project) { + if (!project.isDefault() && newState.isPluginEnabled() && !newState.isDoNotAskContentConfigAgain()) { + VirtualFile magentoFile = getMagentoFile(newState); + if (magentoFile == null) { + return; + } + + if (VfsUtilCore.isAncestor(project.getBaseDir(), magentoFile, false)) { + return; + } + + boolean isModuleInsideMagento = VfsUtilCore.isAncestor(magentoFile, project.getBaseDir(), false); + if (isModuleInsideMagento) { + Module[] modules = ModuleManager.getInstance(project).getModules(); + if (modules.length == 1) { + Module module = modules[0]; + boolean isMagentoIncluded = isFileInsideModule(magentoFile, module); + if (!isMagentoIncluded) { + suggestToChangeContentRoots(magentoFile, project, module); + } + } + } else { + boolean isMagentoIncluded = isInIncludePath(magentoFile, project); + if (!isMagentoIncluded) { + suggestToAddToIncludePath(magentoFile, project); + } + } + } + + } + + private static void suggestToChangeContentRoots(@NotNull VirtualFile magentoFile, @NotNull Project project, @NotNull Module module) { + String message = "For Magento 2 containing plugins inside it's better to add whole Magento 2 to project."; + Function fixAction = (notification) -> { + return new DumbAwareAction("Fix") { + public void actionPerformed(@NotNull AnActionEvent e) { + notification.expire(); + ModuleRootModificationUtil.updateModel(module, (model) -> { + ContentEntry[] contentEntries = model.getContentEntries(); + ContentEntry rootEntry = null; + ContentEntry[] entries = contentEntries; + int length = contentEntries.length; + + for(int i = 0; i < length; ++i) { + ContentEntry entry = entries[i]; + VirtualFile entryFile = entry.getFile(); + if (entryFile != null && VfsUtilCore.isAncestor(entryFile, project.getBaseDir(), false)) { + rootEntry = entry; + break; + } + } + + if (rootEntry != null) { + VirtualFile rootEntryFile = rootEntry.getFile(); + if (rootEntryFile == null || !VfsUtilCore.isAncestor(rootEntryFile, magentoFile, false)) { + model.addContentEntry(magentoFile); + model.removeContentEntry(rootEntry); + } + } else { + model.addContentEntry(magentoFile); + } + + }); + if (PlatformUtils.isPhpStorm()) { + Runnable runnable = () -> { + ShowSettingsUtil.getInstance().showSettingsDialog(project, "Directories"); + }; + ApplicationManager.getApplication().invokeLater(runnable, ModalityState.NON_MODAL); + } + } + }; + }; + Function ignoreAction = (notification) -> { + return new DumbAwareAction("Ignore") { + public void actionPerformed(@NotNull AnActionEvent e) { + notification.expire(); + Settings.getInstance(project).setDoNotAskContentConfigurationAgain(true); + } + }; + }; + showPopup(project, message, fixAction, ignoreAction); + } + + private static void suggestToAddToIncludePath(@NotNull VirtualFile magentoFile, @NotNull Project project) { + String message = "Magento installation is not added to PHP | Include path"; + Function fixAction = (notification) -> { + return new DumbAwareAction("Fix") { + public void actionPerformed(@NotNull AnActionEvent e) { + notification.expire(); + Runnable runnable = () -> { + PhpIncludePathManager facade = PhpIncludePathManager.getInstance(project); + List includePaths = facade.getIncludePath(); + List newIncludePaths = new ArrayList(includePaths.size() + 1); + newIncludePaths.addAll(includePaths); + String path = magentoFile.getPath(); + newIncludePaths.add(path); + facade.setIncludePath(newIncludePaths); + ApplicationManager.getApplication().invokeLater(() -> { + if (!project.isDisposed()) { + PhpUiUtil.editConfigurable(project, new PhpProjectConfigurable(project)); + } + }); + }; + ApplicationManager.getApplication().runWriteAction(runnable); + } + }; + }; + Function ignoreAction = (notification) -> { + return new DumbAwareAction("Ignore") { + public void actionPerformed(@NotNull AnActionEvent e) { + notification.expire(); + Settings.getInstance(project).setDoNotAskContentConfigurationAgain(true); + } + }; + }; + Function showSettingsAction = (notification) -> { + return new DumbAwareAction("Show settings") { + public void actionPerformed(@NotNull AnActionEvent e) { + notification.expire(); + PhpUiUtil.editConfigurable(project, new PhpProjectConfigurable(project)); + } + }; + }; + showPopup(project, message, fixAction, ignoreAction, showSettingsAction); + } + + public static void suggestToConfigureMagentoPath(@NotNull Project project) { + String message = "Magento 2 support is disabled. Please configure Magento 2 installation path."; + Function showSettingsAction = (notification) -> { + return new DumbAwareAction("Show settings") { + public void actionPerformed(@NotNull AnActionEvent e) { + notification.expire(); + ShowSettingsUtil.getInstance().showSettingsDialog(project, "Magento2.SettingsForm"); + } + }; + }; + showPopup(project, message, showSettingsAction); + } + + private static boolean isInIncludePath(@NotNull VirtualFile fileToCheck, @NotNull Project project) { + List includePaths = PhpIncludePathManager.getInstance(project).getRoots(); + Iterator iterator = includePaths.iterator(); + + VirtualFile file; + do { + if (!iterator.hasNext()) { + return false; + } + + file = (VirtualFile)iterator.next(); + } while(file == null || !VfsUtilCore.isAncestor(file, fileToCheck, false)); + + return true; + } + + @Nullable + private static VirtualFile getMagentoFile(Settings.State state) { + String path = state.getMagentoPath(); + if (StringUtil.isEmpty(path)) { + return null; + } else { + VirtualFile file = LocalFileSystem.getInstance().findFileByPath(path); + return file != null && file.isDirectory() ? file : null; + } + } + + private static boolean isFileInsideModule(@NotNull VirtualFile magentoFile, @NotNull Module module) { + VirtualFile[] contentRoots = ModuleRootManager.getInstance(module).getContentRoots(); + VirtualFile[] roots = contentRoots; + int length = contentRoots.length; + + for(int i = 0; i < length; ++i) { + VirtualFile root = roots[i]; + if (VfsUtilCore.isAncestor(root, magentoFile, false)) { + return true; + } + } + + return false; + } + + private static void showPopup(Project project, String message, Function... actions) { + Runnable runnable = () -> { + notifyGlobally(project, "Magento 2 Support", message, NotificationType.INFORMATION, actions); + }; + ApplicationManager.getApplication().invokeLater(runnable, ModalityState.NON_MODAL); + } + + public static void notifyGlobally(@Nullable Project project, String title, String message, NotificationType notificationType, Function... actions) { + Notification notification = new Notification("Magento 2 Support", title, message, notificationType); + Function[] functions = actions; + int length = actions.length; + + for(int i = 0; i < length; ++i) { + Function generator = functions[i]; + notification.addAction(generator.apply(notification)); + } + + notification.notify(project); + } +} diff --git a/src/com/magento/idea/magento2plugin/magento/files/ComposerJson.java b/src/com/magento/idea/magento2plugin/magento/files/ComposerJson.java index 98b05a265..195e6068b 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/ComposerJson.java +++ b/src/com/magento/idea/magento2plugin/magento/files/ComposerJson.java @@ -10,6 +10,7 @@ public class ComposerJson implements ModuleFileInterface { public static String FILE_NAME = "composer.json"; public static String TEMPLATE = "Magento Module Composer"; + public static String DEFAULT_DEPENDENCY = "\"magento/framework\": \"*\""; private static ComposerJson INSTANCE = null; public static ComposerJson getInstance() { diff --git a/src/com/magento/idea/magento2plugin/magento/packages/Package.java b/src/com/magento/idea/magento2plugin/magento/packages/Package.java index e8d38873a..7cdd8c032 100644 --- a/src/com/magento/idea/magento2plugin/magento/packages/Package.java +++ b/src/com/magento/idea/magento2plugin/magento/packages/Package.java @@ -6,6 +6,7 @@ public class Package { public static String PACKAGES_ROOT = "app/code"; + public static String APP = "app"; public static String VENDOR = "vendor"; public static String MODULE_BASE_AREA_DIR = "etc"; public static String VENDOR_MODULE_NAME_SEPARATOR = "_"; diff --git a/src/com/magento/idea/magento2plugin/project/ProjectDetector.java b/src/com/magento/idea/magento2plugin/project/ProjectDetector.java index 586a9e608..beef9810c 100644 --- a/src/com/magento/idea/magento2plugin/project/ProjectDetector.java +++ b/src/com/magento/idea/magento2plugin/project/ProjectDetector.java @@ -15,24 +15,18 @@ import com.intellij.openapi.util.Ref; import com.intellij.openapi.vfs.*; import com.intellij.platform.DirectoryProjectConfigurator; -import com.intellij.psi.PsiFile; -import com.intellij.psi.PsiFileSystemItem; -import com.intellij.psi.search.FilenameIndex; -import com.intellij.psi.search.GlobalSearchScope; -import com.intellij.util.indexing.FileBasedIndex; import com.magento.idea.magento2plugin.indexes.IndexManager; import com.magento.idea.magento2plugin.php.module.MagentoComponentManager; +import com.magento.idea.magento2plugin.util.magento.MagentoBasePathUtil; import org.jetbrains.annotations.NotNull; - import javax.swing.event.HyperlinkEvent; - public class ProjectDetector implements DirectoryProjectConfigurator { @Override public void configureProject(@NotNull Project project, @NotNull VirtualFile baseDir, @NotNull Ref moduleRef, boolean newProject) { StartupManager.getInstance(project).runWhenProjectIsInitialized(() -> { DumbService.getInstance(project).smartInvokeLater(() -> { - if (null == VfsUtil.findRelativeFile(project.getBaseDir(), "app", "etc", "di.xml")) { + if (!MagentoBasePathUtil.isMagentoFolderValid(baseDir.getPath())) { return; } Notification notification = new Notification("Magento", "Magento", @@ -40,13 +34,15 @@ public void configureProject(@NotNull Project project, @NotNull VirtualFile base NotificationType.INFORMATION, new NotificationListener.Adapter() { @Override public void hyperlinkActivated(@NotNull Notification notification, @NotNull HyperlinkEvent event) { - Settings.getInstance(project).pluginEnabled = true; + Settings settings = Settings.getInstance(project); + settings.pluginEnabled = true; + settings.mftfSupportEnabled = true; + settings.magentoPath = project.getBasePath(); IndexManager.manualReindex(); MagentoComponentManager.getInstance(project).flushModules(); notification.expire(); } - } - ); + }); Notifications.Bus.notify(notification, project); }); }); diff --git a/src/com/magento/idea/magento2plugin/project/Settings.java b/src/com/magento/idea/magento2plugin/project/Settings.java index f844594a7..c7ca4ed0f 100644 --- a/src/com/magento/idea/magento2plugin/project/Settings.java +++ b/src/com/magento/idea/magento2plugin/project/Settings.java @@ -4,11 +4,18 @@ */ package com.magento.idea.magento2plugin.project; +import com.intellij.ide.util.PropertiesComponent; import com.intellij.openapi.components.*; import com.intellij.openapi.project.Project; -import com.intellij.util.xmlb.XmlSerializerUtil; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.util.EventDispatcher; +import com.intellij.util.xmlb.annotations.Attribute; +import com.intellij.util.xmlb.annotations.Tag; +import com.magento.idea.magento2plugin.init.ConfigurationManager; +import com.magento.idea.magento2plugin.util.magento.MagentoBasePathUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.EventListener; @State( name = "Magento2PluginSettings", @@ -16,20 +23,53 @@ @Storage("magento2plugin.xml") } ) -public class Settings implements PersistentStateComponent { +public class Settings implements PersistentStateComponent { + private final EventDispatcher myEventDispatcher = EventDispatcher.create(MagentoModuleDataListener.class); public boolean pluginEnabled = false; - public String defaultLicenseName = "Proprietary"; + public static String DEFAULT_LICENSE = "Proprietary"; + public String magentoPath; public boolean mftfSupportEnabled = false; + public boolean myDoNotAskContentConfigAgain = false; @Nullable - @Override - public Settings getState() { - return this; + public Settings.State getState() { + return new Settings.State(this.pluginEnabled, this.magentoPath, DEFAULT_LICENSE, this.mftfSupportEnabled, this.myDoNotAskContentConfigAgain); } - @Override - public void loadState(Settings settings) { - XmlSerializerUtil.copyBean(settings, this); + public void setState(State state) { + State oldState = this.getState(); + this.loadState(state); + this.notifyListeners(state, oldState); + } + + public void loadState(@NotNull Settings.State state) { + this.pluginEnabled = state.isPluginEnabled(); + this.magentoPath = state.getMagentoPath(); + this.DEFAULT_LICENSE = state.getDefaultLicenseName(); + this.mftfSupportEnabled = state.isMftfSupportEnabled(); + this.myDoNotAskContentConfigAgain = state.isDoNotAskContentConfigAgain(); + } + + public void addListener(MagentoModuleDataListener listener) { + this.myEventDispatcher.addListener(listener); + } + + private void notifyListeners(State state, State oldState) { + if (!state.equals(oldState)) { + this.myEventDispatcher.getMulticaster().stateChanged(state, oldState); + } + } + + public void setDoNotAskContentConfigurationAgain(boolean doNotAskContentConfigurationAgain) { + this.myDoNotAskContentConfigAgain = doNotAskContentConfigurationAgain; + } + + public void setMagentoPath(String magentoPath) { + this.magentoPath = magentoPath; + } + + public interface MagentoModuleDataListener extends EventListener { + void stateChanged(State state, State oldState); } public static Settings getInstance(Project project) { @@ -41,10 +81,129 @@ public static boolean isEnabled(@NotNull Project project) { } public static String getDefaultLicenseName(@NotNull Project project) { - return getInstance(project).defaultLicenseName; + return getInstance(project).DEFAULT_LICENSE; } public static boolean isMftfSupportEnabled(@NotNull Project project) { return getInstance(project).mftfSupportEnabled; } + + @Nullable + public static String getLastMagentoPath() { + return PropertiesComponent.getInstance().getValue("magento.support.magentoPath"); + } + + @Nullable + public static String getMagentoPath(@NotNull Project project) { + Settings settings = getInstance(project); + String path = settings.magentoPath; + if (path == null || path.isEmpty()) { + if (MagentoBasePathUtil.isMagentoFolderValid(project.getBasePath())) { + settings.setMagentoPath(project.getBasePath()); + } else { + settings.pluginEnabled = false; + ConfigurationManager.suggestToConfigureMagentoPath(project); + return null; + } + } + return settings.magentoPath; + } + + @Tag + public static class State { + public boolean pluginEnabled; + public String defaultLicenseName; + public String magentoPath; + public boolean mftfSupportEnabled; + public boolean myDoNotAskContentConfigAgain; + + public State() { + } + + public State(boolean pluginEnabled, String magentoPath, String defaultLicenseName, boolean mftfSupportEnabled, boolean myDoNotAskContentConfigAgain) { + this.pluginEnabled = pluginEnabled; + this.magentoPath = magentoPath; + this.defaultLicenseName = defaultLicenseName; + this.mftfSupportEnabled = mftfSupportEnabled; + this.myDoNotAskContentConfigAgain = myDoNotAskContentConfigAgain; + } + + @Attribute("enabled") + public boolean isPluginEnabled() { + return this.pluginEnabled; + } + + public void setPluginEnabled(boolean enabled) { + this.pluginEnabled = enabled; + } + + public String getMagentoPath() { + return this.magentoPath; + } + + @Tag("magentoPath") + public void setMagentoPath(String magentoPath) { + this.magentoPath = magentoPath; + } + + public void setMagentoPathAndUpdateLastUsed(String magetnoPath) { + this.setMagentoPath(magetnoPath); + if (!StringUtil.isEmptyOrSpaces(magetnoPath)) { + PropertiesComponent.getInstance().setValue("magento.support.magentoPath", magetnoPath); + } + } + + public String getDefaultLicenseName() { + return this.defaultLicenseName; + } + + public void setDefaultLicenseName(String defaultLicenseName) { + this.defaultLicenseName = defaultLicenseName; + } + + public boolean isMftfSupportEnabled() { + return this.mftfSupportEnabled; + } + + public boolean isDoNotAskContentConfigAgain() { + return this.myDoNotAskContentConfigAgain; + } + + public void setMftfSupportEnabled(boolean mftfSupportEnabled) { + this.mftfSupportEnabled = mftfSupportEnabled; + } + + public boolean equals(Object objectToCompare) { + if (this == objectToCompare) { + return true; + } else if (objectToCompare != null && this.getClass() == objectToCompare.getClass()) { + State state = (State) objectToCompare; + if (this.isPluginEnabled() != state.isPluginEnabled()) { + return false; + } else if (this.isMftfSupportEnabled() != state.isMftfSupportEnabled()) { + return false; + } else if (this.isDoNotAskContentConfigAgain() != state.isDoNotAskContentConfigAgain()) { + return false; + } else { + if (this.magentoPath != null) { + return this.magentoPath.equals(state.magentoPath); + } + if (this.defaultLicenseName != null) { + return this.defaultLicenseName.equals(state.defaultLicenseName); + } else return state.defaultLicenseName == null; + } + } else { + return false; + } + } + + public int hashCode() { + int result = this.isPluginEnabled() ? 1 : 0; + result = 31 * result + (this.magentoPath != null ? this.magentoPath.hashCode() : 0); + result = 31 * result + (this.isMftfSupportEnabled() ? 1 : 0); + result = 31 * result + (this.isDoNotAskContentConfigAgain() ? 1 : 0); + result = 31 * result + (this.defaultLicenseName != null ? this.defaultLicenseName.hashCode() : 0); + return result; + } + } } diff --git a/src/com/magento/idea/magento2plugin/project/SettingsForm.form b/src/com/magento/idea/magento2plugin/project/SettingsForm.form index aa5a89f77..15dab182e 100644 --- a/src/com/magento/idea/magento2plugin/project/SettingsForm.form +++ b/src/com/magento/idea/magento2plugin/project/SettingsForm.form @@ -1,6 +1,6 @@
- + @@ -22,7 +22,7 @@ - + @@ -36,16 +36,6 @@ - - - - - - - - - - @@ -76,6 +66,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/com/magento/idea/magento2plugin/project/SettingsForm.java b/src/com/magento/idea/magento2plugin/project/SettingsForm.java index 9bc52f4cd..7f07fa8b2 100644 --- a/src/com/magento/idea/magento2plugin/project/SettingsForm.java +++ b/src/com/magento/idea/magento2plugin/project/SettingsForm.java @@ -7,9 +7,14 @@ import com.intellij.javaee.ExternalResourceManager; import com.intellij.javaee.ExternalResourceManagerEx; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.fileChooser.FileChooserDescriptor; +import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory; import com.intellij.openapi.options.Configurable; import com.intellij.openapi.options.ConfigurationException; import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.ComponentWithBrowseButton; +import com.intellij.openapi.ui.TextComponentAccessor; +import com.intellij.openapi.ui.TextFieldWithBrowseButton; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiFile; @@ -17,32 +22,31 @@ import com.intellij.psi.search.FilenameIndex; import com.magento.idea.magento2plugin.actions.generation.util.MagentoVersion; import com.magento.idea.magento2plugin.indexes.IndexManager; +import com.magento.idea.magento2plugin.init.ConfigurationManager; import com.magento.idea.magento2plugin.php.module.*; +import com.magento.idea.magento2plugin.util.magento.MagentoBasePathUtil; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; - import javax.swing.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Collection; import java.util.Stack; -/** - * Created by dkvashnin on 1/9/16. - */ public class SettingsForm implements Configurable { private final static String DISPLAY_NAME = "Magento"; - private final Project project; private JCheckBox pluginEnabled; private JButton buttonReindex; - private JPanel panel1; + private JPanel jPanel; private JButton regenerateUrnMapButton; private JLabel magentoVersion; private JTextField moduleDefaultLicenseName; private JCheckBox mftfSupportEnabled; + private JLabel magentoPathLabel; + private TextFieldWithBrowseButton magentoPath; private MagentoVersion magentoVersionModel = MagentoVersion.getInstance(); public SettingsForm(@NotNull final Project project) { @@ -86,10 +90,12 @@ public void mouseClicked(MouseEvent e) { magentoVersion.setText("Magento version: " . concat(version)); } - moduleDefaultLicenseName.setText(getSettings().defaultLicenseName); + moduleDefaultLicenseName.setText(getSettings().DEFAULT_LICENSE); mftfSupportEnabled.setSelected(getSettings().mftfSupportEnabled); + magentoPath.getTextField().setText(getSettings().magentoPath); + addPathListener(); - return (JComponent) panel1; + return (JComponent) jPanel; } private void reindex() { @@ -99,25 +105,35 @@ private void reindex() { @Override public boolean isModified() { - boolean licenseChanged = !moduleDefaultLicenseName.getText().equals(getSettings().defaultLicenseName); + boolean licenseChanged = !moduleDefaultLicenseName.getText().equals(getSettings().DEFAULT_LICENSE); boolean statusChanged = !pluginEnabled.isSelected() == getSettings().pluginEnabled; boolean mftfSupportChanged = mftfSupportEnabled.isSelected() != getSettings().mftfSupportEnabled; + boolean magentoPathChanged = isMagentoPathChanged(); + + return statusChanged || licenseChanged || mftfSupportChanged || magentoPathChanged; + } - return statusChanged || licenseChanged || mftfSupportChanged; + private boolean isMagentoPathChanged() { + return !magentoPath.getTextField().getText().equals(getSettings().magentoPath); } @Override public void apply() throws ConfigurationException { getSettings().pluginEnabled = pluginEnabled.isSelected(); - getSettings().defaultLicenseName = moduleDefaultLicenseName.getText(); + getSettings().DEFAULT_LICENSE = moduleDefaultLicenseName.getText(); getSettings().mftfSupportEnabled = mftfSupportEnabled.isSelected(); + getSettings().magentoPath = magentoPath.getTextField().getText().trim(); buttonReindex.setEnabled(getSettings().pluginEnabled); regenerateUrnMapButton.setEnabled(getSettings().pluginEnabled); + if (getSettings().pluginEnabled && !MagentoBasePathUtil.isMagentoFolderValid(getSettings().magentoPath)) { + throw new ConfigurationException("Please specify valid magento installation path!"); + } + ConfigurationManager.getInstance().refreshIncludePaths(getSettings().getState(), project); + if (buttonReindex.isEnabled()) { reindex(); } - } @Override @@ -133,6 +149,24 @@ public void disposeUIResources() { private Settings getSettings() { return Settings.getInstance(project); } + + private void addPathListener() { + FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor(); + ComponentWithBrowseButton.BrowseFolderActionListener browseFolderListener = new ComponentWithBrowseButton.BrowseFolderActionListener( + "Magento Root Directory", + "Choose Magento root directory", + this.magentoPath, + null, + descriptor, + TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT + ) { + @Nullable + protected VirtualFile getInitialFile() { + return super.getInitialFile(); + } + }; + this.magentoPath.addActionListener(browseFolderListener); + } } class RegenerateUrnMapListener extends MouseAdapter { diff --git a/src/com/magento/idea/magento2plugin/project/util/GetProjectBasePath.java b/src/com/magento/idea/magento2plugin/project/util/GetProjectBasePath.java new file mode 100644 index 000000000..b650257f5 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/project/util/GetProjectBasePath.java @@ -0,0 +1,21 @@ +/** + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +package com.magento.idea.magento2plugin.project.util; + +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VirtualFile; +import org.jetbrains.annotations.NotNull; + +public class GetProjectBasePath { + + public static VirtualFile execute(@NotNull Project project) { + String basePath = project.getBasePath(); + if (basePath == null) { + return null; + } + return LocalFileSystem.getInstance().findFileByPath(basePath); + } +} diff --git a/src/com/magento/idea/magento2plugin/util/magento/GetModuleNameByDirectory.java b/src/com/magento/idea/magento2plugin/util/magento/GetModuleNameByDirectory.java index df9b21798..3a8a9fc76 100644 --- a/src/com/magento/idea/magento2plugin/util/magento/GetModuleNameByDirectory.java +++ b/src/com/magento/idea/magento2plugin/util/magento/GetModuleNameByDirectory.java @@ -41,15 +41,15 @@ public String execute(PsiDirectory psiDirectory) { private PhpFile getRegistrationPhpRecursively(PsiDirectory psiDirectory, Project project) { - String basePath = project.getBasePath(); - if (psiDirectory.getVirtualFile().getPath().equals(basePath)) { - return null; - } PsiElement[] containingFiles = psiDirectory.getChildren(); PhpFile containingFile = getModuleRegistrationPhpFile(containingFiles); if (containingFile != null) { return containingFile; } + String basePath = project.getBasePath(); + if (psiDirectory.getVirtualFile().getPath().equals(basePath)) { + return null; + } PsiDirectory getParent = psiDirectory.getParent(); if (getParent != null) { return getRegistrationPhpRecursively(getParent, project); diff --git a/src/com/magento/idea/magento2plugin/util/magento/MagentoBasePathUtil.java b/src/com/magento/idea/magento2plugin/util/magento/MagentoBasePathUtil.java new file mode 100644 index 000000000..5d4adf205 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/util/magento/MagentoBasePathUtil.java @@ -0,0 +1,29 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +package com.magento.idea.magento2plugin.util.magento; + +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VfsUtil; +import com.intellij.openapi.vfs.VirtualFile; +import com.magento.idea.magento2plugin.magento.files.ModuleDiXml; +import com.magento.idea.magento2plugin.magento.packages.Package; + +public class MagentoBasePathUtil { + + public static boolean isMagentoFolderValid(String path) { + if (StringUtil.isEmptyOrSpaces(path)) { + return false; + } else { + VirtualFile file = LocalFileSystem.getInstance().findFileByPath(path); + if (file != null && file.isDirectory()) { + return VfsUtil.findRelativeFile(file, Package.APP, Package.MODULE_BASE_AREA_DIR, ModuleDiXml.FILE_NAME) != null && + VfsUtil.findRelativeFile(file, new String[]{Package.VENDOR}) != null; + } else { + return false; + } + } + } +} From b2be06449cd7b57ea96672b35a187a6423a34a0e Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Thu, 9 Apr 2020 14:19:10 +0300 Subject: [PATCH 2/6] Moved configs to frameworks section --- README.md | 2 +- resources/META-INF/plugin.xml | 12 ++++---- .../project/ConfigurableProvider.java | 28 +++++++++++++++++++ .../magento2plugin/project/SettingsForm.java | 15 ++++++++-- .../project/UsagesProvider.java | 24 ++++++++++++++++ 5 files changed, 71 insertions(+), 10 deletions(-) create mode 100644 src/com/magento/idea/magento2plugin/project/ConfigurableProvider.java create mode 100644 src/com/magento/idea/magento2plugin/project/UsagesProvider.java diff --git a/README.md b/README.md index 872237f97..bfbacebbf 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ This is a plugin for Magento 2 development in the PhpStorm IDE. It is available 2. Navigate to `Plugins` 3. Click the `Browse repositories...` button and search for "Magento PhpStorm" 4. Install the plugin and restart PhpStorm -5. Go to `Settings > Preferences > Languages & Frameworks > PHP > Magento` in the PhpStorm IDE +5. Go to `Settings > Preferences > Languages & Frameworks > PHP > Frameworks > Magento` in the PhpStorm IDE 6. Check `Enable` and `OK` button. ## Works with diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index 9279ac4ef..dab6a0340 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -82,13 +82,6 @@ - @@ -156,4 +149,9 @@ + + + + + diff --git a/src/com/magento/idea/magento2plugin/project/ConfigurableProvider.java b/src/com/magento/idea/magento2plugin/project/ConfigurableProvider.java new file mode 100644 index 000000000..8ba3f6960 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/project/ConfigurableProvider.java @@ -0,0 +1,28 @@ +/** + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +package com.magento.idea.magento2plugin.project; + +import com.intellij.openapi.project.Project; +import com.jetbrains.php.frameworks.PhpFrameworkConfigurable; +import com.jetbrains.php.frameworks.PhpFrameworkConfigurableProvider; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NotNull; + +public class ConfigurableProvider implements PhpFrameworkConfigurableProvider { + public ConfigurableProvider() { + } + + @Nls + @NotNull + public String getName() { + return "Magento 2"; + } + + @NotNull + public PhpFrameworkConfigurable createConfigurable(@NotNull Project project) { + + return new SettingsForm(project); + } +} diff --git a/src/com/magento/idea/magento2plugin/project/SettingsForm.java b/src/com/magento/idea/magento2plugin/project/SettingsForm.java index 7f07fa8b2..8ff37098c 100644 --- a/src/com/magento/idea/magento2plugin/project/SettingsForm.java +++ b/src/com/magento/idea/magento2plugin/project/SettingsForm.java @@ -9,7 +9,6 @@ import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.fileChooser.FileChooserDescriptor; import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory; -import com.intellij.openapi.options.Configurable; import com.intellij.openapi.options.ConfigurationException; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.ComponentWithBrowseButton; @@ -20,6 +19,7 @@ import com.intellij.psi.PsiFile; import com.intellij.psi.PsiManager; import com.intellij.psi.search.FilenameIndex; +import com.jetbrains.php.frameworks.PhpFrameworkConfigurable; import com.magento.idea.magento2plugin.actions.generation.util.MagentoVersion; import com.magento.idea.magento2plugin.indexes.IndexManager; import com.magento.idea.magento2plugin.init.ConfigurationManager; @@ -34,7 +34,7 @@ import java.util.Collection; import java.util.Stack; -public class SettingsForm implements Configurable { +public class SettingsForm implements PhpFrameworkConfigurable { private final static String DISPLAY_NAME = "Magento"; private final Project project; @@ -167,6 +167,17 @@ protected VirtualFile getInitialFile() { }; this.magentoPath.addActionListener(browseFolderListener); } + + @Override + public boolean isBeingUsed() { + return this.pluginEnabled.isSelected(); + } + + @NotNull + @Override + public String getId() { + return "Magento2.SettingsForm"; + } } class RegenerateUrnMapListener extends MouseAdapter { diff --git a/src/com/magento/idea/magento2plugin/project/UsagesProvider.java b/src/com/magento/idea/magento2plugin/project/UsagesProvider.java new file mode 100644 index 000000000..4a42a2dd6 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/project/UsagesProvider.java @@ -0,0 +1,24 @@ +/** + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +package com.magento.idea.magento2plugin.project; + +import com.intellij.openapi.project.Project; +import com.jetbrains.php.frameworks.PhpFrameworkUsageProvider; +import org.jetbrains.annotations.NotNull; + +public class UsagesProvider implements PhpFrameworkUsageProvider { + public UsagesProvider() { + } + + @NotNull + public String getName() { + return "Magento 2"; + } + + public boolean isEnabled(@NotNull Project project) { + + return Settings.getInstance(project).pluginEnabled; + } +} From 3c8e290f4c81376a2688a267d0e201038326d016 Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Thu, 9 Apr 2020 15:27:56 +0300 Subject: [PATCH 3/6] Static fix --- .../magento2plugin/generation/MagentoTemplatesFactory.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/com/magento/idea/magento2plugin/generation/MagentoTemplatesFactory.java b/src/com/magento/idea/magento2plugin/generation/MagentoTemplatesFactory.java index c22a74292..22d36c55d 100644 --- a/src/com/magento/idea/magento2plugin/generation/MagentoTemplatesFactory.java +++ b/src/com/magento/idea/magento2plugin/generation/MagentoTemplatesFactory.java @@ -7,6 +7,7 @@ import com.intellij.ide.util.projectWizard.WizardContext; import com.intellij.platform.ProjectTemplate; import com.intellij.platform.ProjectTemplatesFactory; +import com.jetbrains.php.config.generation.PhpEmptyTemplatesFactory; import icons.PhpIcons; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -18,7 +19,7 @@ public MagentoTemplatesFactory() { @NotNull public String[] getGroups() { - return new String[]{"PHP"}; + return new String[]{PhpEmptyTemplatesFactory.PHP_PROJECT_TEMPLATE_GROUP}; } public Icon getGroupIcon(String group) { From bfa0d103864f181ef58c60280b389e1f00192472 Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Thu, 9 Apr 2020 15:37:18 +0300 Subject: [PATCH 4/6] Updated CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 673cd84db..3a6cee20a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ * Create a Preference for a class action * Create a Block action * Line markers for navigation from a plugin class to a target class + * Magento 2 Module Project template on the start up + * Create an observer for an event action + * Plugin - Target line markers + * GraphQL resolver inspection in the scope of a PHP Class 0.3.0 ============= From 65a9262bd1f59417cdeaa0b295d544227d8f4668 Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Thu, 9 Apr 2020 16:19:00 +0300 Subject: [PATCH 5/6] Code review fixes --- CHANGELOG.md | 2 +- .../actions/generation/data/ModuleComposerJsonData.java | 5 +++-- .../actions/generation/data/ModuleRegistrationPhpData.java | 4 +++- .../actions/generation/data/ModuleXmlData.java | 4 +++- .../actions/generation/dialog/NewModuleDialog.java | 4 ---- .../generation/generator/ModuleComposerJsonGenerator.java | 3 +-- .../generator/ModuleRegistrationPhpGenerator.java | 3 +-- .../actions/generation/generator/ModuleXmlGenerator.java | 5 ++--- .../magento2plugin/generation/MagentoModuleGenerator.java | 1 - .../generation/MagentoProjectGeneratorSettings.java | 1 - .../idea/magento2plugin/init/ConfigurationManager.java | 6 +++--- 11 files changed, 17 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a6cee20a..2c35a8112 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ * RequireJS mapping support (reference navigation, completion) * Plugin class methods generation * Plugin declaration inspection in the scope of a Plugin Class - * MFTF support (reference navigation, completion) + * MFTF support MVP (reference navigation, completion) * Fixed support of 2020.* versions of IDE's * Create a New Magento 2 Module action * Code Inspection: Duplicated Observer Usage in events XML diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleComposerJsonData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleComposerJsonData.java index f8e8afb95..f51174bfb 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleComposerJsonData.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleComposerJsonData.java @@ -72,6 +72,7 @@ public List getModuleDependencies() { return moduleDependencies; } - public boolean getCreateModuleDirs() { return this.createModuleDirs; } - + public boolean getCreateModuleDirs() { + return this.createModuleDirs; + } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleRegistrationPhpData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleRegistrationPhpData.java index 3014ffdfe..c7ba86c3e 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleRegistrationPhpData.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleRegistrationPhpData.java @@ -36,5 +36,7 @@ public PsiDirectory getBaseDir() { return this.baseDir; } - public boolean getCreateModuleDirs() { return this.createModuleDirs; } + public boolean getCreateModuleDirs() { + return this.createModuleDirs; + } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleXmlData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleXmlData.java index 236d371e9..3719eec19 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleXmlData.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/ModuleXmlData.java @@ -36,5 +36,7 @@ public PsiDirectory getBaseDir() { return this.baseDir; } - public boolean getCreateModuleDirs() { return this.createModuleDirs; } + public boolean getCreateModuleDirs() { + return this.createModuleDirs; + } } 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 226ba3417..fc8e3e11d 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.java @@ -9,7 +9,6 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiFile; -import com.intellij.util.indexing.FileBasedIndex; 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.ModuleRegistrationPhpData; @@ -24,7 +23,6 @@ import com.magento.idea.magento2plugin.indexes.ModuleIndex; import com.magento.idea.magento2plugin.magento.packages.Package; import com.magento.idea.magento2plugin.project.Settings; -import com.magento.idea.magento2plugin.stubs.indexes.ModuleNameIndex; import com.magento.idea.magento2plugin.util.CamelCaseToHyphen; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -32,8 +30,6 @@ import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import java.awt.event.*; -import java.util.ArrayList; -import java.util.Collection; import java.util.List; import java.util.Vector; diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleComposerJsonGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleComposerJsonGenerator.java index a16056051..33223f5b6 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleComposerJsonGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleComposerJsonGenerator.java @@ -45,9 +45,8 @@ public PsiFile generate(String actionName) { if (moduleComposerJsonData.getCreateModuleDirs()) { ModuleDirectoriesData moduleDirectoriesData = directoryGenerator.createOrFindModuleDirectories(moduleComposerJsonData.getPackageName(), moduleComposerJsonData.getModuleName(), moduleComposerJsonData.getBaseDir()); return fileFromTemplateGenerator.generate(ComposerJson.getInstance(), getAttributes(), moduleDirectoriesData.getModuleDirectory(), actionName); - } else { - return fileFromTemplateGenerator.generate(ComposerJson.getInstance(), getAttributes(), moduleComposerJsonData.getBaseDir(), actionName); } + return fileFromTemplateGenerator.generate(ComposerJson.getInstance(), getAttributes(), moduleComposerJsonData.getBaseDir(), actionName); } protected void fillAttributes(Properties attributes) { diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleRegistrationPhpGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleRegistrationPhpGenerator.java index c2173ce5a..011093080 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleRegistrationPhpGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleRegistrationPhpGenerator.java @@ -31,9 +31,8 @@ public PsiFile generate(String actionName) { if (moduleRegistrationPhpData.getCreateModuleDirs()) { ModuleDirectoriesData moduleDirectoriesData = directoryGenerator.createOrFindModuleDirectories(moduleRegistrationPhpData.getPackageName(), moduleRegistrationPhpData.getModuleName(), moduleRegistrationPhpData.getBaseDir()); return fileFromTemplateGenerator.generate(RegistrationPhp.getInstance(), getAttributes(), moduleDirectoriesData.getModuleDirectory(), actionName); - } else { - return fileFromTemplateGenerator.generate(RegistrationPhp.getInstance(), getAttributes(), moduleRegistrationPhpData.getBaseDir(), actionName); } + return fileFromTemplateGenerator.generate(RegistrationPhp.getInstance(), getAttributes(), moduleRegistrationPhpData.getBaseDir(), actionName); } protected void fillAttributes(Properties attributes) { diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleXmlGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleXmlGenerator.java index 2a591ba11..2afb812be 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleXmlGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleXmlGenerator.java @@ -34,10 +34,9 @@ public PsiFile generate(String actionName) { if (moduleXmlData.getCreateModuleDirs()) { ModuleDirectoriesData moduleDirectoriesData = directoryGenerator.createOrFindModuleDirectories(moduleXmlData.getPackageName(), moduleXmlData.getModuleName(), moduleXmlData.getBaseDir()); return fileFromTemplateGenerator.generate(ModuleXml.getInstance(), getAttributes(), moduleDirectoriesData.getModuleEtcDirectory(), actionName); - } else { - PsiDirectory etcDirectory = directoryGenerator.findOrCreateSubdirectory(moduleXmlData.getBaseDir(), Package.MODULE_BASE_AREA_DIR); - return fileFromTemplateGenerator.generate(ModuleXml.getInstance(), getAttributes(), etcDirectory, actionName); } + PsiDirectory etcDirectory = directoryGenerator.findOrCreateSubdirectory(moduleXmlData.getBaseDir(), Package.MODULE_BASE_AREA_DIR); + return fileFromTemplateGenerator.generate(ModuleXml.getInstance(), getAttributes(), etcDirectory, actionName); } protected void fillAttributes(Properties attributes) { diff --git a/src/com/magento/idea/magento2plugin/generation/MagentoModuleGenerator.java b/src/com/magento/idea/magento2plugin/generation/MagentoModuleGenerator.java index 2cccf0c37..0d43ccbbc 100644 --- a/src/com/magento/idea/magento2plugin/generation/MagentoModuleGenerator.java +++ b/src/com/magento/idea/magento2plugin/generation/MagentoModuleGenerator.java @@ -28,7 +28,6 @@ import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; - import java.util.ArrayList; public class MagentoModuleGenerator extends WebProjectTemplate { diff --git a/src/com/magento/idea/magento2plugin/generation/MagentoProjectGeneratorSettings.java b/src/com/magento/idea/magento2plugin/generation/MagentoProjectGeneratorSettings.java index 33b6ede2a..b30626be7 100644 --- a/src/com/magento/idea/magento2plugin/generation/MagentoProjectGeneratorSettings.java +++ b/src/com/magento/idea/magento2plugin/generation/MagentoProjectGeneratorSettings.java @@ -5,7 +5,6 @@ package com.magento.idea.magento2plugin.generation; import com.magento.idea.magento2plugin.project.Settings; - import java.util.List; public class MagentoProjectGeneratorSettings { diff --git a/src/com/magento/idea/magento2plugin/init/ConfigurationManager.java b/src/com/magento/idea/magento2plugin/init/ConfigurationManager.java index adc9142e3..7f5548015 100644 --- a/src/com/magento/idea/magento2plugin/init/ConfigurationManager.java +++ b/src/com/magento/idea/magento2plugin/init/ConfigurationManager.java @@ -87,7 +87,7 @@ public void actionPerformed(@NotNull AnActionEvent e) { ContentEntry[] entries = contentEntries; int length = contentEntries.length; - for(int i = 0; i < length; ++i) { + for (int i = 0; i < length; ++i) { ContentEntry entry = entries[i]; VirtualFile entryFile = entry.getFile(); if (entryFile != null && VfsUtilCore.isAncestor(entryFile, project.getBaseDir(), false)) { @@ -215,7 +215,7 @@ private static boolean isFileInsideModule(@NotNull VirtualFile magentoFile, @Not VirtualFile[] roots = contentRoots; int length = contentRoots.length; - for(int i = 0; i < length; ++i) { + for (int i = 0; i < length; ++i) { VirtualFile root = roots[i]; if (VfsUtilCore.isAncestor(root, magentoFile, false)) { return true; @@ -237,7 +237,7 @@ public static void notifyGlobally(@Nullable Project project, String title, Strin Function[] functions = actions; int length = actions.length; - for(int i = 0; i < length; ++i) { + for (int i = 0; i < length; ++i) { Function generator = functions[i]; notification.addAction(generator.apply(notification)); } From 85428e3a9476a9ee59c180cb3745017125a824a1 Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Thu, 9 Apr 2020 16:22:51 +0300 Subject: [PATCH 6/6] Code review fixes 2 --- .../project/ConfigurableProvider.java | 1 - .../magento2plugin/project/UsagesProvider.java | 1 - .../util/magento/MagentoBasePathUtil.java | 14 ++++++-------- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/project/ConfigurableProvider.java b/src/com/magento/idea/magento2plugin/project/ConfigurableProvider.java index 8ba3f6960..d5b84f76a 100644 --- a/src/com/magento/idea/magento2plugin/project/ConfigurableProvider.java +++ b/src/com/magento/idea/magento2plugin/project/ConfigurableProvider.java @@ -22,7 +22,6 @@ public String getName() { @NotNull public PhpFrameworkConfigurable createConfigurable(@NotNull Project project) { - return new SettingsForm(project); } } diff --git a/src/com/magento/idea/magento2plugin/project/UsagesProvider.java b/src/com/magento/idea/magento2plugin/project/UsagesProvider.java index 4a42a2dd6..f1e95cdca 100644 --- a/src/com/magento/idea/magento2plugin/project/UsagesProvider.java +++ b/src/com/magento/idea/magento2plugin/project/UsagesProvider.java @@ -18,7 +18,6 @@ public String getName() { } public boolean isEnabled(@NotNull Project project) { - return Settings.getInstance(project).pluginEnabled; } } diff --git a/src/com/magento/idea/magento2plugin/util/magento/MagentoBasePathUtil.java b/src/com/magento/idea/magento2plugin/util/magento/MagentoBasePathUtil.java index 5d4adf205..9a58478f0 100644 --- a/src/com/magento/idea/magento2plugin/util/magento/MagentoBasePathUtil.java +++ b/src/com/magento/idea/magento2plugin/util/magento/MagentoBasePathUtil.java @@ -16,14 +16,12 @@ public class MagentoBasePathUtil { public static boolean isMagentoFolderValid(String path) { if (StringUtil.isEmptyOrSpaces(path)) { return false; - } else { - VirtualFile file = LocalFileSystem.getInstance().findFileByPath(path); - if (file != null && file.isDirectory()) { - return VfsUtil.findRelativeFile(file, Package.APP, Package.MODULE_BASE_AREA_DIR, ModuleDiXml.FILE_NAME) != null && - VfsUtil.findRelativeFile(file, new String[]{Package.VENDOR}) != null; - } else { - return false; - } } + VirtualFile file = LocalFileSystem.getInstance().findFileByPath(path); + if (file != null && file.isDirectory()) { + return VfsUtil.findRelativeFile(file, Package.APP, Package.MODULE_BASE_AREA_DIR, ModuleDiXml.FILE_NAME) != null && + VfsUtil.findRelativeFile(file, new String[]{Package.VENDOR}) != null; + } + return false; } }