Skip to content

Commit

Permalink
#898: Improved output of get-version/edition and uninstall/-plugin (#903
Browse files Browse the repository at this point in the history
)
  • Loading branch information
WorkingAmeise authored Jan 20, 2025
1 parent 9b34319 commit 59b245f
Show file tree
Hide file tree
Showing 24 changed files with 392 additions and 225 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Release with new features and bugfixes:
* https://github.com/devonfw/IDEasy/issues/916[#916]: download is missing status code error handling
* https://github.com/devonfw/IDEasy/issues/757[#757]: Support to allow settings in code repository
* https://github.com/devonfw/IDEasy/issues/826[#826]: Fix git settings check when settings folder is empty
* https://github.com/devonfw/IDEasy/issues/898[#898]: Improved output of get-version/edition and uninstall/-plugin
* https://github.com/devonfw/IDEasy/issues/894[#894]: Fix ide.bat printing for initialization and error output
* https://github.com/devonfw/IDEasy/issues/759[#759]: Add UpgradeSettingsCommandlet for the upgrade of legacy devonfw-ide settings to IDEasy
* https://github.com/devonfw/IDEasy/issues/498[#498]: Improvement of XML merger: resolve before merge
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.devonfw.tools.ide.commandlet;

import java.util.Objects;

import com.devonfw.tools.ide.cli.CliException;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.log.IdeSubLogger;
import com.devonfw.tools.ide.property.FlagProperty;
import com.devonfw.tools.ide.property.ToolProperty;
import com.devonfw.tools.ide.tool.ToolCommandlet;

/**
* An internal {@link Commandlet} to get the installed version for a tool.
*
* @see ToolCommandlet#getInstalledVersion()
*/
public abstract class AbstractVersionOrEditionGetCommandlet extends Commandlet {

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

/** Flag to get the configured version. */
public final FlagProperty configured;

/** Flag to get the installed version. */
public final FlagProperty installed;

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

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

@Override
public boolean isProcessableOutput() {

return true;
}

/**
* @return the property to get (e.g. "version" or "edition").
*/
protected abstract String getPropertyToGet();

/**
* @param commandlet the {@link ToolCommandlet} to get the value from.
* @return the configured value.
* @see ToolCommandlet#getConfiguredVersion()
* @see ToolCommandlet#getConfiguredEdition()
*/
protected abstract Object getConfiguredValue(ToolCommandlet commandlet);

/**
* @param commandlet the {@link ToolCommandlet} to get the value from.
* @return the installed value or {@code null} if the tool is not installed.
* @see ToolCommandlet#getInstalledVersion()
* @see ToolCommandlet#getInstalledEdition()
*/
protected abstract Object getInstalledValue(ToolCommandlet commandlet);

@Override
public void run() {

ToolCommandlet commandlet = this.tool.getValue();
IdeSubLogger logger = this.context.level(IdeLogLevel.PROCESSABLE);
Object configuredValue = getConfiguredValue(commandlet);
Object installedValue = getInstalledValue(commandlet);
boolean getInstalledValue = this.installed.isTrue();
boolean getConfiguredValue = this.configured.isTrue();
if (installedValue == null && getInstalledValue && !getConfiguredValue) {
throw new CliException("Tool " + commandlet + " is not installed.", 1);
}
if (getInstalledValue == getConfiguredValue) {
if (getInstalledValue) { // both --configured and --installed
logToolInfo(logger, commandlet, configuredValue, installedValue);
} else if (this.context.debug().isEnabled()) {
logToolInfo(logger, commandlet, configuredValue, installedValue);
} else {
if (installedValue == null) {
logger.log(configuredValue.toString());
} else {
logger.log(installedValue.toString());
}
}
} else {
if (getInstalledValue) {
if (installedValue == null) {
logToolInfo(logger, commandlet, configuredValue, null);
} else {
logger.log(installedValue.toString());
}
} else {
logger.log(configuredValue.toString());
}
}
}

private void logToolInfo(IdeSubLogger logger, ToolCommandlet commandlet, Object configuredValue, Object installedValue) {

String property = getPropertyToGet();
String toolName = commandlet.getName();
if (installedValue == null) {
logger.log("No installation of tool {} was found.", toolName);
} else {
logger.log("The installed {} for tool {} is {}", property, toolName, installedValue);
}
logger.log("The configured {} for tool {} is {}", property, toolName, configuredValue);
if (!Objects.equals(configuredValue, installedValue)) {
logger.log("To install the configured {} call the following command:", property);
logger.log("ide install {}", toolName);
}
}

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

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.log.IdeSubLogger;
import com.devonfw.tools.ide.property.FlagProperty;
import com.devonfw.tools.ide.property.ToolProperty;
import com.devonfw.tools.ide.tool.ToolCommandlet;

/**
* 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;

/** Flag to get the configured version. */
public final FlagProperty configured;

/** Flag to get the installed version. */
public final FlagProperty installed;
public class EditionGetCommandlet extends AbstractVersionOrEditionGetCommandlet {

/**
* The constructor.
Expand All @@ -31,10 +18,6 @@ public class EditionGetCommandlet extends Commandlet {
public EditionGetCommandlet(IdeContext context) {

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

@Override
Expand All @@ -44,47 +27,20 @@ public String getName() {
}

@Override
public boolean isProcessableOutput() {
protected String getPropertyToGet() {

return true;
return "edition";
}

@Override
public void run() {
protected Object getConfiguredValue(ToolCommandlet commandlet) {

ToolCommandlet commandlet = this.tool.getValue();
String configuredEdition = commandlet.getConfiguredEdition();
IdeSubLogger logger = this.context.level(IdeLogLevel.PROCESSABLE);

if (this.installed.isTrue() && !this.configured.isTrue()) { // get installed edition
String installedEdition = commandlet.getInstalledEdition();
if (commandlet.getInstalledVersion() == null) {
// note: getInstalledEdition() will fallback to configured edition and not return null, therefore we use getInstalledVersion()
toolInstallInfo(commandlet.getName(), configuredEdition, null, commandlet);
} else {
logger.log(installedEdition);
}
} else if (!this.installed.isTrue() && this.configured.isTrue()) { // get configured edition
logger.log(configuredEdition);
} else { // get both configured and installed edition
String installedEdition = commandlet.getInstalledEdition();
if (configuredEdition.equals(installedEdition)) {
logger.log(installedEdition);
} else {
toolInstallInfo(commandlet.getName(), configuredEdition, installedEdition, commandlet);
}
}
return commandlet.getConfiguredEdition();
}

private void toolInstallInfo(String toolName, String configuredEdition, String installedEdition, ToolCommandlet commandlet) {
@Override
protected Object getInstalledValue(ToolCommandlet commandlet) {

if (installedEdition == null) {
this.context.warning("No installation of tool {} was found.", commandlet.getName());
} else {
this.context.info("The installed edition for tool {} is {}", commandlet.getName(), installedEdition);
}
this.context.info("The configured edition for tool {} is {}", toolName, configuredEdition);
this.context.info("To install that edition call the following command:");
this.context.info("ide install {}", toolName);
return commandlet.getInstalledEdition();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ public void run() {

for (int i = 0; i < this.tools.getValueCount(); i++) {
ToolCommandlet toolCommandlet = this.tools.getValue(i);

toolCommandlet.uninstall();
if (toolCommandlet.getInstalledVersion() != null) {
toolCommandlet.uninstall();
} else {
this.context.warning("Couldn't uninstall " + toolCommandlet.getName() + " because we could not find an installation");
}

}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
package com.devonfw.tools.ide.commandlet;

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.log.IdeSubLogger;
import com.devonfw.tools.ide.property.FlagProperty;
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 get the installed version for a tool.
*
* @see ToolCommandlet#getInstalledVersion()
*/
public class VersionGetCommandlet extends Commandlet {

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

/** Flag to get the configured version. */
public final FlagProperty configured;

/** Flag to get the installed version. */
public final FlagProperty installed;
public class VersionGetCommandlet extends AbstractVersionOrEditionGetCommandlet {

/**
* The constructor.
Expand All @@ -32,10 +18,6 @@ public class VersionGetCommandlet extends Commandlet {
public VersionGetCommandlet(IdeContext context) {

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

@Override
Expand All @@ -45,47 +27,20 @@ public String getName() {
}

@Override
public boolean isProcessableOutput() {
protected String getPropertyToGet() {

return true;
return "version";
}

@Override
public void run() {
protected Object getConfiguredValue(ToolCommandlet commandlet) {

ToolCommandlet commandlet = this.tool.getValue();
VersionIdentifier configuredVersion = commandlet.getConfiguredVersion();
IdeSubLogger logger = this.context.level(IdeLogLevel.PROCESSABLE);
if (this.installed.isTrue() && !this.configured.isTrue()) {// get installed version
VersionIdentifier installedVersion = commandlet.getInstalledVersion();
if (installedVersion == null) {
toolInstallInfo(commandlet.getName(), configuredVersion, null, commandlet);
} else {
logger.log(installedVersion.toString());
}
} else if (!this.installed.isTrue() && this.configured.isTrue()) {// get configured version
logger.log(configuredVersion.toString());
} else { // get both configured and installed version
VersionIdentifier installedVersion = commandlet.getInstalledVersion();
if (configuredVersion.matches(installedVersion)) {
logger.log(installedVersion.toString());
} else {
toolInstallInfo(commandlet.getName(), configuredVersion, installedVersion, commandlet);
}
}
return commandlet.getConfiguredVersion();
}

private void toolInstallInfo(String toolName, VersionIdentifier configuredVersion, VersionIdentifier installedVersion, ToolCommandlet commandlet) {

if (installedVersion == null) {
this.context.info("No installation of tool {} was found.", commandlet.getName());
} else {
this.context.info("The installed version for tool {} is {}", commandlet.getName(), installedVersion);
}
this.context.info("The configured version for tool {} is {}", toolName, configuredVersion);
this.context.info("To install that version call the following command:");
this.context.info("ide install {}", toolName);
@Override
protected Object getInstalledValue(ToolCommandlet commandlet) {

return commandlet.getInstalledVersion();
}

}
8 changes: 8 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/io/FileAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,12 @@ default void makeExecutable(Path file) {
* @param file the {@link Path} to the file or folder.
*/
void touch(Path file);

/**
* @param file the {@link Path} to the file to read.
* @return the content of the specified file (in UTF-8 encoding).
* @see java.nio.file.Files#readString(Path)
*/
String readFileContent(Path file);

}
13 changes: 13 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -986,4 +986,17 @@ public void touch(Path file) {
}
}
}

@Override
public String readFileContent(Path file) {

this.context.trace("Reading content of file from {}", file);
try {
String content = Files.readString(file);
this.context.trace("Read content of file {} as {}", file, content);
return content;
} catch (IOException e) {
throw new IllegalStateException("Failed to read file " + file, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public Path findLinkDir(Path rootDir, String tool) {
*/
public Path findRootToolPath(ToolCommandlet commandlet, IdeContext context) {
return context.getSoftwareRepositoryPath().resolve(ToolRepository.ID_DEFAULT).resolve(commandlet.getName())
.resolve(commandlet.getInstalledEdition().toString())
.resolve(commandlet.getInstalledEdition())
.resolve(commandlet.getInstalledVersion().toString());
}

Expand Down
Loading

0 comments on commit 59b245f

Please sign in to comment.