2022-03-01 13:54:49 +08:00
|
|
|
import { addPath } from "../utils/env/addEnv"
|
2021-09-18 20:39:59 +08:00
|
|
|
import { setupAptPack } from "../utils/setup/setupAptPack"
|
2022-06-30 00:06:35 +08:00
|
|
|
import { setupPacmanPack } from "../utils/setup/setupPacmanPack"
|
2021-09-18 20:39:59 +08:00
|
|
|
import { setupBrewPack } from "../utils/setup/setupBrewPack"
|
|
|
|
|
import { setupChocoPack } from "../utils/setup/setupChocoPack"
|
2022-07-11 07:11:32 +08:00
|
|
|
import { isGitHubCI } from "../utils/env/isCI"
|
2022-05-03 12:44:28 +08:00
|
|
|
import { warning, info } from "../utils/io/io"
|
2022-06-30 10:06:33 +08:00
|
|
|
import { isArch } from "../utils/env/isArch"
|
2022-06-30 12:47:20 +08:00
|
|
|
import which from "which"
|
|
|
|
|
import { InstallationInfo } from "../utils/setup/setupBin"
|
|
|
|
|
import { dirname, join } from "path"
|
2022-07-11 07:34:56 +08:00
|
|
|
import { hasDnf } from "../utils/env/hasDnf"
|
|
|
|
|
import { setupDnfPack } from "../utils/setup/setupDnfPack"
|
2021-09-15 04:23:45 +08:00
|
|
|
|
2022-04-25 07:47:52 +08:00
|
|
|
export async function setupPython(version: string, setupDir: string, arch: string) {
|
2022-02-05 08:02:04 +08:00
|
|
|
if (!isGitHubCI()) {
|
2022-02-05 04:10:13 +08:00
|
|
|
// TODO parse version
|
|
|
|
|
return setupPythonViaSystem(version, setupDir, arch)
|
2021-09-18 20:50:31 +08:00
|
|
|
}
|
2021-09-15 04:23:45 +08:00
|
|
|
try {
|
2022-07-06 12:56:30 +08:00
|
|
|
info("Installing python in GitHub Actions")
|
2022-04-25 07:47:52 +08:00
|
|
|
const { setupActionsPython } = await import("./actions_python")
|
2021-11-22 02:46:34 +08:00
|
|
|
return setupActionsPython(version, setupDir, arch)
|
2021-09-15 04:23:45 +08:00
|
|
|
} catch (err) {
|
2022-05-03 12:44:28 +08:00
|
|
|
warning((err as Error).toString())
|
2021-11-22 02:46:34 +08:00
|
|
|
return setupPythonViaSystem(version, setupDir, arch)
|
2021-09-18 20:39:59 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-30 12:47:20 +08:00
|
|
|
export async function setupPythonViaSystem(
|
|
|
|
|
version: string,
|
|
|
|
|
setupDir: string,
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
|
_arch: string
|
|
|
|
|
): Promise<InstallationInfo> {
|
2021-09-18 20:39:59 +08:00
|
|
|
switch (process.platform) {
|
|
|
|
|
case "win32": {
|
2022-02-05 08:02:04 +08:00
|
|
|
if (setupDir) {
|
2022-05-13 03:55:00 +08:00
|
|
|
await setupChocoPack("python3", version, [`--params=/InstallDir:${setupDir}`])
|
2022-02-05 08:02:04 +08:00
|
|
|
} else {
|
2022-05-13 03:55:00 +08:00
|
|
|
await setupChocoPack("python3", version)
|
2022-02-05 08:02:04 +08:00
|
|
|
}
|
2021-09-18 20:39:59 +08:00
|
|
|
// Adding the bin dir to the path
|
2022-06-30 12:47:20 +08:00
|
|
|
const pythonBinPath =
|
|
|
|
|
which.sync("python3.exe", { nothrow: true }) ??
|
|
|
|
|
which.sync("python.exe", { nothrow: true }) ??
|
|
|
|
|
join(setupDir, "python.exe")
|
|
|
|
|
const pythonSetupDir = dirname(pythonBinPath)
|
2021-09-18 20:39:59 +08:00
|
|
|
/** The directory which the tool is installed to */
|
2022-06-30 12:47:20 +08:00
|
|
|
await activateWinPython(pythonSetupDir)
|
|
|
|
|
return { installDir: pythonSetupDir, binDir: pythonSetupDir }
|
2021-09-18 20:39:59 +08:00
|
|
|
}
|
|
|
|
|
case "darwin": {
|
|
|
|
|
return setupBrewPack("python3", version)
|
|
|
|
|
}
|
|
|
|
|
case "linux": {
|
2022-07-11 07:34:56 +08:00
|
|
|
let installInfo: InstallationInfo
|
2022-06-30 10:06:33 +08:00
|
|
|
if (isArch()) {
|
2022-07-11 07:34:56 +08:00
|
|
|
installInfo = setupPacmanPack("python", version)
|
2022-06-30 00:06:35 +08:00
|
|
|
setupPacmanPack("python-pip")
|
2022-07-11 07:34:56 +08:00
|
|
|
} else if (hasDnf()) {
|
|
|
|
|
installInfo = setupDnfPack("python3", version)
|
|
|
|
|
} else {
|
|
|
|
|
installInfo = setupAptPack("python3", version)
|
|
|
|
|
setupAptPack("python3-pip")
|
2022-06-30 00:06:35 +08:00
|
|
|
}
|
2021-09-18 20:39:59 +08:00
|
|
|
return installInfo
|
|
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
throw new Error(`Unsupported platform`)
|
|
|
|
|
}
|
2021-09-15 04:23:45 +08:00
|
|
|
}
|
|
|
|
|
}
|
2021-09-20 20:35:17 +08:00
|
|
|
|
2022-05-13 03:55:00 +08:00
|
|
|
async function activateWinPython(binDir: string) {
|
2022-05-03 12:44:28 +08:00
|
|
|
info(`Add ${binDir} to PATH`)
|
2022-05-13 03:55:00 +08:00
|
|
|
await addPath(binDir)
|
2021-09-20 20:35:17 +08:00
|
|
|
}
|