Skip to content

Commit

Permalink
chore: Re-introduce support for versions specified with 'v' prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Feb 1, 2024
1 parent 3062bbb commit be9b11d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ private final void addUninstallPreparation(ToolDefinitionVersionDescriptor versi
var toolName = installer.getToolName();
var installationDescriptor = ToolInstallationDescriptor.load(toolName, versionDescriptor);
if ( installationDescriptor!=null ) {
var msg = String.format("Uninstall %s v%s from %s", toolName, versionDescriptor.getVersion(), installationDescriptor.getInstallDir());
var msg = String.format("Uninstall %s %s from %s", toolName, versionDescriptor.getVersion(), installationDescriptor.getInstallDir());
requiredPreparations.put(msg, ()->uninstall(versionDescriptor, installationDescriptor));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,22 @@ public static final Path getToolsStatePath() {

/**
* The given version descriptor is considered a candidate for uninstall
* if all of the following conditions are met:
* - The full version is listed in --uninstall
* - The major version is listed in --uninstall
* - The major & minor version is listed in --uninstall
* - An installation descriptor for the version exists
* if an installation descriptor for the version exists, and any of the
* following conditions are met:
* - The full version is listed in --uninstall, optionally prefixed with 'v'
* - The major version is listed in --uninstall, optionally prefixed with 'v'
* - The major & minor version is listed in --uninstall, optionally prefixed with 'v'
*/
public static final boolean isCandidateForUninstall(String toolName, Set<String> versionsToUninstall, ToolDefinitionVersionDescriptor versionDescriptor) {
var version = versionDescriptor.getVersion();
return (versionsToUninstall.contains("all")
|| versionsToUninstall.contains(version)
|| versionsToUninstall.contains(SemVerHelper.getMajor(version).orElse("N/A"))
|| versionsToUninstall.contains(SemVerHelper.getMajorMinor(version).orElse("N/A")))
|| containsCandidateForUninstall(versionsToUninstall, version)
|| containsCandidateForUninstall(versionsToUninstall, SemVerHelper.getMajor(version).orElse("N/A"))
|| containsCandidateForUninstall(versionsToUninstall, SemVerHelper.getMajorMinor(version).orElse("N/A")))
&& ToolInstallationDescriptor.load(toolName, versionDescriptor)!=null;
}

private static final boolean containsCandidateForUninstall(Set<String> versionsToUninstall, String candidate) {
return versionsToUninstall.contains(candidate) || versionsToUninstall.contains("v"+candidate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public final Stream<ToolDefinitionVersionDescriptor> getVersionsStream() {

public final ToolDefinitionVersionDescriptor getVersion(String versionOrAlias) {
return getVersionsStream()
.filter(v-> (v.getVersion().equals(versionOrAlias) || Arrays.stream(v.getAliases()).anyMatch(versionOrAlias::equals)) )
.filter(v->matches(v, versionOrAlias))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Version or alias "+versionOrAlias+" not found"));
}
Expand All @@ -55,4 +55,13 @@ public final ToolDefinitionVersionDescriptor getVersionOrDefault(String versionO
return getVersion(versionOrAlias);
}

private static final boolean matches(ToolDefinitionVersionDescriptor descriptor, String versionOrAlias) {
var result = descriptor.getVersion().equals(versionOrAlias)
|| Arrays.stream(descriptor.getAliases()).anyMatch(versionOrAlias::equals);
if ( !result && versionOrAlias.startsWith("v") ) {
result = matches(descriptor, versionOrAlias.replaceFirst("^v", ""));
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,42 @@ class ToolFoDUploaderSpec extends FcliBaseSpec {
}
}

def "installWithVPrefix"() {
def args = "tool fod-uploader install -y -v=v5.0.0 --progress=none"
when:
def result = Fcli.run(args, {it.expectZeroExitCode()})
then:
verifyAll(result.stdout) {
size()>0
it[0].replace(' ', '').equals("NameVersionAliasesStableInstalldirAction")
it[1].contains("5.0.0")
it[1].contains(" INSTALLED")
}
}

def "installAndUninstallWithVPrefix"() {
def args = "tool fod-uploader install -y -v=v5.0.1 --uninstall=v5.0.0 --progress=none"
when:
def result = Fcli.run(args, {it.expectZeroExitCode()})
then:
verifyAll(result.stdout) {
size()>0
it[0].replace(' ', '').equals("NameVersionAliasesStableInstalldirAction")
it[1].contains("5.0.1")
it[1].contains(" INSTALLED")
}
}

def "uninstallWithVPrefix"() {
def args = "tool fod-uploader uninstall -y -v=v5.0.1 --progress=none"
when:
def result = Fcli.run(args, {it.expectZeroExitCode()})
then:
verifyAll(result.stdout) {
size()>0
it[0].replace(' ', '').equals("NameVersionAliasesStableInstalldirAction")
it[1].contains(" UNINSTALLED")
}
}

}

0 comments on commit be9b11d

Please sign in to comment.