add support for python-version-file (#336)

* add support for python-version-file

* Update action.yml

* update to v4, remove python-version default

* python-version overrides python-version-file, like setup-node
* checks '.python-version' by default if nothing else specified

* update tests, update to checkout@v3

* update build

* appease the linter

* remove old test for default python version

* revert readme changes

* update build
This commit is contained in:
Andrew DiLosa
2022-06-02 07:37:57 -07:00
committed by GitHub
parent 3f82819745
commit 53e15292cd
5 changed files with 98 additions and 32 deletions

View File

@ -3,6 +3,7 @@ import * as finder from './find-python';
import * as finderPyPy from './find-pypy';
import * as path from 'path';
import * as os from 'os';
import fs from 'fs';
import {getCacheDistributor} from './cache-distributions/cache-factory';
import {isCacheFeatureAvailable} from './utils';
@ -21,6 +22,35 @@ async function cacheDependencies(cache: string, pythonVersion: string) {
await cacheDistributor.restoreCache();
}
function resolveVersionInput(): string {
let version = core.getInput('python-version');
const versionFile = core.getInput('python-version-file');
if (version && versionFile) {
core.warning(
'Both python-version and python-version-file inputs are specified, only python-version will be used'
);
}
if (version) {
return version;
}
const versionFilePath = path.join(
process.env.GITHUB_WORKSPACE!,
versionFile || '.python-version'
);
if (!fs.existsSync(versionFilePath)) {
throw new Error(
`The specified python version file at: ${versionFilePath} does not exist`
);
}
version = fs.readFileSync(versionFilePath, 'utf8');
core.info(`Resolved ${versionFile} as ${version}`);
return version;
}
async function run() {
if (process.env.AGENT_TOOLSDIRECTORY?.trim()) {
core.debug(
@ -33,7 +63,7 @@ async function run() {
);
}
try {
const version = core.getInput('python-version');
const version = resolveVersionInput();
if (version) {
let pythonVersion: string;
const arch: string = core.getInput('architecture') || os.arch();