Skip to content

Commit

Permalink
#204: Non existing version installation (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvomiero authored Apr 16, 2024
1 parent 661ea6a commit 6228841
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 56 deletions.
68 changes: 28 additions & 40 deletions cli/src/main/java/com/devonfw/tools/ide/tool/ToolCommandlet.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package com.devonfw.tools.ide.tool;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;

import com.devonfw.tools.ide.commandlet.Commandlet;
import com.devonfw.tools.ide.common.Tag;
import com.devonfw.tools.ide.common.Tags;
Expand All @@ -19,6 +13,12 @@
import com.devonfw.tools.ide.property.StringListProperty;
import com.devonfw.tools.ide.version.VersionIdentifier;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;

/**
* {@link Commandlet} for a tool integrated into the IDE.
*/
Expand All @@ -39,8 +39,7 @@ public abstract class ToolCommandlet extends Commandlet implements Tags {
*
* @param context the {@link IdeContext}.
* @param tool the {@link #getName() tool name}.
* @param tags the {@link #getTags() tags} classifying the tool. Should be created via {@link Set#of(Object) Set.of}
* method.
* @param tags the {@link #getTags() tags} classifying the tool. Should be created via {@link Set#of(Object) Set.of} method.
*/
public ToolCommandlet(IdeContext context, String tool, Set<Tag> tags) {

Expand Down Expand Up @@ -93,9 +92,8 @@ public void run() {
* Ensures the tool is installed and then runs this tool with the given arguments.
*
* @param processMode see {@link ProcessMode}
* @param toolVersion the explicit version (pattern) to run. Typically {@code null} to ensure the configured version
* is installed and use that one. Otherwise, the specified version will be installed in the software repository
* without touching and IDE installation and used to run.
* @param toolVersion the explicit version (pattern) to run. Typically {@code null} to ensure the configured version is installed and use that one. Otherwise,
* the specified version will be installed in the software repository without touching and IDE installation and used to run.
* @param args the command-line arguments to run the tool.
*/
public void runTool(ProcessMode processMode, VersionIdentifier toolVersion, String... args) {
Expand All @@ -108,8 +106,7 @@ public void runTool(ProcessMode processMode, VersionIdentifier toolVersion, Stri
} else {
throw new UnsupportedOperationException("Not yet implemented!");
}
ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.WARNING).executable(binaryPath)
.addArgs(args);
ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.WARNING).executable(binaryPath).addArgs(args);

pc.run(processMode);
}
Expand All @@ -133,8 +130,7 @@ public String getEdition() {
}

/**
* @return the {@link #getName() tool} with its {@link #getEdition() edition}. The edition will be omitted if same as
* tool.
* @return the {@link #getName() tool} with its {@link #getEdition() edition}. The edition will be omitted if same as tool.
* @see #getToolWithEdition(String, String)
*/
protected final String getToolWithEdition() {
Expand All @@ -145,8 +141,7 @@ protected final String getToolWithEdition() {
/**
* @param tool the tool name.
* @param edition the edition.
* @return the {@link #getName() tool} with its {@link #getEdition() edition}. The edition will be omitted if same as
* tool.
* @return the {@link #getName() tool} with its {@link #getEdition() edition}. The edition will be omitted if same as tool.
*/
protected final static String getToolWithEdition(String tool, String edition) {

Expand All @@ -165,24 +160,20 @@ public VersionIdentifier getConfiguredVersion() {
}

/**
* Method to be called for {@link #install(boolean)} from dependent
* {@link com.devonfw.tools.ide.commandlet.Commandlet}s.
* Method to be called for {@link #install(boolean)} from dependent {@link com.devonfw.tools.ide.commandlet.Commandlet}s.
*
* @return {@code true} if the tool was newly installed, {@code false} if the tool was already installed before and
* nothing has changed.
* @return {@code true} if the tool was newly installed, {@code false} if the tool was already installed before and nothing has changed.
*/
public boolean install() {

return install(true);
}

/**
* Performs the installation of the {@link #getName() tool} managed by this
* {@link com.devonfw.tools.ide.commandlet.Commandlet}.
* Performs the installation of the {@link #getName() tool} managed by this {@link com.devonfw.tools.ide.commandlet.Commandlet}.
*
* @param silent - {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
* @return {@code true} if the tool was newly installed, {@code false} if the tool was already installed before and
* nothing has changed.
* @return {@code true} if the tool was newly installed, {@code false} if the tool was already installed before and nothing has changed.
*/
public boolean install(boolean silent) {

Expand All @@ -193,8 +184,7 @@ public boolean install(boolean silent) {
* Installs or updates the managed {@link #getName() tool}.
*
* @param silent - {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
* @return {@code true} if the tool was newly installed, {@code false} if the tool was already installed before and
* nothing has changed.
* @return {@code true} if the tool was newly installed, {@code false} if the tool was already installed before and nothing has changed.
*/
protected abstract boolean doInstall(boolean silent);

Expand Down Expand Up @@ -270,8 +260,8 @@ public String getInstalledEdition() {
}

/**
* @param toolPath the installation {@link Path} where to find currently installed tool. The name of the parent
* directory of the real path corresponding to the passed {@link Path path} must be the name of the edition.
* @param toolPath the installation {@link Path} where to find currently installed tool. The name of the parent directory of the real path corresponding to
* the passed {@link Path path} must be the name of the edition.
* @return the installed edition of this tool or {@code null} if not installed.
*/
public String getInstalledEdition(Path toolPath) {
Expand All @@ -287,10 +277,8 @@ public String getInstalledEdition(Path toolPath) {
}
return edition;
} catch (IOException e) {
throw new IllegalStateException("Couldn't determine the edition of " + getName()
+ " from the directory structure of its software path " + toolPath
+ ", assuming the name of the parent directory of the real path of the software path to be the edition "
+ "of the tool.", e);
throw new IllegalStateException("Couldn't determine the edition of " + getName() + " from the directory structure of its software path " + toolPath
+ ", assuming the name of the parent directory of the real path of the software path to be the edition " + "of the tool.", e);
}

}
Expand Down Expand Up @@ -342,9 +330,11 @@ public void setVersion(String version) {
*/
public void setVersion(VersionIdentifier version, boolean hint) {

String edition = getEdition();
this.context.getUrls().getVersionFolder(tool, edition, version); // CliException is thrown if the version is not existing

EnvironmentVariables variables = this.context.getVariables();
EnvironmentVariables settingsVariables = variables.getByType(EnvironmentVariablesType.SETTINGS);
String edition = getEdition();
String name = EnvironmentVariables.getToolVersionVariable(this.tool);
VersionIdentifier resolvedVersion = this.context.getUrls().getVersion(this.tool, edition, version);
if (version.isPattern()) {
Expand All @@ -355,9 +345,8 @@ public void setVersion(VersionIdentifier version, boolean hint) {
this.context.info("{}={} has been set in {}", name, version, settingsVariables.getSource());
EnvironmentVariables declaringVariables = variables.findVariable(name);
if ((declaringVariables != null) && (declaringVariables != settingsVariables)) {
this.context.warning(
"The variable {} is overridden in {}. Please remove the overridden declaration in order to make the change affect.",
name, declaringVariables.getSource());
this.context.warning("The variable {} is overridden in {}. Please remove the overridden declaration in order to make the change affect.", name,
declaringVariables.getSource());
}
if (hint) {
this.context.info("To install that version call the following command:");
Expand Down Expand Up @@ -400,9 +389,8 @@ public void setEdition(String edition, boolean hint) {
this.context.info("{}={} has been set in {}", name, edition, settingsVariables.getSource());
EnvironmentVariables declaringVariables = variables.findVariable(name);
if ((declaringVariables != null) && (declaringVariables != settingsVariables)) {
this.context.warning(
"The variable {} is overridden in {}. Please remove the overridden declaration in order to make the change affect.",
name, declaringVariables.getSource());
this.context.warning("The variable {} is overridden in {}. Please remove the overridden declaration in order to make the change affect.", name,
declaringVariables.getSource());
}
if (hint) {
this.context.info("To install that edition call the following command:");
Expand Down
32 changes: 16 additions & 16 deletions cli/src/main/java/com/devonfw/tools/ide/url/model/UrlMetadata.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package com.devonfw.tools.ide.url.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.devonfw.tools.ide.cli.CliException;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.url.model.folder.UrlEdition;
Expand All @@ -15,6 +8,13 @@
import com.devonfw.tools.ide.url.model.folder.UrlVersion;
import com.devonfw.tools.ide.version.VersionIdentifier;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Service to {@link #getEdition(String, String) load} an {@link UrlEdition} to get access to its versions.
*/
Expand Down Expand Up @@ -73,8 +73,7 @@ public List<String> getSortedEditions(String tool) {
/**
* @param tool the name of the {@link UrlTool}.
* @param edition the name of the {@link UrlEdition}.
* @return the {@link List} of {@link VersionIdentifier}s sorted descending so the latest version comes first and the
* oldest comes last.
* @return the {@link List} of {@link VersionIdentifier}s sorted descending so the latest version comes first and the oldest comes last.
*/
public List<VersionIdentifier> getSortedVersions(String tool, String edition) {

Expand All @@ -98,8 +97,8 @@ private List<VersionIdentifier> computeSortedVersions(String tool, String editio
/**
* @param tool the name of the {@link UrlTool}.
* @param edition the name of the {@link UrlEdition}.
* @param version the {@link VersionIdentifier} to match. May be a {@link VersionIdentifier#isPattern() pattern}, a
* specific version or {@code null} for the latest version.
* @param version the {@link VersionIdentifier} to match. May be a {@link VersionIdentifier#isPattern() pattern}, a specific version or {@code null} for the
* latest version.
* @return the latest matching {@link VersionIdentifier} for the given {@code tool} and {@code edition}.
*/
public VersionIdentifier getVersion(String tool, String edition, VersionIdentifier version) {
Expand All @@ -117,23 +116,24 @@ public VersionIdentifier getVersion(String tool, String edition, VersionIdentifi
return vi;
}
}
throw new CliException("Could not find any version matching '" + version + "' for tool '" + tool + "' - potentially there are " + versions.size()
+ " version(s) available in " + getEdition(tool, edition).getPath() + " but none matched!");
throw new CliException(
"Could not find any version matching '" + version + "' for tool '" + tool + "' - potentially there are " + versions.size() + " version(s) available in "
+ getEdition(tool, edition).getPath() + " but none matched!");
}

/**
* @param tool the name of the {@link UrlTool}.
* @param edition the name of the {@link UrlEdition}.
* @param version the {@link VersionIdentifier} to match. May be a {@link VersionIdentifier#isPattern() pattern}, a
* specific version or {@code null} for the latest version.
* @param version the {@link VersionIdentifier} to match. May be a {@link VersionIdentifier#isPattern() pattern}, a specific version or {@code null} for the
* latest version.
* @return the latest matching {@link UrlVersion} for the given {@code tool} and {@code edition}.
*/
public UrlVersion getVersionFolder(String tool, String edition, VersionIdentifier version) {

VersionIdentifier resolvedVersion = getVersion(tool, edition, version);
UrlVersion urlVersion = getEdition(tool, edition).getChild(resolvedVersion.toString());
if (urlVersion == null) {
throw new IllegalArgumentException("Version " + version + " for tool " + tool + " does not exist in edition " + edition + ".");
throw new CliException("Version " + version + " for tool " + tool + " does not exist in edition " + edition + ".");
}
return urlVersion;
}
Expand Down

0 comments on commit 6228841

Please sign in to comment.