2024-09-30 02:55:24 -04:00
|
|
|
import path from "node:path";
|
2025-08-12 22:12:10 +02:00
|
|
|
import * as core from "@actions/core";
|
2025-10-09 16:47:24 -04:00
|
|
|
import * as exec from "@actions/exec";
|
2025-10-07 16:08:30 +02:00
|
|
|
import { getConfigValueFromTomlFile } from "./config-file";
|
2024-08-23 23:58:26 +02:00
|
|
|
|
2025-07-23 19:31:57 +02:00
|
|
|
export const workingDirectory = core.getInput("working-directory");
|
2024-09-05 08:06:45 -04:00
|
|
|
export const version = core.getInput("version");
|
2025-07-23 19:31:57 +02:00
|
|
|
export const versionFile = getVersionFile();
|
2024-11-28 21:41:37 +01:00
|
|
|
export const pythonVersion = core.getInput("python-version");
|
2025-04-24 15:17:35 +02:00
|
|
|
export const activateEnvironment = core.getBooleanInput("activate-environment");
|
2024-09-05 08:06:45 -04:00
|
|
|
export const checkSum = core.getInput("checksum");
|
2024-12-13 20:12:42 +01:00
|
|
|
export const enableCache = getEnableCache();
|
2025-09-14 14:55:08 +02:00
|
|
|
export const restoreCache = core.getInput("restore-cache") === "true";
|
|
|
|
|
export const saveCache = core.getInput("save-cache") === "true";
|
2024-09-05 08:06:45 -04:00
|
|
|
export const cacheSuffix = core.getInput("cache-suffix") || "";
|
2024-09-07 14:11:25 +02:00
|
|
|
export const cacheLocalPath = getCacheLocalPath();
|
2025-07-23 15:29:01 +02:00
|
|
|
export const cacheDependencyGlob = getCacheDependencyGlob();
|
2024-10-25 08:11:32 -04:00
|
|
|
export const pruneCache = core.getInput("prune-cache") === "true";
|
2025-10-09 16:47:24 -04:00
|
|
|
export const cachePython = core.getInput("cache-python") === "true";
|
2024-11-23 16:30:54 +01:00
|
|
|
export const ignoreNothingToCache =
|
|
|
|
|
core.getInput("ignore-nothing-to-cache") === "true";
|
2025-03-16 22:15:17 +01:00
|
|
|
export const ignoreEmptyWorkdir =
|
|
|
|
|
core.getInput("ignore-empty-workdir") === "true";
|
2024-09-21 10:14:36 +02:00
|
|
|
export const toolBinDir = getToolBinDir();
|
|
|
|
|
export const toolDir = getToolDir();
|
|
|
|
|
export const githubToken = core.getInput("github-token");
|
2025-06-18 22:33:20 +02:00
|
|
|
export const manifestFile = getManifestFile();
|
2025-08-12 22:33:00 +02:00
|
|
|
export const addProblemMatchers =
|
|
|
|
|
core.getInput("add-problem-matchers") === "true";
|
2024-09-21 10:14:36 +02:00
|
|
|
|
2025-07-23 19:31:57 +02:00
|
|
|
function getVersionFile(): string {
|
|
|
|
|
const versionFileInput = core.getInput("version-file");
|
|
|
|
|
if (versionFileInput !== "") {
|
|
|
|
|
const tildeExpanded = expandTilde(versionFileInput);
|
|
|
|
|
return resolveRelativePath(tildeExpanded);
|
|
|
|
|
}
|
|
|
|
|
return versionFileInput;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 20:12:42 +01:00
|
|
|
function getEnableCache(): boolean {
|
|
|
|
|
const enableCacheInput = core.getInput("enable-cache");
|
|
|
|
|
if (enableCacheInput === "auto") {
|
|
|
|
|
return process.env.RUNNER_ENVIRONMENT === "github-hosted";
|
|
|
|
|
}
|
|
|
|
|
return enableCacheInput === "true";
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-21 10:14:36 +02:00
|
|
|
function getToolBinDir(): string | undefined {
|
|
|
|
|
const toolBinDirInput = core.getInput("tool-bin-dir");
|
|
|
|
|
if (toolBinDirInput !== "") {
|
2025-07-23 15:29:01 +02:00
|
|
|
const tildeExpanded = expandTilde(toolBinDirInput);
|
|
|
|
|
return resolveRelativePath(tildeExpanded);
|
2024-09-21 10:14:36 +02:00
|
|
|
}
|
|
|
|
|
if (process.platform === "win32") {
|
|
|
|
|
if (process.env.RUNNER_TEMP !== undefined) {
|
|
|
|
|
return `${process.env.RUNNER_TEMP}${path.sep}uv-tool-bin-dir`;
|
|
|
|
|
}
|
|
|
|
|
throw Error(
|
|
|
|
|
"Could not determine UV_TOOL_BIN_DIR. Please make sure RUNNER_TEMP is set or provide the tool-bin-dir input",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getToolDir(): string | undefined {
|
|
|
|
|
const toolDirInput = core.getInput("tool-dir");
|
|
|
|
|
if (toolDirInput !== "") {
|
2025-07-23 15:29:01 +02:00
|
|
|
const tildeExpanded = expandTilde(toolDirInput);
|
|
|
|
|
return resolveRelativePath(tildeExpanded);
|
2024-09-21 10:14:36 +02:00
|
|
|
}
|
|
|
|
|
if (process.platform === "win32") {
|
|
|
|
|
if (process.env.RUNNER_TEMP !== undefined) {
|
|
|
|
|
return `${process.env.RUNNER_TEMP}${path.sep}uv-tool-dir`;
|
|
|
|
|
}
|
|
|
|
|
throw Error(
|
|
|
|
|
"Could not determine UV_TOOL_DIR. Please make sure RUNNER_TEMP is set or provide the tool-dir input",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
2024-09-07 14:11:25 +02:00
|
|
|
|
|
|
|
|
function getCacheLocalPath(): string {
|
|
|
|
|
const cacheLocalPathInput = core.getInput("cache-local-path");
|
|
|
|
|
if (cacheLocalPathInput !== "") {
|
2025-07-23 15:29:01 +02:00
|
|
|
const tildeExpanded = expandTilde(cacheLocalPathInput);
|
|
|
|
|
return resolveRelativePath(tildeExpanded);
|
2024-09-07 14:11:25 +02:00
|
|
|
}
|
2025-10-07 16:08:30 +02:00
|
|
|
const cacheDirFromConfig = getCacheDirFromConfig();
|
|
|
|
|
if (cacheDirFromConfig !== undefined) {
|
|
|
|
|
return cacheDirFromConfig;
|
|
|
|
|
}
|
|
|
|
|
if (process.env.UV_CACHE_DIR !== undefined) {
|
|
|
|
|
core.info(`UV_CACHE_DIR is already set to ${process.env.UV_CACHE_DIR}`);
|
|
|
|
|
return process.env.UV_CACHE_DIR;
|
|
|
|
|
}
|
2025-04-24 15:17:56 +02:00
|
|
|
if (process.env.RUNNER_ENVIRONMENT === "github-hosted") {
|
|
|
|
|
if (process.env.RUNNER_TEMP !== undefined) {
|
|
|
|
|
return `${process.env.RUNNER_TEMP}${path.sep}setup-uv-cache`;
|
|
|
|
|
}
|
|
|
|
|
throw Error(
|
|
|
|
|
"Could not determine UV_CACHE_DIR. Please make sure RUNNER_TEMP is set or provide the cache-local-path input",
|
|
|
|
|
);
|
2024-09-07 14:11:25 +02:00
|
|
|
}
|
2025-04-24 15:17:56 +02:00
|
|
|
if (process.platform === "win32") {
|
|
|
|
|
return `${process.env.APPDATA}${path.sep}uv${path.sep}cache`;
|
|
|
|
|
}
|
|
|
|
|
return `${process.env.HOME}${path.sep}.cache${path.sep}uv`;
|
2024-09-07 14:11:25 +02:00
|
|
|
}
|
2024-11-23 09:21:51 +01:00
|
|
|
|
2025-10-07 16:08:30 +02:00
|
|
|
function getCacheDirFromConfig(): string | undefined {
|
|
|
|
|
for (const filePath of [versionFile, "uv.toml", "pyproject.toml"]) {
|
|
|
|
|
const resolvedPath = resolveRelativePath(filePath);
|
|
|
|
|
try {
|
|
|
|
|
const cacheDir = getConfigValueFromTomlFile(resolvedPath, "cache-dir");
|
|
|
|
|
if (cacheDir !== undefined) {
|
|
|
|
|
core.info(`Found cache-dir in ${resolvedPath}: ${cacheDir}`);
|
|
|
|
|
return cacheDir;
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
const message = (err as Error).message;
|
|
|
|
|
core.warning(`Error while parsing ${filePath}: ${message}`);
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-09 16:47:24 -04:00
|
|
|
export async function getUvPythonDir(): Promise<string> {
|
|
|
|
|
if (process.env.UV_PYTHON_INSTALL_DIR !== undefined) {
|
|
|
|
|
core.info(
|
|
|
|
|
`Using UV_PYTHON_INSTALL_DIR from environment: ${process.env.UV_PYTHON_INSTALL_DIR}`,
|
|
|
|
|
);
|
|
|
|
|
return process.env.UV_PYTHON_INSTALL_DIR;
|
|
|
|
|
}
|
|
|
|
|
core.info("Determining uv python dir using `uv python dir`...");
|
|
|
|
|
const result = await exec.getExecOutput("uv", ["python", "dir"]);
|
|
|
|
|
if (result.exitCode !== 0) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Failed to get uv python dir: ${result.stderr || result.stdout}`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
const dir = result.stdout.trim();
|
|
|
|
|
core.info(`Determined uv python dir: ${dir}`);
|
|
|
|
|
return dir;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-23 15:29:01 +02:00
|
|
|
function getCacheDependencyGlob(): string {
|
|
|
|
|
const cacheDependencyGlobInput = core.getInput("cache-dependency-glob");
|
|
|
|
|
if (cacheDependencyGlobInput !== "") {
|
|
|
|
|
return cacheDependencyGlobInput
|
|
|
|
|
.split("\n")
|
|
|
|
|
.map((part) => part.trim())
|
|
|
|
|
.map((part) => expandTilde(part))
|
|
|
|
|
.map((part) => resolveRelativePath(part))
|
|
|
|
|
.join("\n");
|
|
|
|
|
}
|
|
|
|
|
return cacheDependencyGlobInput;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-23 09:21:51 +01:00
|
|
|
function expandTilde(input: string): string {
|
|
|
|
|
if (input.startsWith("~")) {
|
|
|
|
|
return `${process.env.HOME}${input.substring(1)}`;
|
|
|
|
|
}
|
|
|
|
|
return input;
|
|
|
|
|
}
|
2025-06-18 22:33:20 +02:00
|
|
|
|
2025-07-23 15:29:01 +02:00
|
|
|
function resolveRelativePath(inputPath: string): string {
|
2025-09-01 16:12:49 +02:00
|
|
|
const hasNegation = inputPath.startsWith("!");
|
|
|
|
|
const pathWithoutNegation = hasNegation ? inputPath.substring(1) : inputPath;
|
|
|
|
|
|
|
|
|
|
const resolvedPath = path.resolve(workingDirectory, pathWithoutNegation);
|
|
|
|
|
|
|
|
|
|
core.debug(
|
|
|
|
|
`Resolving relative path ${inputPath} to ${hasNegation ? "!" : ""}${resolvedPath}`,
|
|
|
|
|
);
|
|
|
|
|
return hasNegation ? `!${resolvedPath}` : resolvedPath;
|
2025-07-23 15:29:01 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-18 22:33:20 +02:00
|
|
|
function getManifestFile(): string | undefined {
|
|
|
|
|
const manifestFileInput = core.getInput("manifest-file");
|
|
|
|
|
if (manifestFileInput !== "") {
|
|
|
|
|
return manifestFileInput;
|
|
|
|
|
}
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|