Skip to content

Commit

Permalink
Update dependency check to handle the new version format
Browse files Browse the repository at this point in the history
The version is now `desktop-vX.Y.Z.rdN`, so can also use the shared
rcompareVersions function to compare versions with the `.rdN` suffix.

Signed-off-by: Jan Dubois <[email protected]>
  • Loading branch information
jandubois committed Feb 7, 2025
1 parent d0cef42 commit 1769d72
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 60 deletions.
2 changes: 1 addition & 1 deletion pkg/rancher-desktop/assets/dependencies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dockerCompose: 2.32.4
golangci-lint: 1.63.4
trivy: 0.59.1
steve: 0.1.0-beta9
rancherDashboard: desktop-v2.11.0.rd2
rancherDashboard: 2.11.0.rd2
dockerProvidedCredentialHelpers: 0.8.2
ECRCredentialHelper: 0.9.0
mobyOpenAPISpec: "1.47"
Expand Down
35 changes: 1 addition & 34 deletions scripts/dependencies/lima.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,10 @@ import {
getPublishedReleaseTagNames,
GitHubDependency,
GitHubRelease,
rcompareVersions,
} from 'scripts/lib/dependencies';
import { simpleSpawn } from 'scripts/simple_process';

/**
* rcompareVersions implementation for version strings that look like 0.1.2.rd3????.
* Note that anything after the number after "rd" is ignored.
* @param version1 The first version to compare.
* @param version2 The second version to compare.
* @returns Whether version1 is higher (-1), equal to (0), or lower than (1) version2.
*/
function rcompareVersions(version1: string, version2: string): -1 | 0 | 1 {
const semver1 = semver.coerce(version1);
const semver2 = semver.coerce(version2);

if (semver1 === null || semver2 === null) {
throw new Error(`One of ${ version1 } and ${ version2 } failed to be coerced to semver`);
}

if (semver1.raw !== semver2.raw) {
return semver.rcompare(semver1, semver2);
}

// If the two versions are equal, assume we have different build suffixes
// e.g. "0.19.0.rd5" vs "0.19.0.rd6"
const [, match1] = /^\d+\.\d+\.\d+\.rd(\d+)$/.exec(version1) ?? [];
const [, match2] = /^\d+\.\d+\.\d+\.rd(\d+)$/.exec(version2) ?? [];

if (!match1 || !match2) {
// One or both are invalid; prefer the valid one.
const fallback = Math.sign(version2.localeCompare(version1, 'en')) as -1 | 0 | 1;

return match1 ? -1 : match2 ? 1 : fallback;
}

return Math.sign(parseInt(match2, 10) - parseInt(match1, 10)) as -1 | 0 | 1;
}

export class Lima implements Dependency, GitHubDependency {
name = 'lima';
githubOwner = 'rancher-sandbox';
Expand Down
43 changes: 18 additions & 25 deletions scripts/dependencies/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@ import path from 'path';
import semver from 'semver';

import {
download, downloadZip, downloadTarGZ, getResource, DownloadOptions, ArchiveDownloadOptions,
ArchiveDownloadOptions,
download,
DownloadOptions,
downloadTarGZ,
downloadZip,
getResource,
} from '../lib/download';

import {
DownloadContext, Dependency, GitHubDependency, findChecksum, getPublishedReleaseTagNames, getPublishedVersions,
Dependency,
DownloadContext,
findChecksum,
getPublishedReleaseTagNames,
getPublishedVersions,
GitHubDependency,
rcompareVersions,
} from 'scripts/lib/dependencies';
import { simpleSpawn } from 'scripts/simple_process';

Expand Down Expand Up @@ -335,10 +346,9 @@ export class RancherDashboard implements Dependency, GitHubDependency {
name = 'rancherDashboard';
githubOwner = 'rancher-sandbox';
githubRepo = 'rancher-desktop-dashboard';
versionRegex = /^desktop-v([0-9]+\.[0-9]+\.[0-9]+)\.([0-9a-zA-Z]+(\.[0-9a-zA-Z]+)+)$/;

async download(context: DownloadContext): Promise<void> {
const baseURL = `https://github.com/rancher-sandbox/${ this.githubRepo }/releases/download/${ context.versions.rancherDashboard }`;
const baseURL = `https://github.com/rancher-sandbox/${ this.githubRepo }/releases/download/desktop-v${ context.versions.rancherDashboard }`;
const executableName = 'rancher-dashboard-desktop-embed';
const url = `${ baseURL }/${ executableName }.tar.gz`;
const destPath = path.join(context.resourcesDir, 'rancher-dashboard.tgz');
Expand Down Expand Up @@ -386,34 +396,17 @@ export class RancherDashboard implements Dependency, GitHubDependency {
}

async getAvailableVersions(): Promise<string[]> {
const versions = await getPublishedReleaseTagNames(this.githubOwner, this.githubRepo);
const tagNames = await getPublishedReleaseTagNames(this.githubOwner, this.githubRepo);

// Versions that contain .plugins. exist solely for testing during
// plugins development. For more info please see
// https://github.com/rancher-sandbox/rancher-desktop/issues/3757
return versions.filter(version => !version.includes('.plugins.'));
return tagNames.map((tagName: string) => tagName.replace(/^desktop-v/, ''));
}

versionToTagName(version: string): string {
return version;
}

versionToSemver(version: string): string {
const match = this.versionRegex.exec(version);

if (match === null) {
throw new Error(`${ this.name }: ${ version } does not match version regex ${ this.versionRegex }`);
}
const [, mainVersion, prereleaseVersion] = match;

return `${ mainVersion }-${ prereleaseVersion }`;
return `desktop-v${ version }`;
}

rcompareVersions(version1: string, version2: string): -1 | 0 | 1 {
const semver1 = this.versionToSemver(version1);
const semver2 = this.versionToSemver(version2);

return semver.rcompare(semver1, semver2);
return rcompareVersions(version1, version2);
}
}

Expand Down
34 changes: 34 additions & 0 deletions scripts/lib/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,40 @@ export type DependencyVersions = {
spinCLI: string;
};

/**
* rcompareVersions implementation for version strings that look like 0.1.2.rd3????.
* Note that anything after the number after "rd" is ignored.
* @param version1 The first version to compare.
* @param version2 The second version to compare.
* @returns Whether version1 is higher (-1), equal to (0), or lower than (1) version2.
*/
export function rcompareVersions(version1: string, version2: string): -1 | 0 | 1 {
const semver1 = semver.coerce(version1);
const semver2 = semver.coerce(version2);

if (semver1 === null || semver2 === null) {
throw new Error(`One of ${ version1 } and ${ version2 } failed to be coerced to semver`);
}

if (semver1.raw !== semver2.raw) {
return semver.rcompare(semver1, semver2);
}

// If the two versions are equal, assume we have different build suffixes
// e.g. "0.19.0.rd5" vs "0.19.0.rd6"
const [, match1] = /^\d+\.\d+\.\d+\.rd(\d+)$/.exec(version1) ?? [];
const [, match2] = /^\d+\.\d+\.\d+\.rd(\d+)$/.exec(version2) ?? [];

if (!match1 || !match2) {
// One or both are invalid; prefer the valid one.
const fallback = Math.sign(version2.localeCompare(version1, 'en')) as -1 | 0 | 1;

return match1 ? -1 : match2 ? 1 : fallback;
}

return Math.sign(parseInt(match2, 10) - parseInt(match1, 10)) as -1 | 0 | 1;
}

/**
* Download the given checksum file (which contains multiple checksums) and find
* the correct checksum for the given executable name.
Expand Down

0 comments on commit 1769d72

Please sign in to comment.