]> Git Repo - pico-vscode.git/blob - src/commands/switchSDK.mts
Add SDK background auto install for Windows
[pico-vscode.git] / src / commands / switchSDK.mts
1 import { compare } from "semver";
2 import { detectInstalledSDKs } from "../utils/picoSDKUtil.mjs";
3 import { Command } from "./command.mjs";
4 import { window, workspace } from "vscode";
5 import type Settings from "../settings.mjs";
6 import { SettingsKey } from "../settings.mjs";
7 import type UI from "../ui.mjs";
8 import { updateVSCodeStaticConfigs } from "../utils/vscodeConfigUtil.mjs";
9 import { join } from "path";
10 import { setPicoSDKPath, setToolchainPath } from "../utils/picoSDKEnvUtil.mjs";
11
12 export default class SwitchSDKCommand extends Command {
13   private _settings: Settings;
14   private _ui: UI;
15
16   constructor(settings: Settings, ui: UI) {
17     super("switchSDK");
18
19     this._settings = settings;
20     this._ui = ui;
21   }
22
23   async execute(): Promise<void> {
24     const availableSDKs = detectInstalledSDKs()
25       .sort((a, b) =>
26         compare(a.version.replace("v", ""), b.version.replace("v", ""))
27       )
28       .map(sdk => ({
29         label: `Pico SDK v${sdk.version}`,
30         version: sdk.version,
31         // TODO: maybe remove description
32         description: `${sdk.sdkPath}; ${sdk.toolchainPath}`,
33         sdkPath: sdk.sdkPath,
34         toolchainPath: sdk.toolchainPath,
35       }));
36
37     const selectedSDK = await window.showQuickPick(availableSDKs, {
38       placeHolder: "Select Pico SDK",
39       canPickMany: false,
40       ignoreFocusOut: false,
41       title: "Switch Pico SDK",
42     });
43
44     if (!selectedSDK) {
45       return;
46     }
47
48     if (workspace.workspaceFolders && workspace.workspaceFolders.length > 0) {
49       await updateVSCodeStaticConfigs(
50         join(workspace.workspaceFolders?.[0].uri.fsPath, ".vscode"),
51         selectedSDK.toolchainPath
52       );
53     }
54
55     setPicoSDKPath(selectedSDK.sdkPath, this._settings.getExtensionId());
56     setToolchainPath(selectedSDK.toolchainPath);
57     void window.showWarningMessage(
58       "Reload window to apply changes to linting."
59     );
60     // save selected SDK version to settings
61     await this._settings.update(SettingsKey.picoSDK, selectedSDK.label);
62     this._ui.updateSDKVersion(selectedSDK.version);
63   }
64 }
This page took 0.028857 seconds and 4 git commands to generate.