]> Git Repo - pico-vscode.git/blob - src/commands/switchSDK.mts
Add elf2uf2 and pioasm for sdk 1.5.1
[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(selectedSDK.sdk.tagName))
104         ) {
105           progress.report({
106             increment: 40,
107           });
108
109           if (
110             await downloadAndInstallToolchain(
111               selectedToolchainVersion!.toolchain
112             )
113           ) {
114             progress.report({
115               increment: 40,
116             });
117
118             await updateVSCodeStaticConfigs(
119               workspaceFolder.uri.fsPath,
120               selectedSDK.sdk.tagName,
121               selectedToolchainVersion!.toolchain.version
122             );
123
124             progress.report({
125               increment: 10,
126             });
127
128             await cmakeUpdateSDK(
129               workspaceFolder.uri,
130               this._settings,
131               selectedSDK.sdk.tagName,
132               selectedToolchainVersion!.toolchain.version
133             );
134
135             progress.report({
136               // show sdk installed notification
137               message: `Successfully installed SDK ${selectedSDK.label}.`,
138               increment: 10,
139             });
140
141             return true;
142           } else {
143             progress.report({
144               message:
145                 "Failed to install " +
146                 `toolchain ${selectedToolchainVersion!.label}.`,
147               increment: 60,
148             });
149
150             return false;
151           }
152         }
153
154         progress.report({
155           // show sdk install failed notification
156           message:
157             `Failed to install SDK ${selectedSDK.label}.` +
158             "Make sure all requirements are met.",
159           increment: 100,
160         });
161
162         return false;
163       }
164     );
165
166     if (result) {
167       this._ui.updateSDKVersion(selectedSDK.label);
168     }
169   }
170 }
This page took 0.035352 seconds and 4 git commands to generate.