|
| 1 | +/* |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.magento.idea.magento2plugin.actions.context.php; |
| 7 | + |
| 8 | +import com.intellij.openapi.actionSystem.AnActionEvent; |
| 9 | +import com.intellij.psi.PsiDirectory; |
| 10 | +import com.intellij.psi.PsiFile; |
| 11 | +import com.magento.idea.magento2plugin.actions.context.CustomGeneratorContextAction; |
| 12 | +import com.magento.idea.magento2plugin.actions.generation.dialog.NewSetupDataPatchDialog; |
| 13 | +import com.magento.idea.magento2plugin.magento.packages.ComponentType; |
| 14 | +import com.magento.idea.magento2plugin.magento.packages.Package; |
| 15 | +import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil; |
| 16 | +import org.jetbrains.annotations.NotNull; |
| 17 | + |
| 18 | +public class NewSetupDataPatchAction extends CustomGeneratorContextAction { |
| 19 | + |
| 20 | + public static final String ACTION_NAME = "Magento 2 Setup Data Patch"; |
| 21 | + public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Setup Data Patch"; |
| 22 | + public static final String SETUP_DIRECTORY = "Setup"; |
| 23 | + public static final String PATCH_DIRECTORY = "Patch"; |
| 24 | + public static final String DATA_DIRECTORY = "Data"; |
| 25 | + |
| 26 | + public NewSetupDataPatchAction() { |
| 27 | + super(ACTION_NAME, ACTION_DESCRIPTION); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public void actionPerformed(final @NotNull AnActionEvent event) { |
| 32 | + final GetMagentoModuleUtil.MagentoModuleData moduleData = getModuleData(); |
| 33 | + |
| 34 | + if (event.getProject() == null || moduleData == null || getDirectory() == null) { |
| 35 | + return; |
| 36 | + } |
| 37 | + final String[] module = moduleData.getName().split(Package.vendorModuleNameSeparator); |
| 38 | + |
| 39 | + if (module.length != 2) { //NOPMD |
| 40 | + return; |
| 41 | + } |
| 42 | + final PsiDirectory rootDirectory = moduleData.getModuleDir().findSubdirectory( |
| 43 | + SETUP_DIRECTORY |
| 44 | + ); |
| 45 | + |
| 46 | + if (rootDirectory == null) { |
| 47 | + return; |
| 48 | + } |
| 49 | + NewSetupDataPatchDialog.open(event.getProject(), rootDirectory, module[0], module[1]); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected boolean isVisible( |
| 54 | + final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData, |
| 55 | + final PsiDirectory targetDirectory, |
| 56 | + final PsiFile targetFile |
| 57 | + ) { |
| 58 | + if (!moduleData.getType().equals(ComponentType.module)) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + if (SETUP_DIRECTORY.equals(targetDirectory.getName())) { |
| 63 | + return moduleData.getModuleDir().equals(targetDirectory.getParentDirectory()); |
| 64 | + } |
| 65 | + |
| 66 | + if (PATCH_DIRECTORY.equals(targetDirectory.getName())) { |
| 67 | + final PsiDirectory setupDirCandidate = targetDirectory.getParentDirectory(); |
| 68 | + |
| 69 | + return setupDirCandidate != null |
| 70 | + && SETUP_DIRECTORY.equals(setupDirCandidate.getName()) |
| 71 | + && moduleData.getModuleDir().equals(setupDirCandidate.getParentDirectory()); |
| 72 | + } |
| 73 | + |
| 74 | + if (DATA_DIRECTORY.equals(targetDirectory.getName())) { |
| 75 | + final PsiDirectory patchDirCandidate = targetDirectory.getParentDirectory(); |
| 76 | + |
| 77 | + if (patchDirCandidate == null) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + final PsiDirectory setupDirCandidate = patchDirCandidate.getParentDirectory(); |
| 81 | + |
| 82 | + return setupDirCandidate != null |
| 83 | + && PATCH_DIRECTORY.equals(patchDirCandidate.getName()) |
| 84 | + && SETUP_DIRECTORY.equals(setupDirCandidate.getName()) |
| 85 | + && moduleData.getModuleDir().equals(setupDirCandidate.getParentDirectory()); |
| 86 | + } |
| 87 | + |
| 88 | + return false; |
| 89 | + } |
| 90 | +} |
0 commit comments