Skip to content

Commit

Permalink
Changes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
aBega2000 committed Nov 22, 2023
1 parent 41d23ef commit 96f807f
Showing 1 changed file with 49 additions and 96 deletions.
145 changes: 49 additions & 96 deletions cli/src/main/java/com/devonfw/tools/ide/tool/ToolCommandlet.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.devonfw.tools.ide.tool;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -399,6 +398,7 @@ public void setVersion(VersionIdentifier version, boolean hint) {
* @return the dependency name as {@link String}
*/
protected String getDependencyName() {

return "";
}

Expand All @@ -408,160 +408,113 @@ protected String getDependencyName() {
* @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.
* @param toolVersionToCheck the {@link VersionIdentifier} 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) {
protected VersionIdentifier getDependencyVersion(VersionIdentifier toolVersionToCheck) {

try {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode tomcatVersions = objectMapper.readTree(new File(getDependencyJsonPath()));
Path jsonFilePath = Paths.get(getDependencyJsonPath());

JsonNode toolVersions = objectMapper.readTree(jsonFilePath.toFile());

String requiredDependencyVersion = findDependencyVersionFromJson(tomcatVersions, toolVersionToCheck);
VersionIdentifier requiredDependencyVersion = findDependencyVersionFromJson(toolVersions, toolVersionToCheck);

if (requiredDependencyVersion != null) {
this.context.info(getName() + " version " + toolVersionToCheck +
" requires at least " + getDependencyName() + " version " + requiredDependencyVersion);
this.context.info("The tool {} in version {} requires at least {} in version {} as a dependency", getName(),
toolVersionToCheck, getDependencyName(), requiredDependencyVersion);
return requiredDependencyVersion;
} else {
this.context.info("No specific " + getDependencyName() + " version requirement found for "+ getName()
+ " version " + toolVersionToCheck);
this.context.error("No specific {} version requirement found for {} in version {}", getDependencyName(),
getName(), toolVersionToCheck);
return null;
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
this.context.error("An error occurred: ", e);
}
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.
* Method to find the dependency version that should be installed from the list of the versions in the IDEasy, if 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.
* @param dependencyVersionNumberFound the {@link VersionIdentifier} 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) {
protected VersionIdentifier findDependencyVersionToInstall(VersionIdentifier 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;
if (vi.compareVersion(dependencyVersionNumberFound).isGreater()) {
return vi;
}
}
return dependencyVersionToInstall;
return null;
}

/**
* Method to find the dependency version that should be installed from Json file,
* if the version of the tool is given
* 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.
* @param toolVersionToCheck the {@link VersionIdentifier} 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 VersionIdentifier} the Version of the dependency with which the tool works correctly.
*/
private String findDependencyVersionFromJson(JsonNode toolVersions, String toolVersionToCheck) {
String requiredDependencyVersion = null;
private VersionIdentifier findDependencyVersionFromJson(JsonNode toolVersions, VersionIdentifier toolVersionToCheck) {

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

OuterLoop:
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();
// TODO: Add logic to handle MaxVersion if needed
break OuterLoop; // Stop the loop when a matching Java version is found
}
}
VersionIdentifier foundToolVersion = VersionIdentifier.of(versionKey);

if (toolVersionToCheck.getStart().compareVersion(foundToolVersion.getStart()).isGreater()) {
return null;
} else if (toolVersionToCheck.getStart().compareVersion(foundToolVersion.getStart()).isEqual()) {
return searchJsonFile(entry);
}
}

return requiredDependencyVersion;
return null;
}

/**
* Method to parse the versions because they normally contain dots, like 10.1.14 should be separated into
* 10, 1 and 14
* Method to search the Json file
*
* @param versionString the {@link String} of the Version in its normal form
* @return Array of integers with the integers contained in the Version String.
* @param entry the {@link Map.Entry} that contains the next entry in the Json file
* @return {@link VersionIdentifier} the Version of the dependency with which the tool works correctly.
*/
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;
}
private VersionIdentifier searchJsonFile(Map.Entry<String, JsonNode> entry) {

/**
* 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) {
String dependencyString = "dependency";
String MinVersionString = "MinVersion";
String MaxVersionString = "MaxVersion";

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;
JsonNode dependencies = entry.getValue();
for (JsonNode dependencyNode : dependencies) {
if (getDependencyName().equals(dependencyNode.get(dependencyString).asText())) {
return VersionIdentifier.of(dependencyNode.get(MinVersionString).asText());
// TODO: Add logic to handle MaxVersion if needed
}
}
return true;
return null;
}

}

0 comments on commit 96f807f

Please sign in to comment.