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

#898 improved feedback messages #903

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This file documents all notable changes to https://github.com/devonfw/IDEasy[IDE

Release with new features and bugfixes:

* https://github.com/devonfw/IDEasy/issues/898[#898]: Improved feedback messages

The full list of changes for this release can be found in https://github.com/devonfw/IDEasy/milestone/18?closed=1[milestone 2025.01.001].

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

import java.util.Objects;

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 (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 that {} 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
Expand Up @@ -44,7 +44,11 @@ public void run() {
String plugin = this.plugin.getValue();

if (commandlet instanceof PluginBasedCommandlet cmd) {
cmd.uninstallPlugin(cmd.getPlugin(plugin));
if (cmd.uninstallPlugin(cmd.getPlugin(plugin)) == 0) {
context.info("Successfully uninstalled plugin " + plugin);
} else {
context.error("Could not uninstall plugin " + plugin + " because we could not find an installation");
}
} else {
context.warning("Tool {} does not support plugins.", tool.getName());
}
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 @@ -271,4 +271,12 @@ default void extract(Path archiveFile, Path targetDir, Consumer<Path> postExtrac
* @param filePath the {@link Path} to the file or folder.
*/
void touch(Path filePath);

/**
* @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 @@ -952,4 +952,17 @@ public void touch(Path filePath) {
}
}
}

@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
Loading