Skip to content

Commit b17d5ab

Browse files
authored
Merge pull request #747 from Iamwade/incorrect-display-of-magento-2-version
UCT-687: Enhanced Magento 2 version resolved
2 parents 01db38f + 2c0ce1f commit b17d5ab

File tree

4 files changed

+174
-11
lines changed

4 files changed

+174
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.magento.files;
7+
8+
public final class ComposerLock { // NOPMD
9+
10+
public static final String FILE_NAME = "composer.lock";
11+
public static final String PACKAGES_PROP = "packages";
12+
public static final String PACKAGE_NAME_PROP = "name";
13+
public static final String PACKAGE_VERSION_PROP = "version";
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.magento.packages.code;
7+
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
public enum MagentoVersion {
13+
14+
ENTERPRISE_EDITION("magento/product-enterprise-edition", 1),
15+
COMMUNITY_EDITION("magento/product-community-edition", 2);
16+
17+
private final String name;
18+
private final int priority;
19+
20+
/**
21+
* Magento version Enum constructor.
22+
*
23+
* @param name String
24+
* @param priority int
25+
*/
26+
MagentoVersion(final String name, final int priority) {
27+
this.name = name;
28+
this.priority = priority;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public int getPriority() {
36+
return priority;
37+
}
38+
39+
/**
40+
* Get Magento Versions List.
41+
*
42+
* @return List[MagentoVersion]
43+
*/
44+
public static List<MagentoVersion> getVersions() {
45+
final List<MagentoVersion> versions = new ArrayList<>(
46+
Arrays.asList(MagentoVersion.values())
47+
);
48+
versions.sort(
49+
(version1, version2) -> version1.getPriority() > version2.getPriority() ? 1 : 0
50+
);
51+
52+
return versions;
53+
}
54+
55+
/**
56+
* Get Magento Packages names.
57+
*
58+
* @return List[String]
59+
*/
60+
public static List<String> getVersionsNames() {
61+
final List<String> names = new ArrayList<>();
62+
63+
for (final MagentoVersion version : MagentoVersion.values()) {
64+
names.add(version.getName());
65+
}
66+
67+
return names;
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.project.util;
7+
8+
import com.intellij.json.psi.JsonObject;
9+
import com.intellij.json.psi.JsonProperty;
10+
import com.intellij.psi.util.PsiTreeUtil;
11+
import com.magento.idea.magento2plugin.magento.files.ComposerLock;
12+
import com.magento.idea.magento2plugin.magento.packages.code.MagentoVersion;
13+
import java.util.Collection;
14+
import java.util.HashMap;
15+
import java.util.List;
16+
import java.util.Map;
17+
import org.apache.commons.lang.StringUtils;
18+
import org.jetbrains.annotations.NotNull;
19+
import org.jetbrains.annotations.Nullable;
20+
21+
public final class GetMagentoVersionUtil {
22+
23+
private GetMagentoVersionUtil() {
24+
}
25+
26+
/**
27+
* Find Magento Package version in composer.lock json object.
28+
*
29+
* @param object JsonObject
30+
*
31+
* @return String
32+
*/
33+
@SuppressWarnings("PMD.CyclomaticComplexity")
34+
public static @Nullable String getVersion(final @NotNull JsonObject object) {
35+
final JsonProperty packagesProperty = object.findProperty(ComposerLock.PACKAGES_PROP);
36+
37+
if (packagesProperty == null) {
38+
return null;
39+
}
40+
final Collection<JsonObject> packages = PsiTreeUtil.findChildrenOfType(
41+
packagesProperty,
42+
JsonObject.class
43+
);
44+
final List<MagentoVersion> versions = MagentoVersion.getVersions();
45+
final List<String> versionNames = MagentoVersion.getVersionsNames();
46+
final Map<String, String> foundMagentoPackages = new HashMap<>();
47+
48+
for (final JsonObject packageItem : packages) {
49+
final JsonProperty nameProperty = packageItem.findProperty(
50+
ComposerLock.PACKAGE_NAME_PROP
51+
);
52+
53+
if (nameProperty == null || nameProperty.getValue() == null) {
54+
continue;
55+
}
56+
final String name = StringUtils.strip(nameProperty.getValue().getText(), "\"");
57+
58+
if (versionNames.contains(name)) {
59+
final JsonProperty versionProperty = packageItem.findProperty(
60+
ComposerLock.PACKAGE_VERSION_PROP
61+
);
62+
63+
if (versionProperty == null || versionProperty.getValue() == null) {
64+
continue;
65+
}
66+
67+
final String value = StringUtils.strip(
68+
versionProperty.getValue().getText(), "\""
69+
);
70+
foundMagentoPackages.put(name, value);
71+
72+
if (MagentoVersion.ENTERPRISE_EDITION.getName().equals(name)) {
73+
break;
74+
}
75+
}
76+
}
77+
78+
for (final MagentoVersion version : versions) {
79+
if (foundMagentoPackages.containsKey(version.getName())) {
80+
return foundMagentoPackages.get(version.getName());
81+
}
82+
}
83+
84+
return null;
85+
}
86+
}

src/com/magento/idea/magento2plugin/util/magento/MagentoVersionUtil.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
import com.intellij.psi.PsiFile;
1414
import com.intellij.psi.PsiManager;
1515
import com.intellij.psi.util.PsiTreeUtil;
16-
import com.magento.idea.magento2plugin.magento.files.ComposerJson;
17-
import com.magento.idea.magento2plugin.magento.packages.ComposerPackageModel;
18-
import com.magento.idea.magento2plugin.magento.packages.ComposerPackageModelImpl;
16+
import com.magento.idea.magento2plugin.magento.files.ComposerLock;
1917
import com.magento.idea.magento2plugin.magento.packages.File;
20-
import com.magento.idea.magento2plugin.magento.packages.Package;
18+
import com.magento.idea.magento2plugin.project.util.GetMagentoVersionUtil;
2119

2220
public final class MagentoVersionUtil {
2321

@@ -55,20 +53,16 @@ public static String get(final Project project, final String magentoPath) {
5553
return DEFAULT_VERSION;
5654
}
5755

58-
final ComposerPackageModel composerObject = new ComposerPackageModelImpl(jsonObject);
56+
final String version = GetMagentoVersionUtil.getVersion(jsonObject);
5957

60-
if (composerObject.getType() != null
61-
&& composerObject.getType().equals(Package.composerType)
62-
&& composerObject.getVersion() != null) {
63-
return composerObject.getVersion();
64-
}
58+
return version == null ? DEFAULT_VERSION : version;
6559
}
6660

6761
return DEFAULT_VERSION;
6862
}
6963

7064
private static String getFilePath(final String magentoPath) {
71-
return magentoPath + File.separator + ComposerJson.FILE_NAME;
65+
return magentoPath + File.separator + ComposerLock.FILE_NAME;
7266
}
7367

7468
/**

0 commit comments

Comments
 (0)