Skip to content

Commit

Permalink
#410: Refactor get version and edition (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
slskiba authored Jun 27, 2024
1 parent ef8dad8 commit 0147431
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
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.process.ProcessResult;
import com.devonfw.tools.ide.property.ToolProperty;
import com.devonfw.tools.ide.tool.ToolCommandlet;
import com.devonfw.tools.ide.version.VersionIdentifier;
Expand Down Expand Up @@ -41,7 +39,11 @@ public void run() {
ToolCommandlet commandlet = this.tool.getValue();
VersionIdentifier installedVersion = commandlet.getInstalledVersion();
if (installedVersion == null) {
throw new CliException("Tool " + commandlet.getName() + " is not installed!", ProcessResult.TOOL_NOT_INSTALLED);
this.context.info("The configured version for tool {} is {}", commandlet.getName(),
commandlet.getConfiguredVersion());
this.context.info("To install that version call the following command:");
this.context.info("ide install {}", commandlet.getName());
return;
}
this.context.info(installedVersion.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public abstract class GlobalToolCommandlet extends ToolCommandlet {
*
* @param context the {@link IdeContext}.
* @param tool the {@link #getName() tool name}.
* @param tags the {@link #getTags() tags} classifying the tool. Should be created via {@link Set#of(Object) Set.of} method.
* @param tags the {@link #getTags() tags} classifying the tool. Should be created via
* {@link Set#of(Object) Set.of} method.
*/
public GlobalToolCommandlet(IdeContext context, String tool, Set<Tag> tags) {

Expand Down Expand Up @@ -146,6 +147,20 @@ protected boolean doInstall(boolean silent) {
return true;
}

@Override
public VersionIdentifier getInstalledVersion() {
//TODO: handle "get-version <globaltool>"
this.context.error("Couldn't get installed version of " + this.getName());
return null;
}

@Override
public String getInstalledEdition() {
//TODO: handle "get-edition <globaltool>"
this.context.error("Couldn't get installed edition of " + this.getName());
return null;
}

@Override
public void uninstall() {
//TODO: handle "uninstall <globaltool>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,73 @@ protected void postExtract(Path extractedDir) {

}

@Override
public VersionIdentifier getInstalledVersion() {

return getInstalledVersion(this.context.getSoftwarePath().resolve(getName()));
}

/**
* @param toolPath the installation {@link Path} where to find the version file.
* @return the currently installed {@link VersionIdentifier version} of this tool or {@code null} if not installed.
*/
protected VersionIdentifier getInstalledVersion(Path toolPath) {

if (!Files.isDirectory(toolPath)) {
this.context.debug("Tool {} not installed in {}", getName(), toolPath);
return null;
}
Path toolVersionFile = toolPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
if (!Files.exists(toolVersionFile)) {
Path legacyToolVersionFile = toolPath.resolve(IdeContext.FILE_LEGACY_SOFTWARE_VERSION);
if (Files.exists(legacyToolVersionFile)) {
toolVersionFile = legacyToolVersionFile;
} else {
this.context.warning("Tool {} is missing version file in {}", getName(), toolVersionFile);
return null;
}
}
try {
String version = Files.readString(toolVersionFile).trim();
return VersionIdentifier.of(version);
} catch (IOException e) {
throw new IllegalStateException("Failed to read file " + toolVersionFile, e);
}
}

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

}

public void uninstall() {

try {
Expand Down
63 changes: 2 additions & 61 deletions cli/src/main/java/com/devonfw/tools/ide/tool/ToolCommandlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.devonfw.tools.ide.property.StringProperty;
import com.devonfw.tools.ide.version.VersionIdentifier;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
Expand Down Expand Up @@ -218,70 +217,12 @@ protected MacOsHelper getMacOsHelper() {
/**
* @return the currently installed {@link VersionIdentifier version} of this tool or {@code null} if not installed.
*/
public VersionIdentifier getInstalledVersion() {

return getInstalledVersion(this.context.getSoftwarePath().resolve(getName()));
}

/**
* @param toolPath the installation {@link Path} where to find the version file.
* @return the currently installed {@link VersionIdentifier version} of this tool or {@code null} if not installed.
*/
protected VersionIdentifier getInstalledVersion(Path toolPath) {

if (!Files.isDirectory(toolPath)) {
this.context.debug("Tool {} not installed in {}", getName(), toolPath);
return null;
}
Path toolVersionFile = toolPath.resolve(IdeContext.FILE_SOFTWARE_VERSION);
if (!Files.exists(toolVersionFile)) {
Path legacyToolVersionFile = toolPath.resolve(IdeContext.FILE_LEGACY_SOFTWARE_VERSION);
if (Files.exists(legacyToolVersionFile)) {
toolVersionFile = legacyToolVersionFile;
} else {
this.context.warning("Tool {} is missing version file in {}", getName(), toolVersionFile);
return null;
}
}
try {
String version = Files.readString(toolVersionFile).trim();
return VersionIdentifier.of(version);
} catch (IOException e) {
throw new IllegalStateException("Failed to read file " + toolVersionFile, e);
}
}
public abstract VersionIdentifier getInstalledVersion();

/**
* @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);
}

}
public abstract String getInstalledEdition();

/**
* Uninstalls the {@link #getName() tool}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.devonfw.tools.ide.commandlet;

import com.devonfw.tools.ide.cli.CliException;
import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import org.junit.jupiter.api.Test;
Expand All @@ -19,17 +17,15 @@ public class VersionGetCommandletTest extends AbstractIdeContextTest {
public void testVersionGetCommandletRunThrowsCliException() {

// arrange
IdeContext context = newContext(PROJECT_BASIC, null, false);
IdeTestContext context = newContext(PROJECT_BASIC, null, false);
VersionGetCommandlet versionGet = context.getCommandletManager().getCommandlet(VersionGetCommandlet.class);
versionGet.tool.setValueAsString("java", context);
// act
try {
versionGet.run();
failBecauseExceptionWasNotThrown(CliException.class);
} catch (CliException e) {
// assert
assertThat(e).hasMessageContaining("Tool java is not installed!");
}
versionGet.run();
// assert
assertLogMessage(context, IdeLogLevel.INFO, "The configured version for tool java is 17*");
assertLogMessage(context, IdeLogLevel.INFO, "To install that version call the following command:");
assertLogMessage(context, IdeLogLevel.INFO, "ide install java");
}

/**
Expand Down

0 comments on commit 0147431

Please sign in to comment.