Skip to content

Commit ed3b715

Browse files
committed
fix: add range specifier for simple versions
1 parent 5e9dd36 commit ed3b715

File tree

7 files changed

+38
-8
lines changed

7 files changed

+38
-8
lines changed

dist/legacy/setup-cpp.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/modern/setup-cpp.mjs.map

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/gcc/mingw.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export async function getMinGWPackageInfo(
130130
&& (threadModel === undefined || threadModel === assetThreadModel)
131131
&& (assetExceptionModel === undefined || assetExceptionModel === exceptionModel)
132132
},
133-
versionSatisfies: (assetVersion) => {
133+
versionSatisfies: (assetVersion, _version) => {
134134
// extract the base version by coercing the version
135135
const assetCoerce = semverCoerce(assetVersion)
136136
if (assetCoerce === null) {

src/utils/asset/load-assets.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { readFile } from "fs/promises"
22
import semverSatisfies from "semver/functions/satisfies.js"
3+
import { semverCoercedRangeIfInvalid } from "../setup/version.ts"
34

45
/**
56
* The list of assets
@@ -38,11 +39,12 @@ export type MatchAssetOpts = {
3839
/**
3940
* Custom version compare function
4041
* @param candidate The candidate version
42+
* @param coeredVersion The coerced version to compare against
4143
* @returns true if the candidate version satisfies the version
4244
*
4345
* @default semverSatisfies
4446
*/
45-
versionSatisfies?: (candidate: string) => boolean
47+
versionSatisfies?: (candidate: string, coeredVersion: string) => boolean
4648
/**
4749
* Custom tag filter and map function
4850
* @param tag The tag to filter and map
@@ -93,13 +95,16 @@ export function matchAsset(
9395
}
9496

9597
// Assume the version is a semver version if a custom version compare function is not given
96-
const versionSatisfies = opts.versionSatisfies ?? semverSatisfies
98+
const versionSatisfies: (c: string, v: string) => boolean = opts.versionSatisfies ?? semverSatisfies
99+
100+
// If not a valid semver version, coerce it to a semver version range
101+
const versionRange = semverCoercedRangeIfInvalid(opts.version)
97102

98103
// find the first tag that starts with the version
99104
// loop over the versions starting with the latest
100105
const candidateTags: string[] = []
101106
for (const [version, origTag] of versionMap.entries()) {
102-
if (versionSatisfies(version, opts.version)) {
107+
if (versionSatisfies(version, versionRange)) {
103108
candidateTags.push(origTag)
104109
}
105110
}

src/utils/setup/version.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,31 @@ export function semverCoerceIfInvalid(version: string) {
137137
return version
138138
}
139139

140+
/**
141+
* Coerce the given version to a semver range if it is invalid
142+
*/
143+
export function semverCoercedRangeIfInvalid(version: string) {
144+
if (semverValid(version) === null) {
145+
// version coercion
146+
try {
147+
// find the semver version of an integer
148+
const coercedVersion = semverCoerce(version)
149+
if (coercedVersion !== null) {
150+
// if the versions doesn't specify a range specifier (e.g. ^, ~, >, <, etc.), add a ^ to the version
151+
const versionRange = /^[<=>^~]/.test(coercedVersion.version)
152+
? coercedVersion.version
153+
: `^${coercedVersion.version}`
154+
155+
info(`Coerced version '${version}' to '${versionRange}'`)
156+
return versionRange
157+
}
158+
} catch (err) {
159+
// handled below
160+
}
161+
}
162+
return version
163+
}
164+
140165
export function removeVPrefix(version: string) {
141166
return Number.parseInt(version.replace(/^v/, ""), 10)
142167
}

0 commit comments

Comments
 (0)