Skip to content
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

Update Platform Version In Setting Form #751

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 @@ -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() {
Expand All @@ -36,6 +39,10 @@ public int getPriority() {
return priority;
}

public String getDisplayName() {
return displayName;
}

/**
* Get Magento Versions List.
*
Expand Down
20 changes: 18 additions & 2 deletions src/com/magento/idea/magento2plugin/project/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class Settings implements PersistentStateComponent<Settings.State> {
public boolean mftfSupportEnabled;
public boolean myDoNotAskContentConfigAgain;
public String magentoVersion;
public String magentoEdition;

@Override
@Nullable
Expand All @@ -44,7 +45,8 @@ public Settings.State getState() {
defaultLicense,
this.mftfSupportEnabled,
this.myDoNotAskContentConfigAgain,
this.magentoVersion
this.magentoVersion,
this.magentoEdition
);
}

Expand All @@ -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) {
Expand Down Expand Up @@ -128,6 +131,7 @@ public static class State {
public boolean mftfSupportEnabled;
public boolean myDoNotAskContentConfigAgain;
public String magentoVersion;
public String magentoEdition;

public State() {//NOPMD
}
Expand All @@ -141,21 +145,24 @@ public State() {//NOPMD
* @param mftfSupportEnabled boolean
* @param myDoNotAskContentConfigAgain boolean
* @param magentoVersion String
* @param magentoEdition String
*/
public State(
final boolean pluginEnabled,
final String magentoPath,
final String defaultLicenseName,
final boolean mftfSupportEnabled,
final boolean myDoNotAskContentConfigAgain,
final String magentoVersion
final String magentoVersion,
final String magentoEdition
) {
this.pluginEnabled = pluginEnabled;
this.magentoPath = magentoPath;
this.defaultLicenseName = defaultLicenseName;
this.mftfSupportEnabled = mftfSupportEnabled;
this.myDoNotAskContentConfigAgain = myDoNotAskContentConfigAgain;
this.magentoVersion = magentoVersion;
this.magentoEdition = magentoEdition;
}

@Attribute("enabled")
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
<forms/>
</constraints>
<properties>
<text value="Magento version:"/>
<text value="Platform Version:"/>
</properties>
</component>
</children>
Expand Down
31 changes: 29 additions & 2 deletions src/com/magento/idea/magento2plugin/project/SettingsForm.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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

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

Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
Expand Down Expand Up @@ -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<String, String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, String> getVersion(final @NotNull JsonObject object) {
final JsonProperty packagesProperty = object.findProperty(ComposerLock.PACKAGES_PROP);

if (packagesProperty == null) {
Expand Down Expand Up @@ -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()
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,14 +32,34 @@ private MagentoVersionUtil() {}
* @return String
*/
public static String get(final Project project, final String magentoPath) {
final Pair<String, String> 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<String, String> getVersionData(
final Project project,
final String magentoPath
) {
final VirtualFile file = LocalFileSystem.getInstance().findFileByPath(
getFilePath(magentoPath)
);
final Pair<String, String> 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);

Expand All @@ -50,15 +71,14 @@ public static String get(final Project project, final String magentoPath) {
);

if (jsonObject == null) {
return DEFAULT_VERSION;
return versionData;
}
final Pair<String, String> 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) {
Expand All @@ -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)) {
Expand Down