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 1 commit
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
Prev Previous commit
Next Next commit
#180: implemented edition get set and list commandlet
  • Loading branch information
MattesMrzik committed Jan 12, 2024
commit 13f3da2d58a4a6323222280e7783dba26f2c2ff5
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ private 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 Gh(context));
add(new Helm(context));
add(new Java(context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import com.devonfw.tools.ide.version.VersionIdentifier;

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

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

/**
Expand All @@ -37,15 +37,25 @@ public String getName() {
@Override
public void run() {

// i dont want to get the set edition but from the tool which is installed.

// this.context.getVariables().getToolEdition(this.tool.getName());
// String edition = commandlet.getEdition();
// VersionIdentifier installedVersion = commandlet.getInstalledVersion();
// if (installedVersion == null) {
// throw new CliException("Tool " + commandlet.getName() + " is not installed!", 4);
// }
// this.context.info(installedVersion.toString());
ToolCommandlet commandlet = this.tool.getValue();
VersionIdentifier installedVersion = commandlet.getInstalledVersion();
if (installedVersion == null) {
throw new CliException("Tool " + commandlet.getName() + " is not installed!", 4);
}

String installedEdition = commandlet.getInstalledEdition();

if (installedEdition == null) {
this.context.warning("Couldn't get edition of tool {}.", getName());
String configuredEdition = this.context.getVariables().getToolEdition(getName());
if (configuredEdition == null) {
this.context.info("No edition is configured for tool {}.", getName());
} else {
this.context.info("Configured edition for tool {} is {}.", getName(), configuredEdition);
}
} else {
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
Expand Up @@ -3,16 +3,14 @@
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.property.VersionProperty;
import com.devonfw.tools.ide.tool.ToolCommandlet;
import com.devonfw.tools.ide.version.VersionIdentifier;

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

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

/** The edition to set. */
Expand Down Expand Up @@ -43,7 +41,7 @@ public void run() {
ToolCommandlet commandlet = this.tool.getValue();
String edition = this.edition.getValue();

commandlet.setEdition(edition, true);
commandlet.setEdition(edition);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,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 @@ -106,9 +106,6 @@ public interface IdeContext extends IdeLogger {
/** The file where the installed software version is written to as plain text. */
String FILE_LEGACY_SOFTWARE_VERSION = ".devon.software.version";

/** The file where the installed software edition is written to as plain text. */
String FILE_SOFTWARE_EDITION = ".ide.software.edition";

/** The file extension for a {@link java.util.Properties} file. */
String EXT_PROPERTIES = ".properties";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public LocalToolCommandlet(IdeContext context, String tool, Set<String> tags) {
super(context, tool, tags);
}


/**
* @return the {@link Path} where the tool is located (installed).
*/
Expand Down Expand Up @@ -66,7 +65,9 @@ protected boolean doInstall(boolean silent) {
// check if we already have this version installed (linked) locally in IDE_HOME/software
VersionIdentifier installedVersion = getInstalledVersion();
VersionIdentifier resolvedVersion = installation.resolvedVersion();
if (isInstalledVersion(resolvedVersion, installedVersion, silent)) {
// check if we already have this edition installed (linked) locally in IDE_HOME/software
String installedEdition = getInstalledEdition();
if (isInstalledVersion(resolvedVersion, installedVersion, silent) && installedEdition.equals(getEdition())) {
return false;
}
// we need to link the version or update the link.
Expand Down Expand Up @@ -178,6 +179,7 @@ private boolean isInstalledVersion(VersionIdentifier expectedVersion, VersionIde
if (silent) {
level = IdeLogLevel.DEBUG;
}
// TODO this prints twice since installInRepo also prints this message
this.context.level(level).log("Version {} of tool {} is already installed", installedVersion,
getToolWithEdition());
return true;
Expand Down
69 changes: 67 additions & 2 deletions cli/src/main/java/com/devonfw/tools/ide/tool/ToolCommandlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,47 @@ 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();
return edition;
} catch (IOException e) {
this.context.debug("When calling getInstalledEdition() for tool " + getName()
+ " calling toRealPath().getParent() on " + toolPath + " failed. Returning null.");
return null;
}

}

/**
* 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 @@ -388,20 +429,44 @@ public void setVersion(VersionIdentifier version, boolean hint) {
}
}

public void setEdition(String edition) {

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

/**
* TODO
*
* @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){
public void setEdition(String edition, boolean hint) {

//TODO if edition is set to something then check if the current set version exists also for this 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<>();
Path path = this.repository.getChild(tool).getPath();
if (Files.isDirectory(path)) {
try (Stream<Path> childStream = Files.list(path)) {
childStream.forEach(c -> list.add(c.getFileName().toString()));
} catch (IOException e) {
throw new IllegalStateException("Failed to list children of directory " + path, e);
}
}
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