Skip to content

Commit

Permalink
devonfw#131: Changes after reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
aBega2000 committed Nov 27, 2023
1 parent 96f807f commit b222aa5
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 129 deletions.
164 changes: 164 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/tool/LocalToolCommandlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.repo.ToolRepository;
import com.devonfw.tools.ide.version.VersionIdentifier;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Set;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Iterator;

/**
* {@link ToolCommandlet} that is installed locally into the IDE.
*/
public abstract class LocalToolCommandlet extends ToolCommandlet {

public List<String> dependencies = new ArrayList<>();

/**
* The constructor.
*
Expand Down Expand Up @@ -63,6 +71,12 @@ protected boolean doInstall(boolean silent) {
// install configured version of our tool in the software repository if not already installed
ToolInstallation installation = installInRepo(configuredVersion);

if (Files.exists(getDependencyJsonPath())) {
installDependency();
} else {
this.context.info("No Dependencies file found");
}

// check if we already have this version installed (linked) locally in IDE_HOME/software
VersionIdentifier installedVersion = getInstalledVersion();
VersionIdentifier resolvedVersion = installation.resolvedVersion();
Expand Down Expand Up @@ -182,4 +196,154 @@ private boolean isInstalledVersion(VersionIdentifier expectedVersion, VersionIde
return false;
}

/**
* Method to get the Path of the dependencies Json file
*
* @return the {@link Path} of the dependencies file for the tool
*/
protected Path getDependencyJsonPath() {

Path path = this.context.getUrlsPath();
Path toolPath = path.resolve(getName()).resolve(getEdition());
return toolPath.resolve("dependencies.Json");
}

/**
* Method to search the List of version available in the ide and find the right version to install
*
* @param dependencyVersionNumberFound the {@link VersionIdentifier} of the dependency that was found that needs to be
* installed
* @param dependency the {@link String} of the dependency tool
*
* @return the {@link VersionIdentifier} of the dependency that is to be installed
*/
protected VersionIdentifier findDependencyVersionToInstall(VersionIdentifier dependencyVersionNumberFound,
String dependency) {

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

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

for (VersionIdentifier vi : versions) {
if (vi.compareVersion(dependencyVersionNumberFound).isGreater()) {
return vi;
}
}
return null;
}

/**
* Method to install the dependency
*/
protected void installDependency() {

JsonNode nodeInJson = readJson();
String dependencyString = "dependency";
String MinVersionString = "MinVersion";

try {
for (JsonNode node : nodeInJson) {
VersionIdentifier dependencyVersionNumberFound = VersionIdentifier.of(node.get(MinVersionString).asText());
String dependency = node.get(dependencyString).asText();
ToolCommandlet dependencyTool = this.context.getCommandletManager().getToolCommandlet(dependency);
VersionIdentifier dependencyVersionToInstall = findDependencyVersionToInstall(dependencyVersionNumberFound,
dependency);

String DefaultToolRepositoryID = this.context.getDefaultToolRepository().getId();
Path dependecyPath = this.context.getSoftwareRepositoryPath().resolve(DefaultToolRepositoryID)
.resolve(dependency).resolve(dependencyTool.getEdition()).resolve(dependencyVersionToInstall.toString());

if (Files.exists(dependecyPath)) {
this.context.info("The version {} of the dependency {} is already installed in repository",
dependencyTool.getInstalledVersion(dependecyPath), dependency);
} else {
dependencyTool.setVersion(dependencyVersionToInstall, false);
this.context.info("The version {} of the dependency {} is being installed", dependencyVersionToInstall,
dependency);
LocalToolCommandlet dependencyLocal = (LocalToolCommandlet) dependencyTool;
dependencyLocal.installInRepo(dependencyVersionToInstall);
this.context.info("The version {} of the dependency {} was successfully installed",
dependencyVersionToInstall, dependency);
}
}
} catch (NullPointerException e) {
this.context.error("An error occurred: {}", e);
}

}

/**
* Method to read the Json file
*
* @return the {@link JsonNode} of the searched version, which contains all the dependencies and their versions
*/
private JsonNode readJson() {

try {
ObjectMapper objectMapper = new ObjectMapper();
Path jsonFilePath = getDependencyJsonPath();

JsonNode toolVersions = objectMapper.readTree(jsonFilePath.toFile());
Map.Entry<String, JsonNode> entry = findDependenciesFromJson(toolVersions, getInstalledVersion());

if (entry == null) {
this.context.error("The entry with the specified version was not found in the Json file");
} else
return entry.getValue();

} catch (IOException e) {
this.context.error("Error: {}", e);
}
return null;
}

/**
* Method to find the dependency from the Json file
*
* @param toolVersions the {@link JsonNode} of the tool versions
* @param toolVersionToCheck the {@link VersionIdentifier} of the tool to be searched in the Json file
*
* @return the {@link Map} of the searched version
*/
private Map.Entry<String, JsonNode> findDependenciesFromJson(JsonNode toolVersions,
VersionIdentifier toolVersionToCheck) {

// Iterate through the fields of the Json file
Iterator<Map.Entry<String, JsonNode>> fields = toolVersions.fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> entry = fields.next();
String versionKey = entry.getKey();
VersionIdentifier foundToolVersion = VersionIdentifier.of(versionKey);

if (toolVersionToCheck.getStart().compareVersion(foundToolVersion.getStart()).isGreater()) {
return null;
} else if (toolVersionToCheck.getStart().compareVersion(foundToolVersion.getStart()).isEqual()) {
this.dependencies.addAll(searchDependencies(entry.getValue()));
return entry;
}
}
return null;
}

/**
* Method to find the names of the dependencies from the Json file
*
* @param dependenciesNode the {@link JsonNode} of the tool version
*
* @return the {@link List} of the dependencies as Strings
*/
private List<String> searchDependencies(JsonNode dependenciesNode) {

List<String> dependenciesNames = new ArrayList<>();

for (JsonNode dependencyNode : dependenciesNode) {
String dependency = dependencyNode.get("dependency").asText();
if (!dependenciesNames.contains(dependency)) {
dependenciesNames.add(dependency);
}
}

return dependenciesNames;
}

}
129 changes: 0 additions & 129 deletions cli/src/main/java/com/devonfw/tools/ide/tool/ToolCommandlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
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 @@ -24,8 +22,6 @@
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 @@ -392,129 +388,4 @@ 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 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 VersionIdentifier getDependencyVersion(VersionIdentifier toolVersionToCheck) {

try {
ObjectMapper objectMapper = new ObjectMapper();
Path jsonFilePath = Paths.get(getDependencyJsonPath());

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

VersionIdentifier requiredDependencyVersion = findDependencyVersionFromJson(toolVersions, toolVersionToCheck);

if (requiredDependencyVersion != null) {
this.context.info("The tool {} in version {} requires at least {} in version {} as a dependency", getName(),
toolVersionToCheck, getDependencyName(), requiredDependencyVersion);
return requiredDependencyVersion;
} else {
this.context.error("No specific {} version requirement found for {} in version {}", getDependencyName(),
getName(), toolVersionToCheck);
return null;
}
} catch (Exception e) {
this.context.error("An error occurred: ", e);
}
return null;
}

/**
* 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 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(VersionIdentifier dependencyVersionNumberFound) {

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

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

for (VersionIdentifier vi : versions) {
if (vi.compareVersion(dependencyVersionNumberFound).isGreater()) {
return vi;
}
}
return null;
}

/**
* 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 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 VersionIdentifier findDependencyVersionFromJson(JsonNode toolVersions, VersionIdentifier toolVersionToCheck) {

// Iterate through the fields of the Json file
Iterator<Map.Entry<String, JsonNode>> fields = toolVersions.fields();

while (fields.hasNext()) {

Map.Entry<String, JsonNode> entry = fields.next();
String versionKey = entry.getKey();
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 null;
}

/**
* Method to search the Json file
*
* @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 VersionIdentifier searchJsonFile(Map.Entry<String, JsonNode> entry) {

String dependencyString = "dependency";
String MinVersionString = "MinVersion";
String MaxVersionString = "MaxVersion";

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 null;
}

}

0 comments on commit b222aa5

Please sign in to comment.