Skip to content

Commit

Permalink
devonfw#898: constructive review and cleanup + fix of existing code
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Dec 20, 2024
1 parent 823a21b commit 1355887
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 146 deletions.
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,55 +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();
String installedEdition = commandlet.getInstalledEdition();
IdeSubLogger logger = this.context.level(IdeLogLevel.PROCESSABLE);
if (this.installed.isTrue() && !this.configured.isTrue()) { // get installed edition
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 if (this.installed.isTrue() && this.configured.isTrue()) { // get both configured and installed edition
logger.log(configuredEdition);
if (!configuredEdition.equals(installedEdition)) {
if (commandlet.getInstalledVersion() != null) {
logger.log(installedEdition);
} else {
logger.log("No installed edition detected");
}
}
} else { // get configured or installed depending on if the tool is installed or not
if (commandlet.getInstalledVersion() == null) {
logger.log(configuredEdition);
} else {
logger.log(installedEdition);
}
}
return commandlet.getConfiguredEdition();
}

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

IdeSubLogger logger = this.context.level(IdeLogLevel.PROCESSABLE);
if (installedEdition == null) {
logger.log("No installation of tool {} was found.", commandlet.getName());
} else {
logger.log("The installed edition for tool {} is {}", commandlet.getName(), installedEdition);
}
logger.log("The configured edition for tool {} is {}", toolName, configuredEdition);
logger.log("To install that edition call the following command:");
logger.log("ide install {}", toolName);
return commandlet.getInstalledEdition();
}
}
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,56 +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();
VersionIdentifier installedVersion = commandlet.getInstalledVersion();
IdeSubLogger logger = this.context.level(IdeLogLevel.PROCESSABLE);
if (this.installed.isTrue() && !this.configured.isTrue()) {// get installed version
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 if (this.installed.isTrue() && this.configured.isTrue()) {// get both configured and installed version
logger.log(configuredVersion.toString());
if (!configuredVersion.matches(installedVersion)) {
if (installedVersion != null) {
logger.log(installedVersion.toString());
} else {
logger.log("No installed version detected");
}
}
} else {
if (installedVersion == null) {
logger.log(configuredVersion.toString());
} else {
logger.log(installedVersion.toString());
}
}
return commandlet.getConfiguredVersion();
}

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

IdeSubLogger logger = this.context.level(IdeLogLevel.PROCESSABLE);
if (installedVersion == null) {
logger.log("No installation of tool {} was found.", commandlet.getName());
} else {
logger.log("The installed version for tool {} is {}", commandlet.getName(), installedVersion);
}
logger.log("The configured version for tool {} is {}", toolName, configuredVersion);
logger.log("To install that version call the following command:");
logger.log("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

0 comments on commit 1355887

Please sign in to comment.