Add input version-file (#486)

Closes: #411
This commit is contained in:
Kevin Stillhammer
2025-07-17 20:22:18 +02:00
committed by GitHub
parent c893ac1cb2
commit b75ff7d7b8
13 changed files with 369 additions and 136 deletions

View File

@ -0,0 +1,22 @@
import fs from "node:fs";
import * as toml from "smol-toml";
export function getRequiredVersionFromConfigFile(
filePath: string,
): string | undefined {
if (!filePath.endsWith(".toml")) {
return undefined;
}
const fileContent = fs.readFileSync(filePath, "utf-8");
if (filePath.endsWith("pyproject.toml")) {
const tomlContent = toml.parse(fileContent) as {
tool?: { uv?: { "required-version"?: string } };
};
return tomlContent?.tool?.uv?.["required-version"];
}
const tomlContent = toml.parse(fileContent) as {
"required-version"?: string;
};
return tomlContent["required-version"];
}

View File

@ -0,0 +1,43 @@
import * as toml from "smol-toml";
import fs from "node:fs";
export function getUvVersionFromRequirementsFile(
filePath: string,
): string | undefined {
const fileContent = fs.readFileSync(filePath, "utf-8");
if (filePath.endsWith(".txt")) {
return getUvVersionFromAllDependencies(fileContent.split("\n"));
}
const dependencies = parsePyprojectDependencies(fileContent);
return getUvVersionFromAllDependencies(dependencies);
}
function getUvVersionFromAllDependencies(
allDependencies: string[],
): string | undefined {
return allDependencies
.find((dep: string) => dep.startsWith("uv"))
?.match(/^uv([^A-Z0-9._-]+.*)$/)?.[1]
.trim();
}
interface Pyproject {
project?: {
dependencies?: string[];
"optional-dependencies"?: Record<string, string[]>;
};
"dependency-groups"?: Record<string, Array<string | object>>;
}
function parsePyprojectDependencies(pyprojectContent: string): string[] {
const pyproject: Pyproject = toml.parse(pyprojectContent);
const dependencies: string[] = pyproject?.project?.dependencies || [];
const optionalDependencies: string[] = Object.values(
pyproject?.project?.["optional-dependencies"] || {},
).flat();
const devDependencies: string[] = Object.values(
pyproject?.["dependency-groups"] || {},
)
.flat()
.filter((item: string | object) => typeof item === "string");
return dependencies.concat(optionalDependencies, devDependencies);
}

30
src/version/resolve.ts Normal file
View File

@ -0,0 +1,30 @@
import * as core from "@actions/core";
import fs from "node:fs";
import { getRequiredVersionFromConfigFile } from "./config-file";
import { getUvVersionFromRequirementsFile } from "./requirements-file";
export function getUvVersionFromFile(filePath: string): string | undefined {
core.info(`Trying to find version for uv in: ${filePath}`);
if (!fs.existsSync(filePath)) {
core.info(`Could not find file: ${filePath}`);
return undefined;
}
let uvVersion: string | undefined;
try {
uvVersion = getRequiredVersionFromConfigFile(filePath);
if (uvVersion === undefined) {
uvVersion = getUvVersionFromRequirementsFile(filePath);
}
} catch (err) {
const message = (err as Error).message;
core.warning(`Error while parsing ${filePath}: ${message}`);
return undefined;
}
if (uvVersion?.startsWith("==")) {
uvVersion = uvVersion.slice(2);
}
if (uvVersion !== undefined) {
core.info(`Found version for uv in ${filePath}: ${uvVersion}`);
}
return uvVersion;
}