Files
setup-uv/src/utils/inputs.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

350 lines
10 KiB
TypeScript
Raw Normal View History

import path from "node:path";
2025-08-12 22:12:10 +02:00
import * as core from "@actions/core";
2025-10-07 16:08:30 +02:00
import { getConfigValueFromTomlFile } from "./config-file";
2024-08-23 23:58:26 +02:00
export enum CacheLocalSource {
Input,
Config,
Env,
Default,
}
2026-03-28 16:23:26 +01:00
export interface CacheLocalPath {
path: string;
source: CacheLocalSource;
}
2024-09-21 10:14:36 +02:00
2026-03-28 16:23:26 +01:00
export interface SetupInputs {
workingDirectory: string;
version: string;
versionFile: string;
pythonVersion: string;
activateEnvironment: boolean;
venvPath: string;
checksum: string;
enableCache: boolean;
restoreCache: boolean;
saveCache: boolean;
cacheSuffix: string;
cacheLocalPath?: CacheLocalPath;
cacheDependencyGlob: string;
pruneCache: boolean;
cachePython: boolean;
ignoreNothingToCache: boolean;
ignoreEmptyWorkdir: boolean;
toolBinDir?: string;
toolDir?: string;
pythonDir: string;
githubToken: string;
manifestFile?: string;
addProblemMatchers: boolean;
resolutionStrategy: "highest" | "lowest";
}
export function loadInputs(): SetupInputs {
const workingDirectory = core.getInput("working-directory");
const version = core.getInput("version");
const versionFile = getVersionFile(
workingDirectory,
core.getInput("version-file"),
);
const pythonVersion = core.getInput("python-version");
const activateEnvironment = core.getBooleanInput("activate-environment");
const venvPath = getVenvPath(
workingDirectory,
core.getInput("venv-path"),
activateEnvironment,
);
const checksum = core.getInput("checksum");
const enableCache = getEnableCache(core.getInput("enable-cache"));
const restoreCache = core.getInput("restore-cache") === "true";
const saveCache = core.getInput("save-cache") === "true";
const cacheSuffix = core.getInput("cache-suffix") || "";
const cacheLocalPath = getCacheLocalPath(
workingDirectory,
versionFile,
enableCache,
);
const cacheDependencyGlob = getCacheDependencyGlob(
workingDirectory,
core.getInput("cache-dependency-glob"),
);
const pruneCache = core.getInput("prune-cache") === "true";
const cachePython = core.getInput("cache-python") === "true";
const ignoreNothingToCache =
core.getInput("ignore-nothing-to-cache") === "true";
const ignoreEmptyWorkdir = core.getInput("ignore-empty-workdir") === "true";
const toolBinDir = getToolBinDir(
workingDirectory,
core.getInput("tool-bin-dir"),
);
const toolDir = getToolDir(workingDirectory, core.getInput("tool-dir"));
const pythonDir = getUvPythonDir();
const githubToken = core.getInput("github-token");
const manifestFile = getManifestFile(core.getInput("manifest-file"));
const addProblemMatchers = core.getInput("add-problem-matchers") === "true";
const resolutionStrategy = getResolutionStrategy(
core.getInput("resolution-strategy"),
);
return {
activateEnvironment,
addProblemMatchers,
cacheDependencyGlob,
cacheLocalPath,
cachePython,
cacheSuffix,
checksum,
enableCache,
githubToken,
ignoreEmptyWorkdir,
ignoreNothingToCache,
manifestFile,
pruneCache,
pythonDir,
pythonVersion,
resolutionStrategy,
restoreCache,
saveCache,
toolBinDir,
toolDir,
venvPath,
version,
versionFile,
workingDirectory,
};
}
function getVersionFile(
workingDirectory: string,
versionFileInput: string,
): string {
if (versionFileInput !== "") {
const tildeExpanded = expandTilde(versionFileInput);
2026-03-28 16:23:26 +01:00
return resolveRelativePath(workingDirectory, tildeExpanded);
}
return versionFileInput;
}
2026-03-28 16:23:26 +01:00
function getVenvPath(
workingDirectory: string,
venvPathInput: string,
activateEnvironment: boolean,
): string {
if (venvPathInput !== "") {
if (!activateEnvironment) {
core.warning("venv-path is only used when activate-environment is true");
}
const tildeExpanded = expandTilde(venvPathInput);
2026-03-28 16:23:26 +01:00
return normalizePath(resolveRelativePath(workingDirectory, tildeExpanded));
}
2026-03-28 16:23:26 +01:00
return normalizePath(resolveRelativePath(workingDirectory, ".venv"));
}
2026-03-28 16:23:26 +01:00
function getEnableCache(enableCacheInput: string): boolean {
if (enableCacheInput === "auto") {
return process.env.RUNNER_ENVIRONMENT === "github-hosted";
}
return enableCacheInput === "true";
}
2026-03-28 16:23:26 +01:00
function getToolBinDir(
workingDirectory: string,
toolBinDirInput: string,
): string | undefined {
2024-09-21 10:14:36 +02:00
if (toolBinDirInput !== "") {
const tildeExpanded = expandTilde(toolBinDirInput);
2026-03-28 16:23:26 +01:00
return resolveRelativePath(workingDirectory, 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;
}
2026-03-28 16:23:26 +01:00
function getToolDir(
workingDirectory: string,
toolDirInput: string,
): string | undefined {
2024-09-21 10:14:36 +02:00
if (toolDirInput !== "") {
const tildeExpanded = expandTilde(toolDirInput);
2026-03-28 16:23:26 +01:00
return resolveRelativePath(workingDirectory, 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;
}
2026-03-28 16:23:26 +01:00
function getCacheLocalPath(
workingDirectory: string,
versionFile: string,
enableCache: boolean,
): CacheLocalPath | undefined {
const cacheLocalPathInput = core.getInput("cache-local-path");
if (cacheLocalPathInput !== "") {
const tildeExpanded = expandTilde(cacheLocalPathInput);
return {
2026-03-28 16:23:26 +01:00
path: resolveRelativePath(workingDirectory, tildeExpanded),
source: CacheLocalSource.Input,
};
}
2026-03-28 16:23:26 +01:00
const cacheDirFromConfig = getCacheDirFromConfig(
workingDirectory,
versionFile,
);
2025-10-07 16:08:30 +02:00
if (cacheDirFromConfig !== undefined) {
return { path: cacheDirFromConfig, source: CacheLocalSource.Config };
2025-10-07 16:08:30 +02:00
}
if (process.env.UV_CACHE_DIR !== undefined) {
core.info(`UV_CACHE_DIR is already set to ${process.env.UV_CACHE_DIR}`);
return { path: process.env.UV_CACHE_DIR, source: CacheLocalSource.Env };
2025-10-07 16:08:30 +02:00
}
2026-03-28 16:23:26 +01:00
if (enableCache) {
if (process.env.RUNNER_ENVIRONMENT === "github-hosted") {
if (process.env.RUNNER_TEMP !== undefined) {
return {
path: `${process.env.RUNNER_TEMP}${path.sep}setup-uv-cache`,
source: CacheLocalSource.Default,
};
}
throw Error(
"Could not determine UV_CACHE_DIR. Please make sure RUNNER_TEMP is set or provide the cache-local-path input",
);
}
if (process.platform === "win32") {
return {
path: `${process.env.APPDATA}${path.sep}uv${path.sep}cache`,
source: CacheLocalSource.Default,
};
}
return {
path: `${process.env.HOME}${path.sep}.cache${path.sep}uv`,
source: CacheLocalSource.Default,
};
}
}
2024-11-23 09:21:51 +01:00
2026-03-28 16:23:26 +01:00
function getCacheDirFromConfig(
workingDirectory: string,
versionFile: string,
): string | undefined {
2025-10-07 16:08:30 +02:00
for (const filePath of [versionFile, "uv.toml", "pyproject.toml"]) {
2026-03-28 16:23:26 +01:00
const resolvedPath = resolveRelativePath(workingDirectory, filePath);
2025-10-07 16:08:30 +02:00
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;
}
export function getUvPythonDir(): string {
2025-10-09 16:47:24 -04:00
if (process.env.UV_PYTHON_INSTALL_DIR !== undefined) {
core.info(
`UV_PYTHON_INSTALL_DIR is already set to ${process.env.UV_PYTHON_INSTALL_DIR}`,
2025-10-09 16:47:24 -04:00
);
return process.env.UV_PYTHON_INSTALL_DIR;
}
if (process.env.RUNNER_ENVIRONMENT !== "github-hosted") {
if (process.platform === "win32") {
return `${process.env.APPDATA}${path.sep}uv${path.sep}python`;
}
2026-03-28 16:23:26 +01:00
return `${process.env.HOME}${path.sep}.local${path.sep}share${path.sep}uv${path.sep}python`;
2025-10-09 16:47:24 -04:00
}
if (process.env.RUNNER_TEMP !== undefined) {
return `${process.env.RUNNER_TEMP}${path.sep}uv-python-dir`;
}
throw Error(
"Could not determine UV_PYTHON_INSTALL_DIR. Please make sure RUNNER_TEMP is set or provide the UV_PYTHON_INSTALL_DIR environment variable",
);
2025-10-09 16:47:24 -04:00
}
2026-03-28 16:23:26 +01:00
function getCacheDependencyGlob(
workingDirectory: string,
cacheDependencyGlobInput: string,
): string {
if (cacheDependencyGlobInput !== "") {
return cacheDependencyGlobInput
.split("\n")
.map((part) => part.trim())
.map((part) => expandTilde(part))
2026-03-28 16:23:26 +01:00
.map((part) => resolveRelativePath(workingDirectory, 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
function normalizePath(inputPath: string): string {
const normalized = path.normalize(inputPath);
const root = path.parse(normalized).root;
// Remove any trailing path separators, except when the whole path is the root.
let trimmed = normalized;
while (trimmed.length > root.length && trimmed.endsWith(path.sep)) {
trimmed = trimmed.slice(0, -1);
}
return trimmed;
}
2026-03-28 16:23:26 +01:00
function resolveRelativePath(
workingDirectory: string,
inputPath: string,
): string {
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;
}
2026-03-28 16:23:26 +01:00
function getManifestFile(manifestFileInput: string): string | undefined {
2025-06-18 22:33:20 +02:00
if (manifestFileInput !== "") {
return manifestFileInput;
}
return undefined;
}
2026-03-28 16:23:26 +01:00
function getResolutionStrategy(
resolutionStrategyInput: string,
): "highest" | "lowest" {
if (resolutionStrategyInput === "lowest") {
return "lowest";
}
if (resolutionStrategyInput === "highest" || resolutionStrategyInput === "") {
return "highest";
}
throw new Error(
`Invalid resolution-strategy: ${resolutionStrategyInput}. Must be 'highest' or 'lowest'.`,
);
}