Default to ruff-version from pyproject.toml (#30)

Closes: #9 

Changes the default behavior to:

- Search for a pyproject.toml in the repo root
- Search in project.dependencies and dependency-groups.dev for ruff
- If none is found use latest

Support for requirements.txt files can be added later
This commit is contained in:
Kevin Stillhammer
2024-12-23 11:07:46 +01:00
committed by GitHub
parent cac2f108b2
commit 7a82f1f7e4
15 changed files with 4335 additions and 53 deletions
+36 -10
View File
@@ -13,7 +13,16 @@ import {
getPlatform,
type Platform,
} from "./utils/platforms";
import { args, checkSum, githubToken, src, version } from "./utils/inputs";
import {
args,
checkSum,
githubToken,
src,
version,
versionFile as versionFileInput,
} from "./utils/inputs";
import { getRuffVersionFromPyproject } from "./utils/pyproject";
import * as fs from "node:fs";
async function run(): Promise<void> {
const platform = getPlatform();
@@ -26,13 +35,7 @@ async function run(): Promise<void> {
if (arch === undefined) {
throw new Error(`Unsupported architecture: ${process.arch}`);
}
const setupResult = await setupRuff(
platform,
arch,
version,
checkSum,
githubToken,
);
const setupResult = await setupRuff(platform, arch, checkSum, githubToken);
addRuffToPath(setupResult.ruffDir);
setOutputFormat();
@@ -55,11 +58,10 @@ async function run(): Promise<void> {
async function setupRuff(
platform: Platform,
arch: Architecture,
versionInput: string,
checkSum: string | undefined,
githubToken: string,
): Promise<{ ruffDir: string; version: string }> {
const resolvedVersion = await resolveVersion(versionInput, githubToken);
const resolvedVersion = await determineVersion();
const toolCacheResult = tryGetFromToolCache(arch, resolvedVersion);
if (toolCacheResult.installedPath) {
core.info(`Found ruffDir in tool-cache for ${toolCacheResult.version}`);
@@ -83,6 +85,30 @@ async function setupRuff(
};
}
async function determineVersion(): Promise<string> {
if (versionFileInput !== "" && version !== "") {
throw Error("It is not allowed to specify both version and version-file");
}
if (version !== "") {
return await resolveVersion(version, githubToken);
}
if (versionFileInput !== "") {
const versionFromPyproject = getRuffVersionFromPyproject(versionFileInput);
if (versionFromPyproject === undefined) {
core.warning(
"Could not parse version from supplied pyproject.toml. Using latest version.",
);
return await resolveVersion("latest", githubToken);
}
}
const pyProjectPath = path.join(src, "pyproject.toml");
if (!fs.existsSync(pyProjectPath)) {
return await resolveVersion("latest", githubToken);
}
const versionFromPyproject = getRuffVersionFromPyproject(pyProjectPath);
return await resolveVersion(versionFromPyproject || "latest", githubToken);
}
function addRuffToPath(cachedPath: string): void {
core.addPath(cachedPath);
core.info(`Added ${cachedPath} to the path`);