Skip to content

Commit

Permalink
set output for the installed version
Browse files Browse the repository at this point in the history
  • Loading branch information
rdaumann committed Jan 29, 2020
1 parent 9d6ce31 commit 49d27c4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ inputs:
architecture:
description: 'The target architecture (x86, x64) of the Python interpreter.'
default: 'x64'
outputs:
python-version:
description: "The installed python version. Useful when given a version range as input."
runs:
using: 'node12'
main: 'dist/index.js'
Expand Down
18 changes: 11 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3005,7 +3005,9 @@ function usePyPy(majorVersion, architecture) {
core.exportVariable('pythonLocation', pythonLocation);
core.addPath(installDir);
core.addPath(_binDir);
return versionFromPath(installDir);
const impl = 'pypy' + majorVersion.toString();
core.setOutput('python-version', impl);
return { impl: impl, version: versionFromPath(installDir) };
}
function useCpythonVersion(version, architecture) {
return __awaiter(this, void 0, void 0, function* () {
Expand Down Expand Up @@ -3044,7 +3046,9 @@ function useCpythonVersion(version, architecture) {
core.addPath(userScriptsDir);
}
// On Linux and macOS, pip will create the --user directory and add it to PATH as needed.
return versionFromPath(installDir);
const installed = versionFromPath(installDir);
core.setOutput('python-version', installed);
return { impl: 'CPython', version: installed };
});
}
/** Convert versions like `3.8-dev` to a version like `>= 3.8.0-a0`. */
Expand All @@ -3059,9 +3063,9 @@ function desugarDevVersion(versionSpec) {
}
/** Extracts python version from install path from hosted tool cache as described in README.md */
function versionFromPath(installDir) {
let parts = installDir.split(path.sep);
let idx = parts.findIndex(part => part === 'PyPy' || part === 'Python');
return parts.slice(idx, idx + 2).join(' ');
const parts = installDir.split(path.sep);
const idx = parts.findIndex(part => part === 'PyPy' || part === 'Python');
return parts[idx + 1] || '';
}
/**
* Python's prelease versions look like `3.7.0b2`.
Expand Down Expand Up @@ -3714,8 +3718,8 @@ function run() {
let version = core.getInput('python-version');
if (version) {
const arch = core.getInput('architecture', { required: true });
let installed = yield finder.findPythonVersion(version, arch);
console.log(`Successfully setup ${installed}.`);
const installed = yield finder.findPythonVersion(version, arch);
console.log(`Successfully setup ${installed.impl} (${installed.version}).`);
}
const matchersPath = path.join(__dirname, '..', '.github');
console.log(`##[add-matcher]${path.join(matchersPath, 'python.json')}`);
Expand Down
27 changes: 19 additions & 8 deletions src/find-python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function binDir(installDir: string): string {
// A particular version of PyPy may contain one or more versions of the Python interpreter.
// For example, PyPy 7.0 contains Python 2.7, 3.5, and 3.6-alpha.
// We only care about the Python version, so we don't use the PyPy version for the tool cache.
function usePyPy(majorVersion: 2 | 3, architecture: string): string {
function usePyPy(majorVersion: 2 | 3, architecture: string): InstalledVersion {
const findPyPy = tc.find.bind(undefined, 'PyPy', majorVersion.toString());
let installDir: string | null = findPyPy(architecture);

Expand All @@ -78,13 +78,16 @@ function usePyPy(majorVersion: 2 | 3, architecture: string): string {
core.addPath(installDir);
core.addPath(_binDir);

return versionFromPath(installDir);
const impl = 'pypy' + majorVersion.toString();
core.setOutput('python-version', impl);

return {impl: impl, version: versionFromPath(installDir)};
}

async function useCpythonVersion(
version: string,
architecture: string
): Promise<string> {
): Promise<InstalledVersion> {
const desugaredVersionSpec = desugarDevVersion(version);
const semanticVersionSpec = pythonVersionToSemantic(desugaredVersionSpec);
core.debug(`Semantic version spec of ${version} is ${semanticVersionSpec}`);
Expand Down Expand Up @@ -138,7 +141,10 @@ async function useCpythonVersion(
}
// On Linux and macOS, pip will create the --user directory and add it to PATH as needed.

return versionFromPath(installDir);
const installed = versionFromPath(installDir);
core.setOutput('python-version', installed);

return {impl: 'CPython', version: installed};
}

/** Convert versions like `3.8-dev` to a version like `>= 3.8.0-a0`. */
Expand All @@ -153,10 +159,15 @@ function desugarDevVersion(versionSpec: string) {

/** Extracts python version from install path from hosted tool cache as described in README.md */
function versionFromPath(installDir: string) {
let parts = installDir.split(path.sep);
let idx = parts.findIndex(part => part === 'PyPy' || part === 'Python');
const parts = installDir.split(path.sep);
const idx = parts.findIndex(part => part === 'PyPy' || part === 'Python');

return parts[idx + 1] || '';
}

return parts.slice(idx, idx + 2).join(' ');
interface InstalledVersion {
impl: string;
version: string;
}

/**
Expand All @@ -172,7 +183,7 @@ export function pythonVersionToSemantic(versionSpec: string) {
export async function findPythonVersion(
version: string,
architecture: string
): Promise<string> {
): Promise<InstalledVersion> {
switch (version.toUpperCase()) {
case 'PYPY2':
return usePyPy(2, architecture);
Expand Down
6 changes: 4 additions & 2 deletions src/setup-python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ async function run() {
let version = core.getInput('python-version');
if (version) {
const arch: string = core.getInput('architecture', {required: true});
let installed = await finder.findPythonVersion(version, arch);
console.log(`Successfully setup ${installed}.`);
const installed = await finder.findPythonVersion(version, arch);
console.log(
`Successfully setup ${installed.impl} (${installed.version}).`
);
}
const matchersPath = path.join(__dirname, '..', '.github');
console.log(`##[add-matcher]${path.join(matchersPath, 'python.json')}`);
Expand Down

0 comments on commit 49d27c4

Please sign in to comment.