]> Git Repo - pico-vscode.git/blob - src/utils/requirementsUtil.mts
Merge branch 'main' into main
[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, HOME_VAR } from "../settings.mjs";
5 import { homedir } from "os";
6 import { downloadGit } from "./downloadGit.mjs";
7 import Logger, { LoggerSource } from "../logger.mjs";
8
9 /**
10  * Shows an error message that the requirements for development are not met
11  * and lists the missing requirements.
12  *
13  * @param missing The missing requirements.
14  */
15 export async function showRequirementsNotMetErrorMessage(
16   missing: string[]
17 ): Promise<void> {
18   await window.showErrorMessage(
19     "Development for the Pico requires " +
20       missing.join(", ") +
21       " to be installed and available in PATH. " +
22       "Please install and restart VS Code."
23   );
24 }
25
26 /**
27  * Checks if a git executable is avialable or try to install if possible.
28  *
29  * @returns True if git is available, false otherwise.
30  */
31 export async function checkForGit(settings: Settings): Promise<boolean> {
32   const gitExe: string =
33     settings
34       .getString(SettingsKey.gitPath)
35       ?.replace(HOME_VAR, homedir().replaceAll("\\", "/")) || "git";
36
37   const git: string | null = await which(gitExe, { nothrow: true });
38
39   let isGitInstalled = git !== null;
40   if (!isGitInstalled) {
41     // try to install
42     const gitDownloaded: string | undefined = await downloadGit();
43
44     if (gitDownloaded !== undefined) {
45       // if git is downloaded set custom git path
46       await settings.updateGlobal(SettingsKey.gitPath, gitDownloaded);
47       isGitInstalled = true;
48     } else {
49       Logger.error(
50         LoggerSource.requirements,
51         "Git is not installed and could not be downloaded."
52       );
53       void window.showErrorMessage(
54         "The installation of the Pico SDK requires Git " +
55           "to be installed and available in the PATH." +
56           (process.platform === "darwin"
57             ? " You can install it by running `xcode-select --install`" +
58               " in the terminal and restart your computer."
59             : "")
60       );
61     }
62   }
63
64   return isGitInstalled;
65 }
This page took 0.026579 seconds and 4 git commands to generate.