mirror of
https://github.com/actions/setup-python.git
synced 2025-06-13 15:57:13 +02:00
Add support for .tool-versions file in setup-python (#1043)
* add support for .tool-versions file * update regex * optimize code * update test-python.yml for .tool-versions * fix format-check errors * fix formatting in test-python.yml * Fix test-python.yml error * workflow update with latest versions * update test cases * fix lint issue
This commit is contained in:
36
src/utils.ts
36
src/utils.ts
@ -279,11 +279,45 @@ export function getVersionInputFromPlainFile(versionFile: string): string[] {
|
||||
}
|
||||
|
||||
/**
|
||||
* Python version extracted from a plain or TOML file.
|
||||
* Python version extracted from a .tool-versions file.
|
||||
*/
|
||||
export function getVersionInputFromToolVersions(versionFile: string): string[] {
|
||||
if (!fs.existsSync(versionFile)) {
|
||||
core.warning(`File ${versionFile} does not exist.`);
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
const fileContents = fs.readFileSync(versionFile, 'utf8');
|
||||
const lines = fileContents.split('\n');
|
||||
|
||||
for (const line of lines) {
|
||||
// Skip commented lines
|
||||
if (line.trim().startsWith('#')) {
|
||||
continue;
|
||||
}
|
||||
const match = line.match(/^\s*python\s*v?\s*(?<version>[^\s]+)\s*$/);
|
||||
if (match) {
|
||||
return [match.groups?.version.trim() || ''];
|
||||
}
|
||||
}
|
||||
|
||||
core.warning(`No Python version found in ${versionFile}`);
|
||||
|
||||
return [];
|
||||
} catch (error) {
|
||||
core.error(`Error reading ${versionFile}: ${(error as Error).message}`);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Python version extracted from a plain, .tool-versions or TOML file.
|
||||
*/
|
||||
export function getVersionInputFromFile(versionFile: string): string[] {
|
||||
if (versionFile.endsWith('.toml')) {
|
||||
return getVersionInputFromTomlFile(versionFile);
|
||||
} else if (versionFile.match('.tool-versions')) {
|
||||
return getVersionInputFromToolVersions(versionFile);
|
||||
} else {
|
||||
return getVersionInputFromPlainFile(versionFile);
|
||||
}
|
||||
|
Reference in New Issue
Block a user