setup-uv/src/setup-uv.ts

108 lines
2.6 KiB
TypeScript
Raw Normal View History

import * as core from "@actions/core";
import * as path from "path";
import {
downloadVersion,
tryGetFromToolCache,
} from "./download/download-version";
import { restoreCache } from "./cache/restore-cache";
2024-08-23 23:58:26 +02:00
import { downloadLatest } from "./download/download-latest";
import {
Architecture,
getArch,
getPlatform,
Platform,
} from "./utils/platforms";
2024-08-23 23:58:26 +02:00
import {
cacheLocalPath,
checkSum,
enableCache,
githubToken,
version,
} from "./utils/inputs";
2024-08-23 23:58:26 +02:00
async function run(): Promise<void> {
const platform = getPlatform();
const arch = getArch();
2024-08-23 23:58:26 +02:00
try {
if (platform === undefined) {
throw new Error(`Unsupported platform: ${process.platform}`);
2024-08-23 23:58:26 +02:00
}
if (arch === undefined) {
throw new Error(`Unsupported architecture: ${process.arch}`);
2024-08-23 23:58:26 +02:00
}
2024-08-24 00:14:50 +02:00
const setupResult = await setupUv(
2024-08-23 23:58:26 +02:00
platform,
arch,
version,
checkSum,
githubToken,
);
2024-08-23 23:58:26 +02:00
addUvToPath(setupResult.uvDir);
core.setOutput("uv-version", version);
core.info(`Successfully installed uv version ${version}`);
2024-08-24 00:14:50 +02:00
addMatchers();
setCacheDir(cacheLocalPath);
2024-08-23 23:58:26 +02:00
if (enableCache) {
await restoreCache(setupResult.version);
2024-08-23 23:58:26 +02:00
}
} catch (err) {
core.setFailed((err as Error).message);
2024-08-23 23:58:26 +02:00
}
process.exit(0);
2024-08-23 23:58:26 +02:00
}
async function setupUv(
platform: Platform,
arch: Architecture,
versionInput: string,
checkSum: string | undefined,
githubToken: string | undefined,
): 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;
2024-08-23 23:58:26 +02:00
} else {
version = versionInput;
installedPath = tryGetFromToolCache(arch, versionInput);
2024-08-23 23:58:26 +02:00
if (installedPath) {
core.info(`Found uv in tool-cache for ${versionInput}`);
return { uvDir: installedPath, version };
2024-08-23 23:58:26 +02:00
}
2024-08-24 00:14:50 +02:00
cachedToolDir = await downloadVersion(
2024-08-23 23:58:26 +02:00
platform,
arch,
versionInput,
checkSum,
githubToken,
);
2024-08-23 23:58:26 +02:00
}
return { uvDir: cachedToolDir, version };
2024-08-23 23:58:26 +02:00
}
function addUvToPath(cachedPath: string): void {
core.addPath(cachedPath);
core.info(`Added ${cachedPath} to the path`);
2024-08-23 23:58:26 +02:00
}
function setCacheDir(cacheLocalPath: string): void {
core.exportVariable("UV_CACHE_DIR", cacheLocalPath);
core.info(`Set UV_CACHE_DIR to ${cacheLocalPath}`);
2024-08-23 23:58:26 +02:00
}
function addMatchers(): void {
const matchersPath = path.join(__dirname, `..${path.sep}..`, ".github");
core.info(`##[add-matcher]${path.join(matchersPath, "python.json")}`);
2024-08-23 23:58:26 +02:00
}
run();