diff --git a/src/com/magento/idea/magento2plugin/actions/generation/NewModuleAction.java b/src/com/magento/idea/magento2plugin/actions/generation/NewModuleAction.java index 9a4ea61c2..0e9f0ca26 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/NewModuleAction.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/NewModuleAction.java @@ -19,6 +19,7 @@ import com.magento.idea.magento2plugin.actions.generation.util.IsClickedDirectoryInsideProject; import com.magento.idea.magento2plugin.project.Settings; import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil; +import com.magento.idea.magento2plugin.util.magento.MagentoBasePathUtil; import org.jetbrains.annotations.NotNull; public class NewModuleAction extends com.intellij.openapi.actionSystem.AnAction { @@ -89,6 +90,16 @@ public void update(final AnActionEvent event) { final String moduleName = GetModuleNameByDirectoryUtil .execute((PsiDirectory) psiElement, project); if (moduleName == null) { + final String sourceDirPath = ((PsiDirectory) psiElement).getVirtualFile().getPath(); + final boolean isCustomCodeSourceDirValid = + MagentoBasePathUtil.isCustomCodeSourceDirValid(sourceDirPath); + final boolean isCustomVendorDirValid = + MagentoBasePathUtil.isCustomVendorDirValid(sourceDirPath); + + if (!isCustomCodeSourceDirValid && !isCustomVendorDirValid) { //NOPMD + event.getPresentation().setVisible(false); + return; + } event.getPresentation().setVisible(true); return; } 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 e03a60350..407e4d99c 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.java @@ -6,6 +6,7 @@ package com.magento.idea.magento2plugin.actions.generation.dialog;//NOPMD import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiFile; import com.magento.idea.magento2plugin.actions.generation.NewModuleAction; @@ -23,9 +24,9 @@ import com.magento.idea.magento2plugin.indexes.ModuleIndex; import com.magento.idea.magento2plugin.magento.files.ComposerJson; import com.magento.idea.magento2plugin.magento.packages.Licenses; -import com.magento.idea.magento2plugin.magento.packages.Package; import com.magento.idea.magento2plugin.project.Settings; import com.magento.idea.magento2plugin.util.CamelCaseToHyphen; +import com.magento.idea.magento2plugin.util.magento.MagentoBasePathUtil; import com.magento.idea.magento2plugin.util.magento.MagentoVersionUtil; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; @@ -163,8 +164,9 @@ public void windowClosing(final WindowEvent event) { } private void detectPackageName(final @NotNull PsiDirectory initialBaseDir) { - final PsiDirectory parentDir = initialBaseDir.getParent(); - if (parentDir != null && parentDir.toString().endsWith(Package.packagesRoot)) { + final VirtualFile initialBaseDirVf = initialBaseDir.getVirtualFile(); + + if (MagentoBasePathUtil.isCustomVendorDirValid(initialBaseDirVf.getPath())) { packageName.setVisible(false); packageNameLabel.setVisible(false); this.detectedPackageName = initialBaseDir.getName(); diff --git a/src/com/magento/idea/magento2plugin/magento/packages/Package.java b/src/com/magento/idea/magento2plugin/magento/packages/Package.java index dcd91eeec..3bf3d2bd2 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 @@ package com.magento.idea.magento2plugin.magento.packages; public class Package { //NOPMD + public static final String V_FILE_SEPARATOR = "/"; public static String packagesRoot = "app/code"; public static String libWebRoot = "lib/web"; public static String frameworkRootComposer = "vendor/magento/framework"; diff --git a/src/com/magento/idea/magento2plugin/util/magento/MagentoBasePathUtil.java b/src/com/magento/idea/magento2plugin/util/magento/MagentoBasePathUtil.java index 8cd83bf60..b3efd164c 100644 --- a/src/com/magento/idea/magento2plugin/util/magento/MagentoBasePathUtil.java +++ b/src/com/magento/idea/magento2plugin/util/magento/MagentoBasePathUtil.java @@ -9,8 +9,10 @@ import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; -import com.magento.idea.magento2plugin.magento.packages.File; import com.magento.idea.magento2plugin.magento.packages.Package; +import java.io.File; +import java.util.Arrays; +import org.jetbrains.annotations.NotNull; public final class MagentoBasePathUtil { @@ -38,4 +40,54 @@ public static boolean isMagentoFolderValid(final String path) { } return false; } + + /** + * Check if specified path belongs to the correct vendor name. + * + * @param path String + * + * @return boolean + */ + public static boolean isCustomVendorDirValid(final @NotNull String path) { + final String[] pathParts = path.split(Package.V_FILE_SEPARATOR); + + if (pathParts.length < 3) { //NOPMD + return false; + } + + final String[] sourceCandidateParts = Arrays.copyOfRange( + pathParts, + pathParts.length - 3, + pathParts.length - 1 + ); + + return Package.packagesRoot.equals( + String.join(Package.V_FILE_SEPARATOR, sourceCandidateParts) + ); + } + + /** + * Check if specified path belongs to the correct packages folder. + * + * @param path String + * + * @return boolean + */ + public static boolean isCustomCodeSourceDirValid(final @NotNull String path) { + final String[] pathParts = path.split(Package.V_FILE_SEPARATOR); + + if (pathParts.length < 2) { //NOPMD + return false; + } + + final String[] sourceCandidateParts = Arrays.copyOfRange( + pathParts, + pathParts.length - 2, + pathParts.length + ); + + return Package.packagesRoot.equals( + String.join(Package.V_FILE_SEPARATOR, sourceCandidateParts) + ); + } }