-
Notifications
You must be signed in to change notification settings - Fork 87
#2296: Refactor ToolCommandlet to determine InstalledEdition and Version in a Single Cached Lookup #2306
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
base: main
Are you sure you want to change the base?
#2296: Refactor ToolCommandlet to determine InstalledEdition and Version in a Single Cached Lookup #2306
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.devonfw.tools.ide.tool; | ||
|
|
||
| import com.devonfw.tools.ide.version.VersionIdentifier; | ||
|
|
||
| /** | ||
| * Simple record holding an installed tool's edition and version together. | ||
| */ | ||
| public record EditionAndVersion(String edition, VersionIdentifier version) { | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |||||||||
| import org.slf4j.LoggerFactory; | ||||||||||
| import org.slf4j.event.Level; | ||||||||||
|
|
||||||||||
| import com.devonfw.tools.ide.cache.CachedValue; | ||||||||||
| import com.devonfw.tools.ide.commandlet.Commandlet; | ||||||||||
| import com.devonfw.tools.ide.common.Tag; | ||||||||||
| import com.devonfw.tools.ide.common.Tags; | ||||||||||
|
|
@@ -63,6 +64,9 @@ public abstract class ToolCommandlet extends Commandlet implements Tags { | |||||||||
|
|
||||||||||
| private MacOsHelper macOsHelper; | ||||||||||
|
|
||||||||||
| /** Cached result for {@link #getInstalledEditionAndVersion()}. */ | ||||||||||
| private CachedValue<EditionAndVersion> installedEditionAndVersion; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Registry for tool-specific auto-completion candidates. | ||||||||||
| */ | ||||||||||
|
|
@@ -608,6 +612,8 @@ protected ToolInstallation createToolInstallation(Path rootDir, VersionIdentifie | |||||||||
| protected ToolInstallation createToolInstallation(Path rootDir, Path linkDir, Path binDir, VersionIdentifier version, boolean newInstallation, | ||||||||||
| EnvironmentContext environmentContext, boolean additionalInstallation) { | ||||||||||
|
|
||||||||||
| // Invalidate cached edition/version so that subsequent calls reflect the new installation | ||||||||||
|
krystynaShatkovska marked this conversation as resolved.
|
||||||||||
| invalidateInstalledEditionAndVersion(); | ||||||||||
| // do not copy the version file into macOS .app bundles: changing the bundle after codesigning breaks the seal. | ||||||||||
| ToolInstallation toolInstallation = new ToolInstallation(rootDir, linkDir, binDir, version, newInstallation); | ||||||||||
| setEnvironment(environmentContext, toolInstallation, additionalInstallation); | ||||||||||
|
|
@@ -811,10 +817,43 @@ protected MacOsHelper getMacOsHelper() { | |||||||||
| return this.macOsHelper; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Gets the installed edition and version together, resolving both in a single operation. | ||||||||||
| * | ||||||||||
| * @return the {@link EditionAndVersion} or {@code null} if not installed. | ||||||||||
| */ | ||||||||||
| public EditionAndVersion getInstalledEditionAndVersion() { | ||||||||||
|
|
||||||||||
| if (this.installedEditionAndVersion == null) { | ||||||||||
| this.installedEditionAndVersion = new CachedValue<>(this::computeInstalledEditionAndVersion); | ||||||||||
| } | ||||||||||
| return this.installedEditionAndVersion.get(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Hook to compute the installed edition and version together. Override this method in subclasses to resolve both | ||||||||||
| * edition and version in a single operation, avoiding redundant expensive lookups. | ||||||||||
| * | ||||||||||
| * @return the {@link EditionAndVersion} or {@code null} if not installed. | ||||||||||
| */ | ||||||||||
| protected EditionAndVersion computeInstalledEditionAndVersion() { | ||||||||||
|
|
||||||||||
| String edition = computeInstalledEdition(); | ||||||||||
| VersionIdentifier version = computeInstalledVersion(); | ||||||||||
| if ((edition == null) && (version == null)) { | ||||||||||
| return null; | ||||||||||
| } | ||||||||||
| return new EditionAndVersion(edition, version); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @return the currently installed {@link VersionIdentifier version} of this tool or {@code null} if not installed. | ||||||||||
| */ | ||||||||||
| public abstract VersionIdentifier getInstalledVersion(); | ||||||||||
| public final VersionIdentifier getInstalledVersion() { | ||||||||||
|
|
||||||||||
| EditionAndVersion ev = getInstalledEditionAndVersion(); | ||||||||||
| return (ev != null) ? ev.version() : null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @return {@code true} if this tool is installed, {@code false} otherwise. | ||||||||||
|
|
@@ -827,7 +866,57 @@ public boolean isInstalled() { | |||||||||
| /** | ||||||||||
| * @return the installed edition of this tool or {@code null} if not installed. | ||||||||||
| */ | ||||||||||
| public abstract String getInstalledEdition(); | ||||||||||
| public final String getInstalledEdition() { | ||||||||||
|
|
||||||||||
| EditionAndVersion ev = getInstalledEditionAndVersion(); | ||||||||||
| return (ev != null) ? ev.edition() : null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @deprecated Override {@link #computeInstalledEditionAndVersion()} instead. | ||||||||||
| * @return the currently installed version. | ||||||||||
| */ | ||||||||||
| @Deprecated | ||||||||||
| protected VersionIdentifier computeInstalledVersion() { | ||||||||||
|
|
||||||||||
| return getInstalledVersionDeprecated(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @deprecated Override {@link #computeInstalledEditionAndVersion()} instead. | ||||||||||
| * @return the installed edition. | ||||||||||
| */ | ||||||||||
| @Deprecated | ||||||||||
| protected String computeInstalledEdition() { | ||||||||||
|
|
||||||||||
| return getInstalledEditionDeprecated(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @deprecated Override {@link #computeInstalledEditionAndVersion()} instead. | ||||||||||
| * @return the installed version. | ||||||||||
| */ | ||||||||||
| @Deprecated | ||||||||||
| protected VersionIdentifier getInstalledVersionDeprecated() { | ||||||||||
|
|
||||||||||
| return null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @deprecated Override {@link #computeInstalledEditionAndVersion()} instead. | ||||||||||
| * @return the installed edition. | ||||||||||
| */ | ||||||||||
| @Deprecated | ||||||||||
| protected String getInstalledEditionDeprecated() { | ||||||||||
|
|
||||||||||
| return null; | ||||||||||
| } | ||||||||||
|
Comment on lines
+875
to
+913
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we still need this or was that only and intermediate state during refactoring? |
||||||||||
|
|
||||||||||
| /** Invalidates the cached installed edition and version so the next call to {@link #getInstalledEditionAndVersion()} recomputes the result. */ | ||||||||||
| protected void invalidateInstalledEditionAndVersion() { | ||||||||||
|
|
||||||||||
| this.installedEditionAndVersion = null; | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
BTW: I would even suggest to make |
||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Uninstalls the {@link #getName() tool}. | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for the record: having an edition here but no version seems like an inconsistent state to me.
Hopefully never happens but wouldn't this be more consistent?