Add support for semver version ranges (#82)

Closes: #38
This commit is contained in:
Kevin Stillhammer 2024-09-18 11:29:09 +02:00 committed by GitHub
parent 8e09161b4b
commit ce0062aac7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 109 additions and 19 deletions

View File

@ -40,7 +40,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, macos-14, oracle-aarch64]
uv-version: ["latest", "0.3.0", "0.3.2"]
uv-version: ["latest", "0.3.0", "0.3.2", "0.3", "0.3.x", ">=0.3.0"]
steps:
- uses: actions/checkout@v4
- name: Install version ${{ matrix.uv-version }}
@ -49,6 +49,25 @@ jobs:
version: ${{ matrix.uv-version }}
- run: uv sync
working-directory: __tests__/fixtures/uv-project
test-semver-range:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, oracle-aarch64]
steps:
- uses: actions/checkout@v4
- name: Install version 0.3
id: setup-uv
uses: ./
with:
version: "0.3"
- name: Correct version gets installed
run: |
if [ "$UV_VERSION" != "0.3.5" ]; then
exit 1
fi
env:
UV_VERSION: ${{ steps.setup-uv.outputs.uv-version }}
test-checksum:
runs-on: ${{ matrix.os }}
strategy:

View File

@ -50,6 +50,25 @@ For an example workflow, see
version: "0.4.4"
```
### Install a version by supplying a semver range
You can also specify a [semver range](https://github.com/npm/node-semver?tab=readme-ov-file#ranges)
to install the latest version that satisfies the range.
```yaml
- name: Install a semver range of uv
uses: astral-sh/setup-uv@v2
with:
version: ">=0.3.0"
```
```yaml
- name: Pinning a minor version of uv
uses: astral-sh/setup-uv@v2
with:
version: "0.3.x"
```
### Validate checksum
You can also specify a checksum to validate the downloaded file. Checksums up to the default version

BIN
dist/setup/index.js generated vendored

Binary file not shown.

View File

@ -4,15 +4,21 @@ import * as path from "path";
import { OWNER, REPO, TOOL_CACHE_NAME } from "../utils/constants";
import { Architecture, Platform } from "../utils/platforms";
import { validateChecksum } from "./checksum/checksum";
import * as github from "@actions/github";
export function tryGetFromToolCache(
arch: Architecture,
version: string,
): string | undefined {
): { version: string; installedPath: string | undefined } {
core.debug(`Trying to get uv from tool cache for ${version}...`);
const cachedVersions = tc.findAllVersions(TOOL_CACHE_NAME, arch);
core.debug(`Cached versions: ${cachedVersions}`);
return tc.find(TOOL_CACHE_NAME, version, arch);
let resolvedVersion = tc.evaluateVersions(cachedVersions, version);
if (resolvedVersion === "") {
resolvedVersion = version;
}
const installedPath = tc.find(TOOL_CACHE_NAME, resolvedVersion, arch);
return { version: resolvedVersion, installedPath };
}
export async function downloadVersion(
@ -20,10 +26,11 @@ export async function downloadVersion(
arch: Architecture,
version: string,
checkSum: string | undefined,
githubToken: string | undefined,
): Promise<string> {
githubToken: string,
): Promise<{ version: string; cachedToolDir: string }> {
const resolvedVersion = await resolveVersion(version, githubToken);
const artifact = `uv-${arch}-${platform}`;
let downloadUrl = `https://github.com/${OWNER}/${REPO}/releases/download/${version}/${artifact}`;
let downloadUrl = `https://github.com/${OWNER}/${REPO}/releases/download/${resolvedVersion}/${artifact}`;
if (platform === "pc-windows-msvc") {
downloadUrl += ".zip";
} else {
@ -36,7 +43,13 @@ export async function downloadVersion(
undefined,
githubToken,
);
await validateChecksum(checkSum, downloadPath, arch, platform, version);
await validateChecksum(
checkSum,
downloadPath,
arch,
platform,
resolvedVersion,
);
let uvDir: string;
if (platform === "pc-windows-msvc") {
@ -46,6 +59,37 @@ export async function downloadVersion(
const extractedDir = await tc.extractTar(downloadPath);
uvDir = path.join(extractedDir, artifact);
}
return await tc.cacheDir(uvDir, TOOL_CACHE_NAME, version, arch);
const cachedToolDir = await tc.cacheDir(
uvDir,
TOOL_CACHE_NAME,
resolvedVersion,
arch,
);
return { version: resolvedVersion, cachedToolDir };
}
async function resolveVersion(
version: string,
githubToken: string,
): Promise<string> {
if (tc.isExplicitVersion(version)) {
core.debug(`Version ${version} is an explicit version.`);
return version;
}
const availableVersions = await getAvailableVersions(githubToken);
const resolvedVersion = tc.evaluateVersions(availableVersions, version);
if (resolvedVersion === "") {
throw new Error(`No version found for ${version}`);
}
return resolvedVersion;
}
async function getAvailableVersions(githubToken: string): Promise<string[]> {
const octokit = github.getOctokit(githubToken);
const response = await octokit.paginate(octokit.rest.repos.listReleases, {
owner: OWNER,
repo: REPO,
});
return response.map((release) => release.tag_name);
}

View File

@ -41,8 +41,8 @@ async function run(): Promise<void> {
);
addUvToPath(setupResult.uvDir);
core.setOutput("uv-version", version);
core.info(`Successfully installed uv version ${version}`);
core.setOutput("uv-version", setupResult.version);
core.info(`Successfully installed uv version ${setupResult.version}`);
addMatchers();
setCacheDir(cacheLocalPath);
@ -50,10 +50,10 @@ async function run(): Promise<void> {
if (enableCache) {
await restoreCache(setupResult.version);
}
process.exit(0);
} catch (err) {
core.setFailed((err as Error).message);
}
process.exit(0);
}
async function setupUv(
@ -61,29 +61,37 @@ async function setupUv(
arch: Architecture,
versionInput: string,
checkSum: string | undefined,
githubToken: string | undefined,
githubToken: string,
): Promise<{ uvDir: string; version: string }> {
let installedPath: string | undefined;
let cachedToolDir: string;
let version: string;
if (versionInput === "latest") {
const result = await downloadLatest(platform, arch, checkSum, githubToken);
version = result.version;
cachedToolDir = result.cachedToolDir;
const latestResult = await downloadLatest(
platform,
arch,
checkSum,
githubToken,
);
version = latestResult.version;
cachedToolDir = latestResult.cachedToolDir;
} else {
version = versionInput;
installedPath = tryGetFromToolCache(arch, versionInput);
const toolCacheResult = tryGetFromToolCache(arch, versionInput);
version = toolCacheResult.version;
installedPath = toolCacheResult.installedPath;
if (installedPath) {
core.info(`Found uv in tool-cache for ${versionInput}`);
return { uvDir: installedPath, version };
}
cachedToolDir = await downloadVersion(
const versionResult = await downloadVersion(
platform,
arch,
versionInput,
checkSum,
githubToken,
);
cachedToolDir = versionResult.cachedToolDir;
version = versionResult.version;
}
return { uvDir: cachedToolDir, version };