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

#180: commandlet to set tool edition #181

Closed
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
Expand Up @@ -56,6 +56,9 @@ public CommandletManagerImpl(IdeContext context) {
add(new VersionSetCommandlet(context));
add(new VersionGetCommandlet(context));
add(new VersionListCommandlet(context));
add(new EditionGetCommandlet(context));
add(new EditionSetCommandlet(context));
add(new EditionListCommandlet(context));
add(new VersionCommandlet(context));
add(new Gh(context));
add(new Helm(context));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.devonfw.tools.ide.commandlet;

import com.devonfw.tools.ide.cli.CliException;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.property.ToolProperty;
import com.devonfw.tools.ide.tool.ToolCommandlet;
import com.devonfw.tools.ide.version.VersionIdentifier;

import static com.devonfw.tools.ide.process.ProcessResult.TOOL_NOT_INSTALLED;

/**
* An internal {@link Commandlet} to get the installed edition for a tool.
*
* @see ToolCommandlet#getInstalledEdition()
*/
public class EditionGetCommandlet extends Commandlet {

/** The tool to get the edition of. */
public final ToolProperty tool;

/**
* The constructor.
*
* @param context the {@link IdeContext}.
*/
public EditionGetCommandlet(IdeContext context) {

super(context);
addKeyword(getName());
this.tool = add(new ToolProperty("", true, "tool"));
}

@Override
public String getName() {

return "get-edition";
}

@Override
public void run() {

ToolCommandlet commandlet = this.tool.getValue();
VersionIdentifier installedVersion = commandlet.getInstalledVersion();
if (installedVersion == null) {
throw new CliException("Tool " + commandlet.getName() + " is not installed!", TOOL_NOT_INSTALLED);
Copy link
Member

Choose a reason for hiding this comment

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

IMHO it would make much more sense to print the configured edition instead of failing.

Copy link
Contributor

Choose a reason for hiding this comment

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

Done, adjusted the unit test accordingly. See #194

}

try {
String installedEdition = commandlet.getInstalledEdition();
this.context.info(installedEdition);
} catch (IllegalStateException e) {
String configuredEdition = this.context.getVariables().getToolEdition(getName());
this.context.info("The configured edition for tool {} is {}.", getName(), configuredEdition);

}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.devonfw.tools.ide.commandlet;

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.property.ToolProperty;
import com.devonfw.tools.ide.tool.ToolCommandlet;

/**
* An internal {@link Commandlet} to list editions for a tool.
*
* @see ToolCommandlet#listEditions()
*/
public class EditionListCommandlet extends Commandlet {

/** The tool to list the editions of. */
public final ToolProperty tool;

/**
* The constructor.
*
* @param context the {@link IdeContext}.
*/
public EditionListCommandlet(IdeContext context) {

super(context);
addKeyword(getName());
this.tool = add(new ToolProperty("", true, "tool"));
}

@Override
public String getName() {

return "list-editions";
}

@Override
public void run() {

ToolCommandlet commandlet = this.tool.getValue();
commandlet.listEditions();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.devonfw.tools.ide.commandlet;

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.property.EditionProperty;
import com.devonfw.tools.ide.property.ToolProperty;
import com.devonfw.tools.ide.tool.ToolCommandlet;

/**
* An internal {@link Commandlet} to set a tool edition.
*/
public class EditionSetCommandlet extends Commandlet {

/** The tool to set the edition of. */
public final ToolProperty tool;

/** The edition to set. */
public final EditionProperty edition;

/**
* The constructor.
*
* @param context the {@link IdeContext}.
*/
public EditionSetCommandlet(IdeContext context) {

super(context);
addKeyword(getName());
this.tool = add(new ToolProperty("", true, "tool"));
this.edition = add(new EditionProperty("", true, "edition"));
}

@Override
public String getName() {

return "set-edition";
}

@Override
public void run() {

ToolCommandlet commandlet = this.tool.getValue();
String edition = this.edition.getValue();

commandlet.setEdition(edition);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import com.devonfw.tools.ide.version.VersionIdentifier;

/**
* An internal {@link Commandlet} to set a tool version.
* An internal {@link Commandlet} to get the installed version for a tool.
*
* @see ToolCommandlet#setVersion(VersionIdentifier, boolean)
* @see ToolCommandlet#getInstalledVersion()
*/
public class VersionGetCommandlet extends Commandlet {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.property.ToolProperty;
import com.devonfw.tools.ide.tool.ToolCommandlet;
import com.devonfw.tools.ide.version.VersionIdentifier;

/**
* An internal {@link Commandlet} to set a tool version.
* An internal {@link Commandlet} to list versions for a tool.
*
* @see ToolCommandlet#setVersion(VersionIdentifier, boolean)
* @see ToolCommandlet#listVersions())
*/
public class VersionListCommandlet extends Commandlet {

Expand All @@ -30,7 +29,7 @@ public VersionListCommandlet(IdeContext context) {
@Override
public String getName() {

return "list-version";
return "list-versions";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,13 @@ static String getToolVersionVariable(String tool) {
return tool.toUpperCase(Locale.ROOT) + "_VERSION";
}

/**
* @param tool the name of the tool.
* @return the name of the edition variable.
*/
static String getToolEditionVariable(String tool) {

return tool.toUpperCase(Locale.ROOT) + "_EDITION";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.devonfw.tools.ide.property;

import com.devonfw.tools.ide.context.IdeContext;

import java.util.function.Consumer;

public class EditionProperty extends Property<String> {

/**
* The constructor.
*
* @param name the {@link #getName() property name}.
* @param required the {@link #isRequired() required flag}.
* @param alias the {@link #getAlias() property alias}.
*/
public EditionProperty(String name, boolean required, String alias) {

this(name, required, alias, null);
}

/**
* The constructor.
*
* @param name the {@link #getName() property name}.
* @param required the {@link #isRequired() required flag}.
* @param alias the {@link #getAlias() property alias}.
* @param validator the {@link Consumer} used to {@link #validate() validate} the {@link #getValue() value}.
*/
public EditionProperty(String name, boolean required, String alias, Consumer<String> validator) {

super(name, required, alias, validator);
}

@Override
public Class<String> getValueType() {

return String.class;
}

@Override
public String parse(String valueAsString, IdeContext context) {

return valueAsString;
}
}
91 changes: 91 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/tool/ToolCommandlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,52 @@ protected VersionIdentifier getInstalledVersion(Path toolPath) {
}
}

/**
* @return the installed edition of this tool or {@code null} if not installed.
*/
public String getInstalledEdition() {

return getInstalledEdition(this.context.getSoftwarePath().resolve(getName()));
}

/**
* @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) {

if (!Files.isDirectory(toolPath)) {
this.context.debug("Tool {} not installed in {}", getName(), toolPath);
return null;
}
try {
String edition = toolPath.toRealPath().getParent().getFileName().toString();
if (!this.context.getUrls().getSortedEditions(getName()).contains(edition)) {
this.context.warning("The determined edition \"{}\" of tool {} is not among the editions provided by IDEasy",
edition, getName());
}
return edition;
Comment on lines +353 to +358
Copy link
Member

Choose a reason for hiding this comment

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

If the tool was installed via devonfw-ide this warning will be printed and the printed edition will be software.
IMHO this behavior does not really make sense.
In such case as fallback we could simply print the configured edition what is still better than printing software.

Copy link
Contributor

Choose a reason for hiding this comment

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

Done. See #194

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

}

/**
* List the available editions of this tool.
*/
public void listEditions() {

List<String> editions = this.context.getUrls().getSortedEditions(getName());
for (String edition : editions) {
this.context.info(edition);
}
}

/**
* List the available versions of this tool.
*/
Expand Down Expand Up @@ -389,4 +435,49 @@ public void setVersion(VersionIdentifier version, boolean hint) {
}
}

/**
* Sets the tool edition in the environment variable configuration file.
*
* @param edition the edition to set.
*/
public void setEdition(String edition) {
MattesMrzik marked this conversation as resolved.
Show resolved Hide resolved

if ((edition == null) || edition.isBlank()) {
throw new IllegalStateException("Edition has to be specified!");
}

if (!Files.exists(this.context.getUrls().getEdition(getName(), edition).getPath())) {
this.context.warning("Edition {} seems to be invalid", edition);

}
setEdition(edition, true);
}

/**
* Sets the tool edition in the environment variable configuration file.
*
* @param edition the edition to set
* @param hint - {@code true} to print the installation hint, {@code false} otherwise.
*/
public void setEdition(String edition, boolean hint) {

EnvironmentVariables variables = this.context.getVariables();
EnvironmentVariables settingsVariables = variables.getByType(EnvironmentVariablesType.SETTINGS);
String name = EnvironmentVariables.getToolEditionVariable(this.tool);
settingsVariables.set(name, edition, false);
settingsVariables.save();

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());
}
if (hint) {
this.context.info("To install that edition call the following command:");
this.context.info("ide install {}", this.tool);
}
}

}
Loading
Loading