mirror of
https://code.forgejo.org/actions/setup-python
synced 2025-06-25 11:58:01 +02:00
Enhance reading from .python-version (#787)
* Enhance reading from .python-version * Fix typos * Fix lint * Add built files * Don't use EOL versions in `utils.test.ts` * Fix Prettier * Don't use unreleased versions in `utils.test.ts` * Update versions in `utils.test.ts` again
This commit is contained in:
parent
a26af69be9
commit
5db1cf9a59
4 changed files with 98 additions and 40 deletions
|
@ -11,7 +11,7 @@ import {
|
|||
logWarning,
|
||||
IS_MAC,
|
||||
getVersionInputFromFile,
|
||||
getVersionInputFromPlainFile
|
||||
getVersionsInputFromPlainFile
|
||||
} from './utils';
|
||||
|
||||
function isPyPyVersion(versionSpec: string) {
|
||||
|
@ -35,7 +35,7 @@ async function cacheDependencies(cache: string, pythonVersion: string) {
|
|||
|
||||
function resolveVersionInputFromDefaultFile(): string[] {
|
||||
const couples: [string, (versionFile: string) => string[]][] = [
|
||||
['.python-version', getVersionInputFromPlainFile]
|
||||
['.python-version', getVersionsInputFromPlainFile]
|
||||
];
|
||||
for (const [versionFile, _fn] of couples) {
|
||||
logWarning(
|
||||
|
|
31
src/utils.ts
31
src/utils.ts
|
@ -228,7 +228,7 @@ function extractValue(obj: any, keys: string[]): string | undefined {
|
|||
* If none is present, returns an empty list.
|
||||
*/
|
||||
export function getVersionInputFromTomlFile(versionFile: string): string[] {
|
||||
core.debug(`Trying to resolve version form ${versionFile}`);
|
||||
core.debug(`Trying to resolve version from ${versionFile}`);
|
||||
|
||||
let pyprojectFile = fs.readFileSync(versionFile, 'utf8');
|
||||
// Normalize the line endings in the pyprojectFile
|
||||
|
@ -269,13 +269,28 @@ export function getVersionInputFromTomlFile(versionFile: string): string[] {
|
|||
}
|
||||
|
||||
/**
|
||||
* Python version extracted from a plain text file.
|
||||
* Python versions extracted from a plain text file.
|
||||
* - Resolves multiple versions from multiple lines.
|
||||
* - Handles pyenv-virtualenv pointers (e.g. `3.10/envs/virtualenv`).
|
||||
* - Ignores empty lines and lines starting with `#`
|
||||
* - Trims whitespace.
|
||||
*/
|
||||
export function getVersionInputFromPlainFile(versionFile: string): string[] {
|
||||
core.debug(`Trying to resolve version form ${versionFile}`);
|
||||
const version = fs.readFileSync(versionFile, 'utf8').trim();
|
||||
core.info(`Resolved ${versionFile} as ${version}`);
|
||||
return [version];
|
||||
export function getVersionsInputFromPlainFile(versionFile: string): string[] {
|
||||
core.debug(`Trying to resolve versions from ${versionFile}`);
|
||||
const content = fs.readFileSync(versionFile, 'utf8').trim();
|
||||
const lines = content.split(/\r\n|\r|\n/);
|
||||
const versions = lines
|
||||
.map(line => {
|
||||
if (line.startsWith('#') || line.trim() === '') {
|
||||
return undefined;
|
||||
}
|
||||
let version: string = line.trim();
|
||||
version = version.split('/')[0];
|
||||
return version;
|
||||
})
|
||||
.filter(version => version !== undefined) as string[];
|
||||
core.info(`Resolved ${versionFile} as ${versions.join(', ')}`);
|
||||
return versions;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -319,7 +334,7 @@ export function getVersionInputFromFile(versionFile: string): string[] {
|
|||
} else if (versionFile.match('.tool-versions')) {
|
||||
return getVersionInputFromToolVersions(versionFile);
|
||||
} else {
|
||||
return getVersionInputFromPlainFile(versionFile);
|
||||
return getVersionsInputFromPlainFile(versionFile);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue