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 edition #194

Merged
merged 20 commits into from
Feb 12, 2024
Merged
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 @@ -60,6 +60,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,53 @@
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) {
this.context.info("The configured edition for tool {} is {}", commandlet.getName(), commandlet.getEdition());
this.context.info("To install that edition call the following command:");
this.context.info("ide install {}", commandlet.getName());
return;
}
String installedEdition = commandlet.getInstalledEdition();
this.context.info(installedEdition);
}
}
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;
}
}
90 changes: 90 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 @@ -345,6 +345,51 @@ 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)) {
edition = getEdition();
}
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);
}

}

/**
* 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 @@ -404,4 +449,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) {

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) {

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

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

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.devonfw.tools.ide.url.model;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
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 java.util.Objects;
import java.util.stream.Stream;

import com.devonfw.tools.ide.cli.CliException;
import com.devonfw.tools.ide.context.IdeContext;
Expand Down Expand Up @@ -52,6 +55,25 @@ public UrlEdition getEdition(String tool, String edition) {
return urlEdition;
}

/**
* @param tool the name of the {@link UrlTool}.
* @return the sorted {@link List} of {@link String editions} .
*/
public List<String> getSortedEditions(String tool) {

List<String> list = new ArrayList<>();
try {
for (UrlEdition urlEdition : this.repository.getChild(tool).getChildren()) {
list.add(urlEdition.getName());
}
} catch (NullPointerException e) {
this.context.warning("Can't get sorted editions for tool {} because it does not exist in {}.", tool,
this.repository.getPath());
}
Collections.sort(list);
return Collections.unmodifiableList(list);
}

/**
* @param tool the name of the {@link UrlTool}.
* @param edition the name of the {@link UrlEdition}.
Expand Down
Loading
Loading