From 0147431fc419635f18fba56899ffbaf10c4e83ee Mon Sep 17 00:00:00 2001 From: slskiba <161473135+slskiba@users.noreply.github.com> Date: Thu, 27 Jun 2024 16:51:16 +0200 Subject: [PATCH] #410: Refactor get version and edition (#411) --- .../ide/commandlet/VersionGetCommandlet.java | 8 ++- .../tools/ide/tool/GlobalToolCommandlet.java | 17 ++++- .../tools/ide/tool/LocalToolCommandlet.java | 67 +++++++++++++++++++ .../tools/ide/tool/ToolCommandlet.java | 63 +---------------- .../commandlet/VersionGetCommandletTest.java | 16 ++--- 5 files changed, 96 insertions(+), 75 deletions(-) diff --git a/cli/src/main/java/com/devonfw/tools/ide/commandlet/VersionGetCommandlet.java b/cli/src/main/java/com/devonfw/tools/ide/commandlet/VersionGetCommandlet.java index c7042d02f..4581e23d5 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/commandlet/VersionGetCommandlet.java +++ b/cli/src/main/java/com/devonfw/tools/ide/commandlet/VersionGetCommandlet.java @@ -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; @@ -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()); } diff --git a/cli/src/main/java/com/devonfw/tools/ide/tool/GlobalToolCommandlet.java b/cli/src/main/java/com/devonfw/tools/ide/tool/GlobalToolCommandlet.java index 6149abea0..641897dd3 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/tool/GlobalToolCommandlet.java +++ b/cli/src/main/java/com/devonfw/tools/ide/tool/GlobalToolCommandlet.java @@ -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 tags) { @@ -146,6 +147,20 @@ protected boolean doInstall(boolean silent) { return true; } + @Override + public VersionIdentifier getInstalledVersion() { + //TODO: handle "get-version " + this.context.error("Couldn't get installed version of " + this.getName()); + return null; + } + + @Override + public String getInstalledEdition() { + //TODO: handle "get-edition " + this.context.error("Couldn't get installed edition of " + this.getName()); + return null; + } + @Override public void uninstall() { //TODO: handle "uninstall " diff --git a/cli/src/main/java/com/devonfw/tools/ide/tool/LocalToolCommandlet.java b/cli/src/main/java/com/devonfw/tools/ide/tool/LocalToolCommandlet.java index e8e2f96c3..ace5140c3 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/tool/LocalToolCommandlet.java +++ b/cli/src/main/java/com/devonfw/tools/ide/tool/LocalToolCommandlet.java @@ -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 { diff --git a/cli/src/main/java/com/devonfw/tools/ide/tool/ToolCommandlet.java b/cli/src/main/java/com/devonfw/tools/ide/tool/ToolCommandlet.java index dd9629321..6ec9731ea 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/tool/ToolCommandlet.java +++ b/cli/src/main/java/com/devonfw/tools/ide/tool/ToolCommandlet.java @@ -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; @@ -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}. diff --git a/cli/src/test/java/com/devonfw/tools/ide/commandlet/VersionGetCommandletTest.java b/cli/src/test/java/com/devonfw/tools/ide/commandlet/VersionGetCommandletTest.java index f3834cf4f..993451e68 100644 --- a/cli/src/test/java/com/devonfw/tools/ide/commandlet/VersionGetCommandletTest.java +++ b/cli/src/test/java/com/devonfw/tools/ide/commandlet/VersionGetCommandletTest.java @@ -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; @@ -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"); } /**