Skip to content

UCT-687: Enhanced Magento 2 version resolved #747

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
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
@@ -0,0 +1,14 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.magento.files;

public final class ComposerLock { // NOPMD

public static final String FILE_NAME = "composer.lock";
public static final String PACKAGES_PROP = "packages";
public static final String PACKAGE_NAME_PROP = "name";
public static final String PACKAGE_VERSION_PROP = "version";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.magento.packages.code;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public enum MagentoVersion {

ENTERPRISE_EDITION("magento/product-enterprise-edition", 1),
COMMUNITY_EDITION("magento/product-community-edition", 2);
Copy link
Contributor

@eduard13 eduard13 Nov 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these composer packages? If yes - how is supposed to work with projects installed via GitHub?

Copy link
Collaborator

@bohdan-harniuk bohdan-harniuk Nov 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eduard13, it was not supported in the previous functional and is not supposed to be supported for now. We need correct version lookup for the UCT version auto-detection. You can create new issue for adding support of dev Magento version resolving, or I'll create issue for that later.

Thanks, Bohdan


private final String name;
private final int priority;

/**
* Magento version Enum constructor.
*
* @param name String
* @param priority int
*/
MagentoVersion(final String name, final int priority) {
this.name = name;
this.priority = priority;
}

public String getName() {
return name;
}

public int getPriority() {
return priority;
}

/**
* Get Magento Versions List.
*
* @return List[MagentoVersion]
*/
public static List<MagentoVersion> getVersions() {
final List<MagentoVersion> versions = new ArrayList<>(
Arrays.asList(MagentoVersion.values())
);
versions.sort(
(version1, version2) -> version1.getPriority() > version2.getPriority() ? 1 : 0
);

return versions;
}

/**
* Get Magento Packages names.
*
* @return List[String]
*/
public static List<String> getVersionsNames() {
final List<String> names = new ArrayList<>();

for (final MagentoVersion version : MagentoVersion.values()) {
names.add(version.getName());
}

return names;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.project.util;

import com.intellij.json.psi.JsonObject;
import com.intellij.json.psi.JsonProperty;
import com.intellij.psi.util.PsiTreeUtil;
import com.magento.idea.magento2plugin.magento.files.ComposerLock;
import com.magento.idea.magento2plugin.magento.packages.code.MagentoVersion;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class GetMagentoVersionUtil {

private GetMagentoVersionUtil() {
}

/**
* Find Magento Package version in composer.lock json object.
*
* @param object JsonObject
*
* @return String
*/
@SuppressWarnings("PMD.CyclomaticComplexity")
public static @Nullable String getVersion(final @NotNull JsonObject object) {
final JsonProperty packagesProperty = object.findProperty(ComposerLock.PACKAGES_PROP);

if (packagesProperty == null) {
return null;
}
final Collection<JsonObject> packages = PsiTreeUtil.findChildrenOfType(
packagesProperty,
JsonObject.class
);
final List<MagentoVersion> versions = MagentoVersion.getVersions();
final List<String> versionNames = MagentoVersion.getVersionsNames();
final Map<String, String> foundMagentoPackages = new HashMap<>();

for (final JsonObject packageItem : packages) {
final JsonProperty nameProperty = packageItem.findProperty(
ComposerLock.PACKAGE_NAME_PROP
);

if (nameProperty == null || nameProperty.getValue() == null) {
continue;
}
final String name = StringUtils.strip(nameProperty.getValue().getText(), "\"");

if (versionNames.contains(name)) {
final JsonProperty versionProperty = packageItem.findProperty(
ComposerLock.PACKAGE_VERSION_PROP
);

if (versionProperty == null || versionProperty.getValue() == null) {
continue;
}

final String value = StringUtils.strip(
versionProperty.getValue().getText(), "\""
);
foundMagentoPackages.put(name, value);

if (MagentoVersion.ENTERPRISE_EDITION.getName().equals(name)) {
break;
}
}
}

for (final MagentoVersion version : versions) {
if (foundMagentoPackages.containsKey(version.getName())) {
return foundMagentoPackages.get(version.getName());
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.magento.idea.magento2plugin.magento.files.ComposerJson;
import com.magento.idea.magento2plugin.magento.packages.ComposerPackageModel;
import com.magento.idea.magento2plugin.magento.packages.ComposerPackageModelImpl;
import com.magento.idea.magento2plugin.magento.files.ComposerLock;
import com.magento.idea.magento2plugin.magento.packages.File;
import com.magento.idea.magento2plugin.magento.packages.Package;
import com.magento.idea.magento2plugin.project.util.GetMagentoVersionUtil;

public final class MagentoVersionUtil {

Expand Down Expand Up @@ -55,20 +53,16 @@ public static String get(final Project project, final String magentoPath) {
return DEFAULT_VERSION;
}

final ComposerPackageModel composerObject = new ComposerPackageModelImpl(jsonObject);
final String version = GetMagentoVersionUtil.getVersion(jsonObject);

if (composerObject.getType() != null
&& composerObject.getType().equals(Package.composerType)
&& composerObject.getVersion() != null) {
return composerObject.getVersion();
}
return version == null ? DEFAULT_VERSION : version;
}

return DEFAULT_VERSION;
}

private static String getFilePath(final String magentoPath) {
return magentoPath + File.separator + ComposerJson.FILE_NAME;
return magentoPath + File.separator + ComposerLock.FILE_NAME;
}

/**
Expand Down