setup-uv/src/setup-uv.ts

147 lines
3.8 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,
toolBinDir,
toolDir,
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);
addToolBinToPath();
setToolDir();
core.setOutput("uv-version", setupResult.version);
core.info(`Successfully installed uv version ${setupResult.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
}
process.exit(0);
2024-08-23 23:58:26 +02:00
} catch (err) {
core.setFailed((err as Error).message);
2024-08-23 23:58:26 +02:00
}
}
async function setupUv(
platform: Platform,
arch: Architecture,
versionInput: string,
checkSum: string | undefined,
githubToken: string,
): Promise<{ uvDir: string; version: string }> {
let installedPath: string | undefined;
let cachedToolDir: string;
let version: string;
if (versionInput === "latest") {
const latestResult = await downloadLatest(
platform,
arch,
checkSum,
githubToken,
);
version = latestResult.version;
cachedToolDir = latestResult.cachedToolDir;
2024-08-23 23:58:26 +02:00
} else {
const toolCacheResult = tryGetFromToolCache(arch, versionInput);
version = toolCacheResult.version;
installedPath = toolCacheResult.installedPath;
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
}
const versionResult = await downloadVersion(
2024-08-23 23:58:26 +02:00
platform,
arch,
versionInput,
checkSum,
githubToken,
);
cachedToolDir = versionResult.cachedToolDir;
version = versionResult.version;
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 addToolBinToPath(): void {
if (toolBinDir !== undefined) {
core.exportVariable("UV_TOOL_BIN_DIR", toolBinDir);
core.info(`Set UV_TOOL_BIN_DIR to ${toolBinDir}`);
core.addPath(toolBinDir);
core.info(`Added ${toolBinDir} to the path`);
} else {
if (process.env.XDG_BIN_HOME !== undefined) {
core.addPath(process.env.XDG_BIN_HOME);
core.info(`Added ${process.env.XDG_BIN_HOME} to the path`);
} else if (process.env.XDG_DATA_HOME !== undefined) {
core.addPath(`${process.env.XDG_DATA_HOME}/../bin`);
core.info(`Added ${process.env.XDG_DATA_HOME}/../bin to the path`);
} else {
core.addPath(`${process.env.HOME}/.local/bin`);
core.info(`Added ${process.env.HOME}/.local/bin to the path`);
}
}
}
function setToolDir(): void {
if (toolDir !== undefined) {
core.exportVariable("UV_TOOL_DIR", toolDir);
core.info(`Set UV_TOOL_DIR to ${toolDir}`);
}
}
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();