1 import { homedir } from "os";
2 import { cloneRepository } from "./gitUtil.mjs";
3 import { PYENV_REPOSITORY_URL } from "./githubREST.mjs";
4 import { join as joinPosix } from "path/posix";
5 import { exec } from "child_process";
6 import { buildPython3Path } from "./download.mjs";
7 import { HOME_VAR } from "../settings.mjs";
8 import { existsSync, mkdirSync, symlinkSync } from "fs";
9 import { join } from "path";
11 export function buildPyEnvPath(): string {
12 // TODO: maybe replace . with _
13 return joinPosix(homedir().replaceAll("\\", "/"), ".pico-sdk", "pyenv");
17 * Download pyenv and install it.
19 export async function setupPyenv(): Promise<boolean> {
20 const targetDirectory = buildPyEnvPath();
21 const result = await cloneRepository(
34 export async function pyenvInstallPython(
36 ): Promise<string | null> {
37 const targetDirectory = buildPyEnvPath();
38 const binDirectory = joinPosix(targetDirectory, "bin");
39 const command = `${binDirectory}/pyenv install ${version}`;
41 const customEnv = { ...process.env };
42 customEnv["PYENV_ROOT"] = targetDirectory;
44 process.platform === "win32" ? "Path" : "PATH"
45 ] = `${binDirectory};${customEnv["PATH"]}`;
47 const settingsTarget =
48 `${HOME_VAR}/.pico-sdk` + `/python/${version}/python.exe`;
49 const pythonVersionPath = buildPython3Path(version);
51 if (existsSync(pythonVersionPath)) {
52 return settingsTarget;
55 return new Promise(resolve => {
58 { env: customEnv, cwd: binDirectory },
59 (error, stdout, stderr) => {
64 const versionFolder = joinPosix(targetDirectory, "versions", version);
65 const pyBin = joinPosix(versionFolder, "bin");
66 mkdirSync(pythonVersionPath, { recursive: true });
68 joinPosix(pyBin, "python3"),
69 joinPosix(pythonVersionPath, "python.exe")
72 joinPosix(pyBin, "python3"),
73 joinPosix(pythonVersionPath, "python3exe")
76 resolve(settingsTarget);