Add support for .tools-versions (#531)

Closes: #504
This commit is contained in:
Kevin Stillhammer
2025-08-21 11:15:28 +02:00
committed by GitHub
parent fce199e243
commit adeb28643f
7 changed files with 248 additions and 2 deletions

View File

@ -2,6 +2,7 @@ import fs from "node:fs";
import * as core from "@actions/core";
import { getRequiredVersionFromConfigFile } from "./config-file";
import { getUvVersionFromRequirementsFile } from "./requirements-file";
import { getUvVersionFromToolVersions } from "./tool-versions-file";
export function getUvVersionFromFile(filePath: string): string | undefined {
core.info(`Trying to find version for uv in: ${filePath}`);
@ -11,7 +12,10 @@ export function getUvVersionFromFile(filePath: string): string | undefined {
}
let uvVersion: string | undefined;
try {
uvVersion = getRequiredVersionFromConfigFile(filePath);
uvVersion = getUvVersionFromToolVersions(filePath);
if (uvVersion === undefined) {
uvVersion = getRequiredVersionFromConfigFile(filePath);
}
if (uvVersion === undefined) {
uvVersion = getUvVersionFromRequirementsFile(filePath);
}

View File

@ -0,0 +1,31 @@
import fs from "node:fs";
import * as core from "@actions/core";
export function getUvVersionFromToolVersions(
filePath: string,
): string | undefined {
if (!filePath.endsWith(".tool-versions")) {
return undefined;
}
const fileContents = fs.readFileSync(filePath, "utf8");
const lines = fileContents.split("\n");
for (const line of lines) {
// Skip commented lines
if (line.trim().startsWith("#")) {
continue;
}
const match = line.match(/^\s*uv\s*v?\s*(?<version>[^\s]+)\s*$/);
if (match) {
const matchedVersion = match.groups?.version.trim();
if (matchedVersion?.startsWith("ref")) {
core.warning(
"The ref syntax of .tool-versions is not supported. Please use a released version instead.",
);
return undefined;
}
return matchedVersion;
}
}
return undefined;
}