fix: use input python version if compatible

This commit is contained in:
Frost Ming 2022-07-21 22:12:25 +08:00
parent 06d2d28e10
commit 1912606aa4
No known key found for this signature in database
GPG Key ID: 5BFA9CB4DDA943BF
4 changed files with 988 additions and 75 deletions

View File

@ -11,7 +11,7 @@ jobs:
os: ["windows-latest", "ubuntu-latest"] os: ["windows-latest", "ubuntu-latest"]
name: Test the action name: Test the action
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v3
- name: Setup PDM - name: Setup PDM
uses: ./ uses: ./
with: with:

957
dist/setup-pdm.js vendored

File diff suppressed because it is too large Load Diff

View File

@ -13,6 +13,7 @@
"dependencies": { "dependencies": {
"@actions/core": "^1.2.6", "@actions/core": "^1.2.6",
"@actions/exec": "^1.0.4", "@actions/exec": "^1.0.4",
"semver": "^7.3.7",
"setup-python": "actions/setup-python" "setup-python": "actions/setup-python"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,79 +1,60 @@
import * as core from "@actions/core"; import * as core from '@actions/core'
import * as exec from "@actions/exec"; import * as exec from '@actions/exec'
import * as setupPython from "setup-python/src/find-python"; import * as setupPython from 'setup-python/src/find-python'
import { IS_WINDOWS } from "setup-python/src/utils"; import { IS_WINDOWS } from 'setup-python/src/utils'
import * as os from "os"; import * as os from 'os'
import { exec as execChild } from "child_process"; import { promises as fs } from 'fs'
import { promises as fs } from "fs"; import path from 'path'
import path from "path"; import semParse from 'semver/functions/parse'
import semIntersect from 'semver/ranges/intersects'
const INSTALL_VERSION = "3.8"; const PDM_PYTHON_REQUIRES = '>=3.7'
const GITHUB_REPO = "https://github.com/pdm-project/pdm.git"; const FALLBACK_INSTALL_VERSION = '3.10'
const GITHUB_REPO = 'https://github.com/pdm-project/pdm.git'
function getPep582Path(): string { function getPep582Path(version: string): string {
const installDir = process.env.pythonLocation || ""; const installDir = process.env.pythonLocation || ''
const parsedVersion = semParse(version)!
if (IS_WINDOWS) { if (IS_WINDOWS) {
return path.resolve(installDir, "Lib/site-packages/pdm/pep582"); return path.resolve(installDir, 'Lib/site-packages/pdm/pep582')
} else { } else {
return path.resolve( return path.resolve(installDir, 'lib', `python${parsedVersion.major}.${parsedVersion.minor}`, 'site-packages/pdm/pep582')
installDir,
"lib",
`python${INSTALL_VERSION}`,
"site-packages/pdm/pep582"
);
} }
} }
async function run(): Promise<void> { async function run(): Promise<void> {
const arch = core.getInput("architecture") || os.arch(); const arch = core.getInput('architecture') || os.arch()
const pdmVersion = core.getInput("version"); const pdmVersion = core.getInput('version')
const ref = core.getInput("ref"); const pythonVersion = core.getInput('python-version')
const pdmPackage = pdmVersion const versionCompatible = semIntersect(PDM_PYTHON_REQUIRES, pythonVersion)
? `pdm==${pdmVersion}` const ref = core.getInput('ref')
: ref const pdmPackage = pdmVersion ? `pdm==${pdmVersion}` : ref ? `pdm @ git+${GITHUB_REPO}@${ref}` : 'pdm'
? `pdm @ git+${GITHUB_REPO}@${ref}` const cmdArgs = ['-m', 'pip', 'install', '-U', pdmPackage]
: "pdm"; if (core.getInput('prerelease') === 'true') {
const cmdArgs = ["-m", "pip", "install", "-U", pdmPackage]; cmdArgs.push('--pre')
if (core.getInput("prerelease") === "true") {
cmdArgs.push("--pre");
} }
try { try {
let installedPython = await setupPython.findPythonVersion( let installedPython = await setupPython.findPythonVersion(versionCompatible ? pythonVersion : FALLBACK_INSTALL_VERSION, arch)
INSTALL_VERSION, await exec.exec('python', cmdArgs)
arch if (core.getInput('enable-pep582') === 'true') {
); core.exportVariable('PYTHONPATH', getPep582Path(installedPython.version))
await exec.exec("python", cmdArgs);
if (core.getInput("enable-pep582") === "true") {
core.exportVariable("PYTHONPATH", getPep582Path());
} }
if (core.getInput("python-version") !== INSTALL_VERSION) { if (!versionCompatible) {
installedPython = await setupPython.findPythonVersion( installedPython = await setupPython.findPythonVersion(pythonVersion, arch)
core.getInput("python-version"),
arch
);
} }
const pythonBin = path const pythonBin = path.join(process.env.pythonLocation as string, IS_WINDOWS ? 'python.exe' : 'bin/python').replace(/\\/g, '/')
.join( await fs.writeFile('.pdm.toml', `[python]\npath="${pythonBin}"\n`)
process.env.pythonLocation as string, const { stdout: pdmVersionOutput } = await exec.getExecOutput('pdm --version')
IS_WINDOWS ? "python.exe" : "bin/python" if (process.platform === 'linux') {
)
.replace(/\\/g, "/");
await fs.writeFile(".pdm.toml", `[python]\npath="${pythonBin}"\n`);
const pdmVersionOutput = (await execChild("pdm --version")).stdout;
if (process.platform === "linux") {
// See https://github.com/actions/virtual-environments/issues/2803 // See https://github.com/actions/virtual-environments/issues/2803
core.exportVariable("LD_PRELOAD", "/lib/x86_64-linux-gnu/libgcc_s.so.1"); core.exportVariable('LD_PRELOAD', '/lib/x86_64-linux-gnu/libgcc_s.so.1')
} }
core.info( core.info(`Successfully setup ${pdmVersionOutput} with Python ${installedPython.version}`)
`Successfully setup ${ const matchersPath = path.join(__dirname, '..', '.github')
pdmVersionOutput && pdmVersionOutput.read() core.info(`##[add-matcher]${path.join(matchersPath, 'python.json')}`)
} with Python ${installedPython.version}`
);
const matchersPath = path.join(__dirname, "..", ".github");
core.info(`##[add-matcher]${path.join(matchersPath, "python.json")}`);
} catch (error: any) { } catch (error: any) {
core.setFailed(error.message); core.setFailed(error.message)
} }
} }
run(); run()