]> Git Repo - pico-vscode.git/blob - src/utils/requirementsUtil.mts
Add elf2uf2 and pioasm for sdk 1.5.1
[pico-vscode.git] / src / utils / requirementsUtil.mts
1 import { window } from "vscode";
2 import which from "which";
3 import type Settings from "../settings.mjs";
4 import { SettingsKey } from "../settings.mjs";
5 import { downloadGit } from "./download.mjs";
6 import Logger from "../logger.mjs";
7
8 export async function showRequirementsNotMetErrorMessage(
9   missing: string[]
10 ): Promise<void> {
11   await window.showErrorMessage(
12     "Development for the Pico (W) requires " +
13       missing.join(", ") +
14       " to be installed and available in the PATH. " +
15       "Please install and restart VS Code."
16   );
17 }
18
19 /**
20  * Checks if all requirements for installing a Pico-SDK are met
21  * TODO: add support for custom compiler and git paths in settings
22  *
23  * @returns true if all requirements are met, false otherwise
24  */
25 export async function checkForInstallationRequirements(
26   settings: Settings,
27   gitPath?: string
28 ): Promise<boolean> {
29   const gitExe: string = gitPath || "git";
30   const compilerExe: string[] = ["clang", "gcc", "cl"];
31
32   const git: string | null = await which(gitExe, { nothrow: true });
33   //check if any of the compilers is available
34   const compiler: string | null = await Promise.any(
35     compilerExe
36       .map(compiler => which(compiler, { nothrow: true }))
37       .map(p => p.catch(() => null))
38   );
39   // set availability of tools on windows
40   const allToolsAvailable: boolean = process.platform === "win32";
41
42   let requirementsMet: boolean = true;
43   if (git === null) {
44     if (process.platform === "linux") {
45       requirementsMet = false;
46     } else if (process.platform === "darwin") {
47       void window.showErrorMessage(
48         "Installation of the Pico-SDK requires Git to be installed " +
49           "and in PATH. You can install it by running " +
50           "`xcode-select --install` in the terminal and restart your computer."
51       );
52       requirementsMet = false;
53     } else {
54       // install if not available
55       const gitDownloaded: string | undefined = await downloadGit();
56
57       if (gitDownloaded === undefined) {
58         Logger.log("Error: Git is not installed and could not be downloaded.");
59         requirementsMet = false;
60       } else {
61         // if git is downloaded set custom git path
62         await settings.updateGlobal(SettingsKey.gitPath, gitDownloaded);
63       }
64     }
65   }
66   if (!allToolsAvailable) {
67     // only check for compilers if pioasm
68     if (compiler === null) {
69       requirementsMet = false;
70     }
71   }
72
73   if (!requirementsMet) {
74     void showInstallationRequirementsNotMetErrorMessage(
75       git !== null,
76       allToolsAvailable || compiler !== null
77     );
78   }
79
80   return requirementsMet;
81 }
82
83 export async function showInstallationRequirementsNotMetErrorMessage(
84   isGitInstalled: boolean,
85   allToolsAvailableOrCompilerInstalled: boolean
86 ): Promise<void> {
87   if (!isGitInstalled && process.platform !== "darwin") {
88     await window.showErrorMessage(
89       "Installation of the Pico-SDK requires Git " +
90         "to be installed and available in the PATH."
91     );
92   }
93   if (!allToolsAvailableOrCompilerInstalled) {
94     await window.showErrorMessage(
95       "A native C/C++ compiler (clang or gcc) needs to be installed and in " +
96         "PATH for manual compilation of the tools." +
97         "Please install and restart VS Code."
98     );
99   }
100 }
This page took 0.031933 seconds and 4 git commands to generate.