From 1f5f6f8f7b91b6be520f05e486cbaaef8cadb3c7 Mon Sep 17 00:00:00 2001 From: Serhii Akulov Date: Thu, 11 Nov 2021 19:42:19 +0200 Subject: [PATCH] Update Platform Version In Setting Form --- .../magento/packages/code/MagentoVersion.java | 13 +++++-- .../idea/magento2plugin/project/Settings.java | 20 +++++++++-- .../magento2plugin/project/SettingsForm.form | 2 +- .../magento2plugin/project/SettingsForm.java | 31 ++++++++++++++-- .../project/util/GetMagentoVersionUtil.java | 10 ++++-- .../util/magento/MagentoVersionUtil.java | 36 ++++++++++++++----- 6 files changed, 93 insertions(+), 19 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/magento/packages/code/MagentoVersion.java b/src/com/magento/idea/magento2plugin/magento/packages/code/MagentoVersion.java index 22af0c76e..810a8faab 100644 --- a/src/com/magento/idea/magento2plugin/magento/packages/code/MagentoVersion.java +++ b/src/com/magento/idea/magento2plugin/magento/packages/code/MagentoVersion.java @@ -11,21 +11,24 @@ public enum MagentoVersion { - ENTERPRISE_EDITION("magento/product-enterprise-edition", 1), - COMMUNITY_EDITION("magento/product-community-edition", 2); + ENTERPRISE_EDITION("magento/product-enterprise-edition", 1, "Adobe Commerce"), + COMMUNITY_EDITION("magento/product-community-edition", 2, "Magento Open Source"); private final String name; private final int priority; + private final String displayName; /** * Magento version Enum constructor. * * @param name String * @param priority int + * @param displayName String */ - MagentoVersion(final String name, final int priority) { + MagentoVersion(final String name, final int priority, final String displayName) { this.name = name; this.priority = priority; + this.displayName = displayName; } public String getName() { @@ -36,6 +39,10 @@ public int getPriority() { return priority; } + public String getDisplayName() { + return displayName; + } + /** * Get Magento Versions List. * diff --git a/src/com/magento/idea/magento2plugin/project/Settings.java b/src/com/magento/idea/magento2plugin/project/Settings.java index cc46f1f21..5f33a19b9 100644 --- a/src/com/magento/idea/magento2plugin/project/Settings.java +++ b/src/com/magento/idea/magento2plugin/project/Settings.java @@ -34,6 +34,7 @@ public class Settings implements PersistentStateComponent { public boolean mftfSupportEnabled; public boolean myDoNotAskContentConfigAgain; public String magentoVersion; + public String magentoEdition; @Override @Nullable @@ -44,7 +45,8 @@ public Settings.State getState() { defaultLicense, this.mftfSupportEnabled, this.myDoNotAskContentConfigAgain, - this.magentoVersion + this.magentoVersion, + this.magentoEdition ); } @@ -67,6 +69,7 @@ public void loadState(final @NotNull Settings.State state) { this.mftfSupportEnabled = state.isMftfSupportEnabled(); this.myDoNotAskContentConfigAgain = state.isDoNotAskContentConfigAgain(); this.magentoVersion = state.getMagentoVersion(); + this.magentoEdition = state.getMagentoEdition(); } public void addListener(final MagentoModuleDataListener listener) { @@ -128,6 +131,7 @@ public static class State { public boolean mftfSupportEnabled; public boolean myDoNotAskContentConfigAgain; public String magentoVersion; + public String magentoEdition; public State() {//NOPMD } @@ -141,6 +145,7 @@ public State() {//NOPMD * @param mftfSupportEnabled boolean * @param myDoNotAskContentConfigAgain boolean * @param magentoVersion String + * @param magentoEdition String */ public State( final boolean pluginEnabled, @@ -148,7 +153,8 @@ public State( final String defaultLicenseName, final boolean mftfSupportEnabled, final boolean myDoNotAskContentConfigAgain, - final String magentoVersion + final String magentoVersion, + final String magentoEdition ) { this.pluginEnabled = pluginEnabled; this.magentoPath = magentoPath; @@ -156,6 +162,7 @@ public State( this.mftfSupportEnabled = mftfSupportEnabled; this.myDoNotAskContentConfigAgain = myDoNotAskContentConfigAgain; this.magentoVersion = magentoVersion; + this.magentoEdition = magentoEdition; } @Attribute("enabled") @@ -185,6 +192,15 @@ public void setMagentoVersion(final String magentoVersion) { this.magentoVersion = magentoVersion; } + public String getMagentoEdition() { + return magentoEdition; + } + + @Tag("magentoEdition") + public void setMagentoEdition(final String magentoEdition) { + this.magentoEdition = magentoEdition; + } + /** * Last Used Magento Path setter. * diff --git a/src/com/magento/idea/magento2plugin/project/SettingsForm.form b/src/com/magento/idea/magento2plugin/project/SettingsForm.form index 727f0deeb..58fc6561b 100644 --- a/src/com/magento/idea/magento2plugin/project/SettingsForm.form +++ b/src/com/magento/idea/magento2plugin/project/SettingsForm.form @@ -98,7 +98,7 @@ - + diff --git a/src/com/magento/idea/magento2plugin/project/SettingsForm.java b/src/com/magento/idea/magento2plugin/project/SettingsForm.java index bf92acb56..7d7eca560 100644 --- a/src/com/magento/idea/magento2plugin/project/SettingsForm.java +++ b/src/com/magento/idea/magento2plugin/project/SettingsForm.java @@ -1,4 +1,4 @@ -/** +/* * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ @@ -12,6 +12,7 @@ import com.intellij.openapi.ui.ComponentWithBrowseButton; import com.intellij.openapi.ui.TextComponentAccessor; import com.intellij.openapi.ui.TextFieldWithBrowseButton; +import com.intellij.openapi.util.Pair; import com.intellij.openapi.vfs.VirtualFile; import com.jetbrains.php.frameworks.PhpFrameworkConfigurable; import com.magento.idea.magento2plugin.indexes.IndexManager; @@ -38,6 +39,9 @@ "PMD.TooManyMethods" }) public class SettingsForm implements PhpFrameworkConfigurable { + + private static final String DEFAULT_MAGENTO_EDITION_LABEL = "Platform Version:"; + private final Project project; private JCheckBox pluginEnabled; private JButton buttonReindex; @@ -48,6 +52,7 @@ public class SettingsForm implements PhpFrameworkConfigurable { private JCheckBox mftfSupportEnabled; private TextFieldWithBrowseButton magentoPath; private final SettingsFormValidator validator = new SettingsFormValidator(this); + private String magentoEdition; private JLabel magentoVersionLabel;//NOPMD private JLabel magentoPathLabel;//NOPMD @@ -95,6 +100,12 @@ public void mouseClicked(final MouseEvent event) { addPathListener(); addMagentoVersionListener(); + final String storedMagentoEdition = getSettings().magentoEdition; + + magentoVersionLabel.setText( + storedMagentoEdition == null ? DEFAULT_MAGENTO_EDITION_LABEL : storedMagentoEdition + ); + return (JComponent) panel; } @@ -159,6 +170,7 @@ private void saveSettings() { getSettings().mftfSupportEnabled = mftfSupportEnabled.isSelected(); getSettings().magentoPath = getMagentoPath(); getSettings().magentoVersion = getMagentoVersion(); + getSettings().magentoEdition = getMagentoEdition(); buttonReindex.setEnabled(getSettings().pluginEnabled); regenerateUrnMapButton.setEnabled(getSettings().pluginEnabled); } @@ -168,6 +180,10 @@ public String getMagentoVersion() { return magentoVersion.getText().trim(); } + public @NotNull String getMagentoEdition() { + return magentoEdition == null ? DEFAULT_MAGENTO_EDITION_LABEL : magentoEdition; + } + @NotNull public String getMagentoPath() { return magentoPath.getTextField().getText().trim(); @@ -233,8 +249,19 @@ public void changedUpdate(final DocumentEvent documentEvent) { */ public void updateMagentoVersion() { final String magentoPathValue = this.magentoPath.getTextField().getText(); - final String resolvedVersion = MagentoVersionUtil.get(project, magentoPathValue); + final Pair version = MagentoVersionUtil.getVersionData( + project, + magentoPathValue + ); + final String resolvedVersion = version.getFirst(); + final String resolvedEdition = version.getSecond() == null + ? DEFAULT_MAGENTO_EDITION_LABEL + : version.getSecond(); magentoVersion.setText(resolvedVersion); + magentoVersionLabel.setText(resolvedEdition); + + magentoEdition = resolvedEdition; + getSettings().magentoEdition = getMagentoEdition(); } @Override diff --git a/src/com/magento/idea/magento2plugin/project/util/GetMagentoVersionUtil.java b/src/com/magento/idea/magento2plugin/project/util/GetMagentoVersionUtil.java index b30dcea46..001db0b18 100644 --- a/src/com/magento/idea/magento2plugin/project/util/GetMagentoVersionUtil.java +++ b/src/com/magento/idea/magento2plugin/project/util/GetMagentoVersionUtil.java @@ -7,6 +7,7 @@ import com.intellij.json.psi.JsonObject; import com.intellij.json.psi.JsonProperty; +import com.intellij.openapi.util.Pair; import com.intellij.psi.util.PsiTreeUtil; import com.magento.idea.magento2plugin.magento.files.ComposerLock; import com.magento.idea.magento2plugin.magento.packages.code.MagentoVersion; @@ -28,10 +29,10 @@ private GetMagentoVersionUtil() { * * @param object JsonObject * - * @return String + * @return Pair[String, String] */ @SuppressWarnings("PMD.CyclomaticComplexity") - public static @Nullable String getVersion(final @NotNull JsonObject object) { + public static @Nullable Pair getVersion(final @NotNull JsonObject object) { final JsonProperty packagesProperty = object.findProperty(ComposerLock.PACKAGES_PROP); if (packagesProperty == null) { @@ -77,7 +78,10 @@ private GetMagentoVersionUtil() { for (final MagentoVersion version : versions) { if (foundMagentoPackages.containsKey(version.getName())) { - return foundMagentoPackages.get(version.getName()); + return new Pair<>( + foundMagentoPackages.get(version.getName()), + version.getDisplayName() + ); } } diff --git a/src/com/magento/idea/magento2plugin/util/magento/MagentoVersionUtil.java b/src/com/magento/idea/magento2plugin/util/magento/MagentoVersionUtil.java index 4db517cd2..f4506bb29 100644 --- a/src/com/magento/idea/magento2plugin/util/magento/MagentoVersionUtil.java +++ b/src/com/magento/idea/magento2plugin/util/magento/MagentoVersionUtil.java @@ -8,6 +8,7 @@ import com.intellij.json.psi.JsonFile; import com.intellij.json.psi.JsonObject; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Pair; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; @@ -31,14 +32,34 @@ private MagentoVersionUtil() {} * @return String */ public static String get(final Project project, final String magentoPath) { + final Pair version = getVersionData( + project, + magentoPath + ); + + return version.getFirst(); + } + + /** + * Parse composer.lock to detect Magento 2 version + * + * @param project Project + * @param magentoPath String + * + * @return Pair[String, String] + */ + public static Pair getVersionData( + final Project project, + final String magentoPath + ) { final VirtualFile file = LocalFileSystem.getInstance().findFileByPath( getFilePath(magentoPath) ); + final Pair versionData = new Pair<>(DEFAULT_VERSION, null); if (file == null) { - return DEFAULT_VERSION; + return versionData; } - final PsiManager psiManager = PsiManager.getInstance(project); final PsiFile composerFile = psiManager.findFile(file); @@ -50,15 +71,14 @@ public static String get(final Project project, final String magentoPath) { ); if (jsonObject == null) { - return DEFAULT_VERSION; + return versionData; } + final Pair version = GetMagentoVersionUtil.getVersion(jsonObject); - final String version = GetMagentoVersionUtil.getVersion(jsonObject); - - return version == null ? DEFAULT_VERSION : version; + return version == null ? versionData : version; } - return DEFAULT_VERSION; + return versionData; } private static String getFilePath(final String magentoPath) { @@ -74,7 +94,7 @@ private static String getFilePath(final String magentoPath) { * the value {@code false} if the argument version1 is less than to version2. */ public static boolean compare(final String version1, final String version2) { - if (version1.equals(DEFAULT_VERSION)) { + if (DEFAULT_VERSION.equals(version1)) { return true; } if (version1.equals(version2)) {