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

#131: Support for tool dependencies #145

Closed
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a9f80db
#131: Support for tool dependencies
aBega2000 Nov 20, 2023
41d23ef
Merge branch 'main' of https://github.com/aBega2000/IDEasy into featu…
aBega2000 Nov 20, 2023
96f807f
Changes after review
aBega2000 Nov 22, 2023
ecd1df3
Update ToolCommandlet.java
aBega2000 Nov 22, 2023
b222aa5
#131: Changes after reviews
aBega2000 Nov 27, 2023
6080c80
Merge branch 'feature/131-support-for-tool-dependecies' of https://gi…
aBega2000 Nov 27, 2023
f7ff2c2
#131: Changes after review
aBega2000 Dec 1, 2023
e44d547
#131: Changes after review
aBega2000 Dec 13, 2023
4a6c8e9
Merge branch 'devonfw:main' into feature/131-support-for-tool-depende…
aBega2000 Dec 13, 2023
2805f0c
Merge branch 'feature/131-support-for-tool-dependecies' of https://gi…
aBega2000 Dec 13, 2023
67c5c9c
Small Change
aBega2000 Dec 13, 2023
cc8c0cb
#131: Changes after review
aBega2000 Dec 14, 2023
ed0b0f3
Merge branch 'feature/131-support-for-tool-dependecies' of https://gi…
aBega2000 Dec 14, 2023
18884c2
Merge branch 'main' into feature/131-support-for-tool-dependecies
jan-vcapgemini Jan 8, 2024
f2ed771
Merge branch 'devonfw:main' into feature/131-support-for-tool-depende…
aBega2000 Jan 8, 2024
32e4bd1
#131: Changes after review and according to VersionRange with boundar…
aBega2000 Jan 10, 2024
48e3437
Merge branch 'main' into feature/131-support-for-tool-dependecies
aBega2000 Jan 17, 2024
a3d82b9
Merge branch 'main' into feature/131-support-for-tool-dependecies
hohwille Jan 19, 2024
21781f2
#131: Resolved Conflicts
aBega2000 Feb 1, 2024
e4ad57b
Merge commit '43272714a125e23e8b47d4501a16b722cde17ab2' into feature/…
aBega2000 Feb 1, 2024
b9846f8
Merge branch 'feature/131-support-for-tool-dependecies' of https://gi…
aBega2000 Feb 1, 2024
52840dc
Merge branch 'main' of https://github.com/aBega2000/IDEasy into featu…
aBega2000 Feb 1, 2024
fbcbf31
#131: Changes after review
aBega2000 Feb 7, 2024
0a9b2b5
Merge branch 'main' into feature/131-support-for-tool-dependecies
aBega2000 Mar 7, 2024
8f04879
Merge branch 'devonfw:main' into feature/131-support-for-tool-depende…
aBega2000 Mar 12, 2024
584c5f4
Update DependencyJson.java
hohwille Mar 19, 2024
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
176 changes: 176 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/tool/ToolCommandlet.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.devonfw.tools.ide.tool;

import java.io.File;
aBega2000 marked this conversation as resolved.
Show resolved Hide resolved
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

Expand All @@ -22,6 +25,8 @@
import com.devonfw.tools.ide.property.StringListProperty;
import com.devonfw.tools.ide.util.FilenameUtil;
import com.devonfw.tools.ide.version.VersionIdentifier;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
* {@link Commandlet} for a tool integrated into the IDE.
Expand Down Expand Up @@ -388,4 +393,175 @@ public void setVersion(VersionIdentifier version, boolean hint) {
}
}

/**
* Method to be overridden so that for each tool the actual dependency name is set.
*
* @return the dependency name as {@link String}
*/
protected String getDependencyName() {
return "";
}

/**
* Method to be overridden so that for each tool the actual Json Path of the dependencies is set.
*
* @return the dependency Json Path as {@link String}
*/
protected String getDependencyJsonPath() {
return "";
}

/**
* Method to get the version of the dependency of a specific tool, after the Json file is read.
*
* @param toolVersionToCheck the {@link String} of the version of the tool that should be checked.
* @return the Version of the dependency as {@link String}, for the tool that was to be checked or
* {@code null} if not found.
*/
protected String getDependencyVersion(String toolVersionToCheck) {

try {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode tomcatVersions = objectMapper.readTree(new File(getDependencyJsonPath()));
aBega2000 marked this conversation as resolved.
Show resolved Hide resolved

String requiredDependencyVersion = findDependencyVersionFromJson(tomcatVersions, toolVersionToCheck);

if (requiredDependencyVersion != null) {
this.context.info(getName() + " version " + toolVersionToCheck +
aBega2000 marked this conversation as resolved.
Show resolved Hide resolved
" requires at least " + getDependencyName() + " version " + requiredDependencyVersion);
return requiredDependencyVersion;
} else {
this.context.info("No specific " + getDependencyName() + " version requirement found for "+ getName()
+ " version " + toolVersionToCheck);
return null;
}
} catch (IOException e) {
e.printStackTrace();
aBega2000 marked this conversation as resolved.
Show resolved Hide resolved
}
return null;
}

/**
* Method to check if the installed version of the dependency is at least the version that needs to be installed.
*
* @param alreadyInstalledDependencyVersion the {@link String} of the version of the dependency that is already
* installed.
* @param dependencyVersionNumberFound the {@link String} of the version of the dependency that is found, and is
* a candidate to be installed
* @return {@code true} if the already installed version is at least the found candidate version,
* {@code false} otherwise
*/
protected boolean checkVersions (String alreadyInstalledDependencyVersion, String dependencyVersionNumberFound) {

int dotIndex = alreadyInstalledDependencyVersion.indexOf(".");
String majorVersionString = alreadyInstalledDependencyVersion.substring(0, dotIndex);
int majorVersionInstalled = Integer.parseInt(majorVersionString);
int majorVersionToInstall = Integer.parseInt(dependencyVersionNumberFound);

return majorVersionInstalled >= majorVersionToInstall;
}

/**
* Method to find the dependency version that should be installed from the list of the versions in the IDEasy,
* if the checkVersions method has returned false and the already installed version of the
* dependency is not sufficient.
*
* @param dependencyVersionNumberFound the {@link String} of the version of the dependency that is found that
* needs to be installed.
* @return {@link VersionIdentifier} the Version found from the IDEasy that should be installed.
*/
protected VersionIdentifier findDependencyVersionToInstall(String dependencyVersionNumberFound) {

String dependencyEdition = this.context.getVariables().getToolEdition(getDependencyName());

List<VersionIdentifier> versions = this.context.getUrls().getSortedVersions(getDependencyName(), dependencyEdition);

VersionIdentifier dependencyVersionToInstall = null;

for (VersionIdentifier vi : versions) {
if (vi.toString().startsWith(dependencyVersionNumberFound)) {
dependencyVersionToInstall = vi;
break;
}
}
return dependencyVersionToInstall;
}

/**
* Method to find the dependency version that should be installed from Json file,
* if the version of the tool is given
*
* @param toolVersions the {@link JsonNode} that contains the versions of the tool listed in the Json file
* @param toolVersionToCheck the {@link String} of the tool version that is installed and needs to be checked
* in the Json file to find the right version of the dependency to be installed
* @return {@link String} the Version of the dependency with which the tool works correctly.
*/
private String findDependencyVersionFromJson(JsonNode toolVersions, String toolVersionToCheck) {
String requiredDependencyVersion = null;

// Iterate through the fields of the Json file
Iterator<Map.Entry<String, JsonNode>> fields = toolVersions.fields();
int[] targetVersion = parseVersion(toolVersionToCheck);

OuterLoop:
aBega2000 marked this conversation as resolved.
Show resolved Hide resolved
while (fields.hasNext()) {

Map.Entry<String, JsonNode> entry = fields.next();
String versionKey = entry.getKey();
int[] foundToolVersion = parseVersion(versionKey); // Found version when iterating the Json file

if (isEqualOrLess(foundToolVersion, targetVersion)) {
JsonNode dependencies = entry.getValue();
for (JsonNode dependencyNode : dependencies) {
if (getDependencyName().equals(dependencyNode.get("dependency").asText())) {
requiredDependencyVersion = dependencyNode.get("MinVersion").asText();
aBega2000 marked this conversation as resolved.
Show resolved Hide resolved
// TODO: Add logic to handle MaxVersion if needed
break OuterLoop; // Stop the loop when a matching Java version is found
}
}
}
}

return requiredDependencyVersion;
}

/**
* Method to parse the versions because they normally contain dots, like 10.1.14 should be separated into
* 10, 1 and 14
*
* @param versionString the {@link String} of the Version in its normal form
* @return Array of integers with the integers contained in the Version String.
*/
private int[] parseVersion(String versionString) {

String[] versionComponents = versionString.split("\\.");

int[] version = new int[versionComponents.length];
for (int j = 0; j < versionComponents.length; j++) {
version[j] = Integer.parseInt(versionComponents[j]);
}
return version;
}
aBega2000 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Method to compare two versions after they are parsed, so basically each Integer part of a version is compared with
* the corresponding part of the other Version.
*
* @param currentToolVersion the Array of integers of the current tool version that is parsed
* @param targetVersion the Array of integers of the target version, that is also parsed
* @return {@code true} if the current tool version is equal or less than the target version, {@code false} if
* it's larger
*/
private boolean isEqualOrLess(int[] currentToolVersion, int[] targetVersion) {

for (int i = 0; i < Math.min(currentToolVersion.length, targetVersion.length); i++) {
if (currentToolVersion[i] < targetVersion[i]) {
return true;
} else if (currentToolVersion[i] > targetVersion[i]) {
return false;
}
}
return true;
}
aBega2000 marked this conversation as resolved.
Show resolved Hide resolved

}
Loading