]> Git Repo - pico-vscode.git/blob - src/utils/pyenvUtil.mts
Add elf2uf2 and pioasm for sdk 1.5.1
[pico-vscode.git] / src / utils / pyenvUtil.mts
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";
10
11 export function buildPyEnvPath(): string {
12   // TODO: maybe replace . with _
13   return joinPosix(homedir().replaceAll("\\", "/"), ".pico-sdk", "pyenv");
14 }
15
16 /**
17  * Download pyenv and install it.
18  */
19 export async function setupPyenv(): Promise<boolean> {
20   const targetDirectory = buildPyEnvPath();
21   const result = await cloneRepository(
22     PYENV_REPOSITORY_URL,
23     "master",
24     targetDirectory
25   );
26
27   if (!result) {
28     return false;
29   }
30
31   return true;
32 }
33
34 export async function pyenvInstallPython(
35   version: string
36 ): Promise<string | null> {
37   const targetDirectory = buildPyEnvPath();
38   const binDirectory = joinPosix(targetDirectory, "bin");
39   const command = `${binDirectory}/pyenv install ${version}`;
40
41   const customEnv = { ...process.env };
42   customEnv["PYENV_ROOT"] = targetDirectory;
43   customEnv[
44     process.platform === "win32" ? "Path" : "PATH"
45   ] = `${binDirectory};${customEnv["PATH"]}`;
46
47   const settingsTarget =
48     `${HOME_VAR}/.pico-sdk` + `/python/${version}/python.exe`;
49   const pythonVersionPath = buildPython3Path(version);
50
51   if (existsSync(pythonVersionPath)) {
52     return settingsTarget;
53   }
54
55   return new Promise(resolve => {
56     exec(
57       command,
58       { env: customEnv, cwd: binDirectory },
59       (error, stdout, stderr) => {
60         if (error) {
61           resolve(null);
62         }
63
64         const versionFolder = joinPosix(targetDirectory, "versions", version);
65         const pyBin = joinPosix(versionFolder, "bin");
66         mkdirSync(pythonVersionPath, { recursive: true });
67         symlinkSync(
68           joinPosix(pyBin, "python3"),
69           joinPosix(pythonVersionPath, "python.exe")
70         );
71         symlinkSync(
72           joinPosix(pyBin, "python3"),
73           joinPosix(pythonVersionPath, "python3exe")
74         );
75
76         resolve(settingsTarget);
77       }
78     );
79   });
80 }
This page took 0.026066 seconds and 4 git commands to generate.