]> Git Repo - pico-vscode.git/blob - src/commands/switchSDK.mts
Install OpenOCD and tools from github
[pico-vscode.git] / src / commands / switchSDK.mts
1 import { Command } from "./command.mjs";
2 import { ProgressLocation, window, workspace } from "vscode";
3 import type UI from "../ui.mjs";
4 import { updateVSCodeStaticConfigs } from "../utils/vscodeConfigUtil.mjs";
5 import { SDK_REPOSITORY_URL, getSDKReleases } from "../utils/githubREST.mjs";
6 import {
7   downloadAndInstallSDK,
8   downloadAndInstallToolchain,
9   downloadAndInstallTools,
10 } from "../utils/download.mjs";
11 import { cmakeUpdateSDK } from "../utils/cmakeUtil.mjs";
12 import {
13   type SupportedToolchainVersion,
14   getSupportedToolchains,
15 } from "../utils/toolchainUtil.mjs";
16 import type Settings from "../settings.mjs";
17
18 export default class SwitchSDKCommand extends Command {
19   private _ui: UI;
20   private _settings: Settings;
21
22   constructor(ui: UI, settings: Settings) {
23     super("switchSDK");
24
25     this._ui = ui;
26     this._settings = settings;
27   }
28
29   async execute(): Promise<void> {
30     if (
31       workspace.workspaceFolders === undefined ||
32       workspace.workspaceFolders.length === 0
33     ) {
34       void window.showErrorMessage("Please open a Pico project folder first.");
35
36       return;
37     }
38
39     const workspaceFolder = workspace.workspaceFolders[0];
40
41     const sdks = await getSDKReleases();
42
43     // show quick pick
44     const selectedSDK = await window.showQuickPick(
45       sdks.map(sdk => ({
46         label: `v${sdk.tagName}`,
47         sdk: sdk,
48       })),
49       {
50         placeHolder: "Select SDK version",
51       }
52     );
53
54     if (selectedSDK === undefined) {
55       return;
56     }
57
58     let selectedToolchainVersion:
59       | { label: string; toolchain: SupportedToolchainVersion }
60       | undefined;
61
62     try {
63       const supportedToolchainVersions = await getSupportedToolchains();
64       // show quick pick for toolchain version
65       selectedToolchainVersion = await window.showQuickPick(
66         supportedToolchainVersions.map(toolchain => ({
67           label: toolchain.version.replaceAll("_", "."),
68           toolchain: toolchain,
69         })),
70         {
71           placeHolder: "Select ARM Embeded Toolchain version",
72         }
73       );
74     } catch (error) {
75       void window.showErrorMessage(
76         "Failed to get supported toolchain versions. " +
77           "Make sure you are connected to the internet."
78       );
79
80       return;
81     }
82
83     if (selectedToolchainVersion === undefined) {
84       return;
85     }
86
87     // show progress bar while installing
88     const result = await window.withProgress(
89       {
90         title:
91           `Installing SDK ${selectedSDK.label} and ` +
92           `toolchain ${selectedToolchainVersion.label}...`,
93         location: ProgressLocation.Notification,
94       },
95       async progress => {
96         // download and install selected SDK
97         if (
98           await downloadAndInstallSDK(
99             selectedSDK.sdk.tagName,
100             SDK_REPOSITORY_URL,
101             this._settings
102           ) &&
103           (await downloadAndInstallTools(
104             selectedSDK.sdk.tagName, process.platform === "win32")
105           )
106         ) {
107           progress.report({
108             increment: 40,
109           });
110
111           if (
112             await downloadAndInstallToolchain(
113               selectedToolchainVersion!.toolchain
114             )
115           ) {
116             progress.report({
117               increment: 40,
118             });
119
120             await updateVSCodeStaticConfigs(
121               workspaceFolder.uri.fsPath,
122               selectedSDK.sdk.tagName,
123               selectedToolchainVersion!.toolchain.version
124             );
125
126             progress.report({
127               increment: 10,
128             });
129
130             await cmakeUpdateSDK(
131               workspaceFolder.uri,
132               this._settings,
133               selectedSDK.sdk.tagName,
134               selectedToolchainVersion!.toolchain.version
135             );
136
137             progress.report({
138               // show sdk installed notification
139               message: `Successfully installed SDK ${selectedSDK.label}.`,
140               increment: 10,
141             });
142
143             return true;
144           } else {
145             progress.report({
146               message:
147                 "Failed to install " +
148                 `toolchain ${selectedToolchainVersion!.label}.`,
149               increment: 60,
150             });
151
152             return false;
153           }
154         }
155
156         progress.report({
157           // show sdk install failed notification
158           message:
159             `Failed to install SDK ${selectedSDK.label}.` +
160             "Make sure all requirements are met.",
161           increment: 100,
162         });
163
164         return false;
165       }
166     );
167
168     if (result) {
169       this._ui.updateSDKVersion(selectedSDK.label);
170     }
171   }
172 }
This page took 0.039437 seconds and 4 git commands to generate.