Skip to content

Commit

Permalink
fix: add range specifier for simple versions
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Sep 23, 2024
1 parent 5e9dd36 commit ed3b715
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dist/legacy/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/legacy/setup-cpp.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/modern/setup-cpp.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/modern/setup-cpp.mjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/gcc/mingw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export async function getMinGWPackageInfo(
&& (threadModel === undefined || threadModel === assetThreadModel)
&& (assetExceptionModel === undefined || assetExceptionModel === exceptionModel)
},
versionSatisfies: (assetVersion) => {
versionSatisfies: (assetVersion, _version) => {
// extract the base version by coercing the version
const assetCoerce = semverCoerce(assetVersion)
if (assetCoerce === null) {
Expand Down
11 changes: 8 additions & 3 deletions src/utils/asset/load-assets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readFile } from "fs/promises"
import semverSatisfies from "semver/functions/satisfies.js"
import { semverCoercedRangeIfInvalid } from "../setup/version.ts"

/**
* The list of assets
Expand Down Expand Up @@ -38,11 +39,12 @@ export type MatchAssetOpts = {
/**
* Custom version compare function
* @param candidate The candidate version
* @param coeredVersion The coerced version to compare against
* @returns true if the candidate version satisfies the version
*
* @default semverSatisfies
*/
versionSatisfies?: (candidate: string) => boolean
versionSatisfies?: (candidate: string, coeredVersion: string) => boolean
/**
* Custom tag filter and map function
* @param tag The tag to filter and map
Expand Down Expand Up @@ -93,13 +95,16 @@ export function matchAsset(
}

// Assume the version is a semver version if a custom version compare function is not given
const versionSatisfies = opts.versionSatisfies ?? semverSatisfies
const versionSatisfies: (c: string, v: string) => boolean = opts.versionSatisfies ?? semverSatisfies

// If not a valid semver version, coerce it to a semver version range
const versionRange = semverCoercedRangeIfInvalid(opts.version)

// find the first tag that starts with the version
// loop over the versions starting with the latest
const candidateTags: string[] = []
for (const [version, origTag] of versionMap.entries()) {
if (versionSatisfies(version, opts.version)) {
if (versionSatisfies(version, versionRange)) {
candidateTags.push(origTag)
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/utils/setup/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,31 @@ export function semverCoerceIfInvalid(version: string) {
return version
}

/**
* Coerce the given version to a semver range if it is invalid
*/
export function semverCoercedRangeIfInvalid(version: string) {
if (semverValid(version) === null) {
// version coercion
try {
// find the semver version of an integer
const coercedVersion = semverCoerce(version)
if (coercedVersion !== null) {
// if the versions doesn't specify a range specifier (e.g. ^, ~, >, <, etc.), add a ^ to the version
const versionRange = /^[<=>^~]/.test(coercedVersion.version)
? coercedVersion.version
: `^${coercedVersion.version}`

info(`Coerced version '${version}' to '${versionRange}'`)
return versionRange
}
} catch (err) {
// handled below
}
}
return version
}

export function removeVPrefix(version: string) {
return Number.parseInt(version.replace(/^v/, ""), 10)
}
Expand Down

0 comments on commit ed3b715

Please sign in to comment.