Skip to content

929: Fix showing package name on windows #1013

New issue

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

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

Already on GitHub? Sign in to your account

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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)
);
}
}