-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathextension.mts
More file actions
1260 lines (1130 loc) · 36.6 KB
/
extension.mts
File metadata and controls
1260 lines (1130 loc) · 36.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
workspace,
type ExtensionContext,
window,
type WebviewPanel,
commands,
ProgressLocation,
Uri,
FileSystemError,
type WorkspaceFolder,
} from "vscode";
import {
extensionName,
type Command,
type CommandWithArgs,
type CommandWithResult,
} from "./commands/command.mjs";
import NewProjectCommand from "./commands/newProject.mjs";
import Logger, { LoggerSource } from "./logger.mjs";
import {
CMAKE_DO_NOT_EDIT_HEADER_PREFIX,
CMAKE_DO_NOT_EDIT_HEADER_PREFIX_OLD,
cmakeGetPicoVar,
cmakeGetSelectedBoard,
cmakeGetSelectedToolchainAndSDKVersions,
configureCmakeNinja,
} from "./utils/cmakeUtil.mjs";
import Settings, {
SettingsKey,
type PackageJSON,
HOME_VAR,
} from "./settings.mjs";
import UI from "./ui.mjs";
import SwitchSDKCommand from "./commands/switchSDK.mjs";
import { existsSync, readFileSync } from "fs";
import { basename, join } from "path";
import CompileProjectCommand from "./commands/compileProject.mjs";
import RunProjectCommand from "./commands/runProject.mjs";
import LaunchTargetPathCommand, {
LaunchTargetPathReleaseCommand,
SbomTargetPathDebugCommand,
SbomTargetPathReleaseCommand,
} from "./commands/launchTargetPath.mjs";
import {
GetPythonPathCommand,
GetEnvPathCommand,
GetGDBPathCommand,
GetCompilerPathCommand,
GetCxxCompilerPathCommand,
GetChipCommand,
GetTargetCommand,
GetChipUppercaseCommand,
GetPicotoolPathCommand,
GetOpenOCDRootCommand,
GetSVDPathCommand,
GetWestPathCommand,
GetZephyrWorkspacePathCommand,
GetZephyrSDKPathCommand,
GetGitPathCommand,
} from "./commands/getPaths.mjs";
import {
downloadAndInstallCmake,
downloadAndInstallNinja,
downloadAndInstallSDK,
downloadAndInstallToolchain,
downloadAndInstallTools,
downloadAndInstallPicotool,
downloadAndInstallOpenOCD,
} from "./utils/download.mjs";
import { getSupportedToolchains } from "./utils/toolchainUtil.mjs";
import { NewProjectPanel } from "./webview/newProjectPanel.mjs";
import GithubApiCache from "./utils/githubApiCache.mjs";
import ClearGithubApiCacheCommand from "./commands/clearGithubApiCache.mjs";
import { ContextKeys } from "./contextKeys.mjs";
import { PicoProjectActivityBar } from "./webview/activityBar.mjs";
import ConditionalDebuggingCommand from "./commands/conditionalDebugging.mjs";
import DebugLayoutCommand from "./commands/debugLayout.mjs";
import OpenSdkDocumentationCommand from "./commands/openSdkDocumentation.mjs";
import ConfigureCmakeCommand, {
CleanCMakeCommand,
SwitchBuildTypeCommand,
} from "./commands/configureCmake.mjs";
import ImportProjectCommand from "./commands/importProject.mjs";
import { homedir } from "os";
import NewExampleProjectCommand from "./commands/newExampleProject.mjs";
import SwitchBoardCommand from "./commands/switchBoard.mjs";
import UninstallPicoSDKCommand from "./commands/uninstallPicoSDK.mjs";
import UpdateOpenOCDCommand from "./commands/updateOpenOCD.mjs";
import FlashProjectSWDCommand from "./commands/flashProjectSwd.mjs";
// eslint-disable-next-line max-len
import { NewMicroPythonProjectPanel } from "./webview/newMicroPythonProjectPanel.mjs";
import type { Progress as GotProgress } from "got";
import findPython, { showPythonNotFoundError } from "./utils/pythonHelper.mjs";
import {
downloadAndInstallRust,
installLatestRustRequirements,
rustProjectGetSelectedChip,
} from "./utils/rustUtil.mjs";
import State from "./state.mjs";
import { cmakeToolsForcePicoKit } from "./utils/cmakeToolsUtil.mjs";
import { NewRustProjectPanel } from "./webview/newRustProjectPanel.mjs";
import {
CMAKELISTS_ZEPHYR_HEADER,
OPENOCD_VERSION,
SDK_REPOSITORY_URL,
} from "./utils/sharedConstants.mjs";
import VersionBundlesLoader from "./utils/versionBundles.mjs";
import { unknownErrorToString } from "./utils/errorHelper.mjs";
import {
getBoardFromZephyrProject,
getZephyrVersion,
setupZephyr,
updateZephyrCompilerPath,
updateZephyrVersion,
zephyrVerifyCMakeCache,
} from "./utils/setupZephyr.mjs";
import { IMPORT_PROJECT } from "./commands/cmdIds.mjs";
import {
ZEPHYR_PICO,
ZEPHYR_PICO2,
ZEPHYR_PICO2_W,
ZEPHYR_PICO_W,
} from "./models/zephyrBoards.mjs";
import { NewZephyrProjectPanel } from "./webview/newZephyrProjectPanel.mjs";
import LastUsedDepsStore from "./utils/lastUsedDeps.mjs";
import { getWebviewOptions } from "./webview/sharedFunctions.mjs";
import { UninstallerPanel } from "./webview/uninstallerPanel.mjs";
import OpenUninstallerCommand from "./commands/openUninstaller.mjs";
import { CleanZephyrCommand } from "./commands/cleanZephyr.mjs";
export async function activate(context: ExtensionContext): Promise<void> {
Logger.info(LoggerSource.extension, "Extension activation triggered");
const settings = Settings.createInstance(
context.workspaceState,
context.globalState,
context.extension.packageJSON as PackageJSON
);
GithubApiCache.createInstance(context);
LastUsedDepsStore.instance.setup(context.globalState);
const picoProjectActivityBarProvider = new PicoProjectActivityBar();
const ui = new UI(picoProjectActivityBarProvider);
ui.init();
const COMMANDS: Array<
| Command
| CommandWithResult<string>
| CommandWithResult<string | undefined>
| CommandWithResult<boolean>
| CommandWithArgs
> = [
new NewProjectCommand(context.extensionUri),
new SwitchSDKCommand(ui, context.extensionUri),
new SwitchBoardCommand(ui, context.extensionUri),
new LaunchTargetPathCommand(),
new LaunchTargetPathReleaseCommand(),
new GetPythonPathCommand(),
new GetEnvPathCommand(),
new GetGDBPathCommand(context.extensionUri),
new GetCompilerPathCommand(),
new GetCxxCompilerPathCommand(),
new GetChipCommand(),
new GetChipUppercaseCommand(),
new GetTargetCommand(),
new GetPicotoolPathCommand(),
new GetOpenOCDRootCommand(),
new GetSVDPathCommand(context.extensionUri),
new GetWestPathCommand(),
new GetZephyrWorkspacePathCommand(),
new GetZephyrSDKPathCommand(),
new CompileProjectCommand(),
new RunProjectCommand(),
new FlashProjectSWDCommand(),
new ClearGithubApiCacheCommand(),
new ConditionalDebuggingCommand(),
new DebugLayoutCommand(),
new OpenSdkDocumentationCommand(context.extensionUri),
new ConfigureCmakeCommand(),
new SwitchBuildTypeCommand(ui),
new ImportProjectCommand(context.extensionUri),
new NewExampleProjectCommand(context.extensionUri),
new UninstallPicoSDKCommand(),
new CleanCMakeCommand(ui),
new UpdateOpenOCDCommand(),
new SbomTargetPathDebugCommand(),
new SbomTargetPathReleaseCommand(),
new OpenUninstallerCommand(context.extensionUri),
new GetGitPathCommand(settings),
new CleanZephyrCommand(),
];
// register all command handlers
COMMANDS.forEach(command => {
context.subscriptions.push(command.register());
});
context.subscriptions.push(
window.registerWebviewPanelSerializer(NewProjectPanel.viewType, {
// eslint-disable-next-line @typescript-eslint/require-await
async deserializeWebviewPanel(
webviewPanel: WebviewPanel,
state: { isImportProject: boolean; forceFromExample: boolean }
): Promise<void> {
// Reset the webview options so we use latest uri for `localResourceRoots`.
webviewPanel.webview.options = getWebviewOptions(context.extensionUri);
NewProjectPanel.revive(
webviewPanel,
context.extensionUri,
state && state.isImportProject,
state && state.forceFromExample
);
},
})
);
context.subscriptions.push(
window.registerWebviewPanelSerializer(NewMicroPythonProjectPanel.viewType, {
// eslint-disable-next-line @typescript-eslint/require-await
async deserializeWebviewPanel(webviewPanel: WebviewPanel): Promise<void> {
// Reset the webview options so we use latest uri for `localResourceRoots`.
webviewPanel.webview.options = getWebviewOptions(context.extensionUri);
NewMicroPythonProjectPanel.revive(webviewPanel, context.extensionUri);
},
})
);
context.subscriptions.push(
window.registerWebviewPanelSerializer(NewRustProjectPanel.viewType, {
// eslint-disable-next-line @typescript-eslint/require-await
async deserializeWebviewPanel(webviewPanel: WebviewPanel): Promise<void> {
// Reset the webview options so we use latest uri for `localResourceRoots`.
webviewPanel.webview.options = getWebviewOptions(context.extensionUri);
NewRustProjectPanel.revive(webviewPanel, context.extensionUri);
},
})
);
// TODO: currently broken
context.subscriptions.push(
window.registerWebviewPanelSerializer(NewZephyrProjectPanel.viewType, {
// eslint-disable-next-line @typescript-eslint/require-await
async deserializeWebviewPanel(webviewPanel: WebviewPanel): Promise<void> {
// Reset the webview options so we use latest uri for `localResourceRoots`.
webviewPanel.webview.options = getWebviewOptions(context.extensionUri);
NewZephyrProjectPanel.revive(webviewPanel, context.extensionUri);
},
})
);
context.subscriptions.push(
window.registerWebviewPanelSerializer(UninstallerPanel.viewType, {
// eslint-disable-next-line @typescript-eslint/require-await
async deserializeWebviewPanel(webviewPanel: WebviewPanel): Promise<void> {
// Reset the webview options so we use latest uri for `localResourceRoots`.
webviewPanel.webview.options = getWebviewOptions(context.extensionUri);
UninstallerPanel.revive(webviewPanel, context.extensionUri);
},
})
);
context.subscriptions.push(
window.registerTreeDataProvider(
PicoProjectActivityBar.viewType,
picoProjectActivityBarProvider
)
);
const workspaceFolder = workspace.workspaceFolders?.[0];
const isRustProject = workspaceFolder
? existsSync(join(workspaceFolder.uri.fsPath, ".pico-rs"))
: false;
// check if there is a workspace folder
if (workspaceFolder === undefined) {
// finish activation
Logger.warn(LoggerSource.extension, "No workspace folder found.");
await commands.executeCommand(
"setContext",
ContextKeys.isPicoProject,
false
);
return;
}
await commands.executeCommand(
"setContext",
ContextKeys.isRustProject,
isRustProject
);
State.getInstance().isRustProject = isRustProject;
if (!isRustProject) {
const cmakeListsFilePath = join(
workspaceFolder.uri.fsPath,
"CMakeLists.txt"
);
if (!existsSync(cmakeListsFilePath)) {
Logger.warn(
LoggerSource.extension,
"No CMakeLists.txt in workspace folder has been found."
);
await commands.executeCommand(
"setContext",
ContextKeys.isPicoProject,
false
);
return;
}
// Set Pico Zephyr Project false by default
await commands.executeCommand(
"setContext",
ContextKeys.isZephyrProject,
false
);
const cmakeListsContents = new TextDecoder().decode(
await workspace.fs.readFile(Uri.file(cmakeListsFilePath))
);
// Check for pico_zephyr in CMakeLists.txt
if (cmakeListsContents.startsWith(CMAKELISTS_ZEPHYR_HEADER)) {
Logger.info(LoggerSource.extension, "Project is of type: Zephyr");
const vb = new VersionBundlesLoader(context.extensionUri);
const latest = await vb.getLatest();
if (latest === undefined) {
Logger.error(
LoggerSource.extension,
"Failed to get latest version bundle for Zephyr project."
);
void window.showErrorMessage(
"Failed to get latest version bundle for Zephyr project."
);
return;
}
const cmakePath = settings.getString(SettingsKey.cmakePath);
// TODO: or auto upgrade to latest cmake
if (cmakePath === undefined) {
Logger.error(
LoggerSource.extension,
"CMake path not set in settings. Cannot setup Zephyr project."
);
void window.showErrorMessage(
"CMake path not set in settings. Cannot setup Zephyr project."
);
await commands.executeCommand(
"setContext",
ContextKeys.isPicoProject,
false
);
return;
}
let cmakeVersion = "";
if (cmakePath && cmakePath.includes("/.pico-sdk/cmake")) {
const version = /\/\.pico-sdk\/cmake\/([v.0-9A-Za-z-]+)\//.exec(
cmakePath
)?.[1];
if (version === undefined) {
Logger.error(
LoggerSource.extension,
"Failed to get CMake version from path in the settings."
);
await commands.executeCommand(
"setContext",
ContextKeys.isPicoProject,
false
);
return;
}
cmakeVersion = version;
}
const ninjaPath = settings.getString(SettingsKey.ninjaPath);
let ninjaVersion = "";
if (ninjaPath === undefined) {
Logger.error(
LoggerSource.extension,
"Ninja path not set in settings. Cannot setup Zephyr project."
);
void window.showErrorMessage(
"Ninja path not set in settings. Cannot setup Zephyr project."
);
await commands.executeCommand(
"setContext",
ContextKeys.isPicoProject,
false
);
return;
} else if (ninjaPath && ninjaPath.includes("/.pico-sdk/ninja")) {
const version = /\/\.pico-sdk\/ninja\/([v.0-9]+)\//.exec(
ninjaPath
)?.[1];
if (version === undefined) {
Logger.error(
LoggerSource.extension,
"Failed to get Ninja version from path in the settings."
);
await commands.executeCommand(
"setContext",
ContextKeys.isPicoProject,
false
);
return;
}
ninjaVersion = version;
}
// check for pinned zephyr version in workspace settings
const pinnedVersion = settings.getString(SettingsKey.zephyrVersion);
if (pinnedVersion !== undefined && pinnedVersion.length > 0) {
const systemVersion = await getZephyrVersion();
if (systemVersion === undefined) {
Logger.error(
LoggerSource.extension,
"Failed to get system Zephyr version."
);
void window.showErrorMessage(
"Failed to get system Zephyr version. Cannot setup Zephyr project."
);
// TODO: instead reset zephyr workspace
await commands.executeCommand(
"setContext",
ContextKeys.isPicoProject,
false
);
return;
}
if (systemVersion !== pinnedVersion) {
// ask user to switch zephyr version
const switchVersion = await window.showInformationMessage(
`Project Zephyr version (${pinnedVersion}) differs from system ` +
`version (${systemVersion}). ` +
`Do you want to switch the system version?`,
{ modal: true },
"Yes",
"No - Use system version",
"No - Pin to system version"
);
if (switchVersion === "Yes") {
const switchResult = await updateZephyrVersion(pinnedVersion);
if (!switchResult) {
void window.showErrorMessage(
`Failed to switch Zephyr version to ${pinnedVersion}. ` +
"Cannot setup Zephyr project."
);
await commands.executeCommand(
"setContext",
ContextKeys.isPicoProject,
false
);
return;
} else {
void window.showInformationMessage(
`Switched Zephyr version to ${pinnedVersion}.`
);
Logger.info(
LoggerSource.extension,
`Switched Zephyr version to ${pinnedVersion}.`
);
}
} else if (switchVersion === "No - Pin to system version") {
// update workspace settings
await settings.update(SettingsKey.zephyrVersion, systemVersion);
void window.showInformationMessage(
`Pinned Zephyr version to ${systemVersion}.`
);
Logger.info(
LoggerSource.extension,
`Pinned Zephyr version to ${systemVersion}.`
);
}
}
}
const result = await setupZephyr({
extUri: context.extensionUri,
cmakeMode: cmakeVersion !== "" ? 2 : 3,
cmakePath:
cmakeVersion !== ""
? ""
: cmakePath.replace(HOME_VAR, homedir().replaceAll("\\", "/")) ??
"",
cmakeVersion: cmakeVersion,
ninjaMode: ninjaVersion !== "" ? 2 : 3,
ninjaPath:
ninjaVersion !== ""
? ""
: ninjaPath.replace(HOME_VAR, homedir().replaceAll("\\", "/")) ??
"",
ninjaVersion: ninjaVersion,
});
if (result === undefined) {
void window.showErrorMessage(
"Failed to setup Zephyr Toolchain. See logs for details."
);
return;
}
void window.showInformationMessage(
"Zephyr Toolchain setup done. You can now build your project."
);
await commands.executeCommand(
"setContext",
ContextKeys.isPicoProject,
true
);
await commands.executeCommand(
"setContext",
ContextKeys.isZephyrProject,
true
);
State.getInstance().isZephyrProject = true;
ui.showStatusBarItems(false, true);
const selectedZephyrVersion = await getZephyrVersion();
if (selectedZephyrVersion === undefined) {
Logger.error(
LoggerSource.extension,
"Failed to get selected system Zephyr version. Defaulting to main."
);
}
ui.updateSDKVersion(selectedZephyrVersion ?? "main");
const cppCompilerUpdated = await updateZephyrCompilerPath(
workspaceFolder.uri,
selectedZephyrVersion ?? "main"
);
if (!cppCompilerUpdated) {
void window.showErrorMessage(
"Failed to update C++ compiler path in c_cpp_properties.json"
);
// TODO: maybe cancel activation
}
// Update the board info if it can be found in tasks.json
const tasksJsonFilePath = join(
workspaceFolder.uri.fsPath,
".vscode",
"tasks.json"
);
// Update UI with board description
const board = await getBoardFromZephyrProject(tasksJsonFilePath);
if (board !== undefined) {
if (board === ZEPHYR_PICO2_W) {
ui.updateBoard("Pico 2W");
} else if (board === ZEPHYR_PICO2) {
ui.updateBoard("Pico 2");
} else if (board === ZEPHYR_PICO_W) {
ui.updateBoard("Pico W");
} else if (board === ZEPHYR_PICO) {
ui.updateBoard("Pico");
} else {
ui.updateBoard("Other");
}
}
await zephyrVerifyCMakeCache(workspaceFolder.uri);
if (settings.getBoolean(SettingsKey.cmakeAutoConfigure)) {
await cmakeSetupAutoConfigure(workspaceFolder, ui);
} else {
// check if build dir is empty and recommend to run a build to
// get the intellisense working
const buildUri = Uri.file(join(workspaceFolder.uri.fsPath, "build"));
try {
// workaround to stop verbose logging of readDirectory
// internals even if we catch the error
await workspace.fs.stat(buildUri);
const buildDirContents = await workspace.fs.readDirectory(buildUri);
if (buildDirContents.length === 0) {
void window.showWarningMessage(
"To get full intellisense support please build the project once."
);
}
} catch (error) {
if (
error instanceof FileSystemError &&
error.code === "FileNotFound"
) {
void window.showWarningMessage(
"To get full intellisense support please build the project once."
);
Logger.debug(
LoggerSource.extension,
'No "build" folder found. Intellisense might not work ' +
"properly until a build has been done."
);
} else {
Logger.error(
LoggerSource.extension,
"Error when reading build folder:",
unknownErrorToString(error)
);
}
}
}
return;
}
// check for pico_sdk_init() in CMakeLists.txt
else if (!cmakeListsContents.includes("pico_sdk_init()")) {
Logger.warn(
LoggerSource.extension,
"No pico_sdk_init() in CMakeLists.txt found."
);
await commands.executeCommand(
"setContext",
ContextKeys.isPicoProject,
false
);
return;
}
// check if it has .vscode folder and cmake donotedit header in CMakelists.txt
if (
!existsSync(join(workspaceFolder.uri.fsPath, ".vscode")) ||
!(
readFileSync(cmakeListsFilePath)
.toString("utf-8")
.includes(CMAKE_DO_NOT_EDIT_HEADER_PREFIX) ||
readFileSync(cmakeListsFilePath)
.toString("utf-8")
.includes(CMAKE_DO_NOT_EDIT_HEADER_PREFIX_OLD)
)
) {
Logger.warn(
LoggerSource.extension,
"No .vscode folder and/or cmake",
'"DO NOT EDIT"-header in CMakelists.txt found.'
);
await commands.executeCommand(
"setContext",
ContextKeys.isPicoProject,
false
);
const wantToImport = await window.showInformationMessage(
"Do you want to import this project as Raspberry Pi Pico project?",
"Yes",
"No"
);
if (wantToImport === "Yes") {
void commands.executeCommand(
`${extensionName}.${IMPORT_PROJECT}`,
workspaceFolder.uri
);
}
return;
}
}
await commands.executeCommand("setContext", ContextKeys.isPicoProject, true);
if (isRustProject) {
const vs = new VersionBundlesLoader(context.extensionUri);
const latestSDK = await vs.getLatestSDK();
if (!latestSDK) {
Logger.error(
LoggerSource.extension,
"Failed to get latest Pico SDK version for Rust project."
);
void window.showErrorMessage(
"Failed to get latest Pico SDK version for Rust project."
);
return;
}
const sdk = await window.withProgress(
{
location: ProgressLocation.Notification,
title:
"Downloading and installing latest Pico SDK (" +
latestSDK +
"). This may take a while...",
cancellable: false,
},
async progress => {
const result = await downloadAndInstallSDK(
context.extensionUri,
latestSDK,
SDK_REPOSITORY_URL
);
progress.report({
increment: 100,
});
if (!result) {
installSuccess = false;
Logger.error(
LoggerSource.extension,
"Failed to install latest SDK",
`version: ${latestSDK}.`,
"Make sure all requirements are met."
);
void window.showErrorMessage(
"Failed to install latest SDK version for rust project."
);
return false;
} else {
Logger.info(
LoggerSource.extension,
"Found/installed latest SDK",
`version: ${latestSDK}`
);
return true;
}
}
);
if (!sdk) {
return;
}
const cargo = await window.withProgress(
{
location: ProgressLocation.Notification,
title: "Downloading and installing Rust. This may take a while...",
cancellable: false,
},
async () => downloadAndInstallRust()
);
if (!cargo) {
void window.showErrorMessage("Failed to install Rust.");
return;
}
const result = await installLatestRustRequirements(context.extensionUri);
if (!result) {
return;
}
ui.showStatusBarItems(isRustProject);
const chip = rustProjectGetSelectedChip(workspaceFolder.uri.fsPath);
if (chip !== null) {
ui.updateBoard(chip.toUpperCase());
} else {
ui.updateBoard("N/A");
}
return;
}
// get sdk selected in the project
const selectedToolchainAndSDKVersions =
await cmakeGetSelectedToolchainAndSDKVersions(workspaceFolder.uri);
if (selectedToolchainAndSDKVersions === null) {
return;
}
// get all available toolchains for download link of current selected one
const toolchains = await getSupportedToolchains(context.extensionUri);
const selectedToolchain = toolchains.find(
toolchain => toolchain.version === selectedToolchainAndSDKVersions[1]
);
// TODO: for failed installation message add option to change version
let installSuccess = true;
// install if needed
await window.withProgress(
{
location: ProgressLocation.Notification,
title:
"Downloading and installing Pico SDK as selected. " +
"This may take a while...",
cancellable: false,
},
async progress => {
const result = await downloadAndInstallSDK(
context.extensionUri,
selectedToolchainAndSDKVersions[0],
SDK_REPOSITORY_URL
);
progress.report({
increment: 100,
});
if (!result) {
installSuccess = false;
Logger.error(
LoggerSource.extension,
"Failed to install project SDK",
`version: ${selectedToolchainAndSDKVersions[0]}.`,
"Make sure all requirements are met."
);
void window.showErrorMessage("Failed to install project SDK version.");
return;
} else {
Logger.info(
LoggerSource.extension,
"Found/installed project SDK",
`version: ${selectedToolchainAndSDKVersions[0]}`
);
}
}
);
if (!installSuccess) {
return;
}
if (selectedToolchain === undefined) {
Logger.error(
LoggerSource.extension,
"Failed to detect project toolchain version."
);
void window.showErrorMessage("Failed to detect project toolchain version.");
return;
}
let progressState = 0;
await window.withProgress(
{
location: ProgressLocation.Notification,
title:
"Downloading and installing toolchain as selected. " +
"This may take a while...",
cancellable: false,
},
async progress => {
const result = await downloadAndInstallToolchain(
selectedToolchain,
(prog: GotProgress) => {
const percent = prog.percent * 100;
progress.report({
increment: percent - progressState,
});
progressState = percent;
}
);
progress.report({
increment: 100,
});
if (!result) {
installSuccess = false;
Logger.error(
LoggerSource.extension,
"Failed to install project toolchain",
`version: ${selectedToolchainAndSDKVersions[1]}`
);
void window.showErrorMessage(
"Failed to install project toolchain version."
);
return;
} else {
Logger.info(
LoggerSource.extension,
"Found/installed project toolchain",
`version: ${selectedToolchainAndSDKVersions[1]}`
);
}
}
);
if (!installSuccess) {
return;
}
progressState = 0;
await window.withProgress(
{
location: ProgressLocation.Notification,
title:
"Downloading and installing tools as selected. " +
"This may take a while...",
cancellable: false,
},
async progress => {
const result = await downloadAndInstallTools(
selectedToolchainAndSDKVersions[0],
(prog: GotProgress) => {
const percent = prog.percent * 100;
progress.report({
increment: percent - progressState,
});
progressState = percent;
}
);
progress.report({
increment: 100,
});
if (!result) {
installSuccess = false;
Logger.error(
LoggerSource.extension,
"Failed to install project SDK",
`version: ${selectedToolchainAndSDKVersions[0]}.`,
"Make sure all requirements are met."
);
void window.showErrorMessage("Failed to install project SDK version.");
return;
} else {
Logger.info(
LoggerSource.extension,
"Found/installed project SDK",
`version: ${selectedToolchainAndSDKVersions[0]}`
);
}
}
);
if (!installSuccess) {
return;
}
progressState = 0;
await window.withProgress(
{
location: ProgressLocation.Notification,
title:
"Downloading and installing picotool as selected. " +
"This may take a while...",
cancellable: false,
},
async progress => {
const result = await downloadAndInstallPicotool(
selectedToolchainAndSDKVersions[2],
(prog: GotProgress) => {
const percent = prog.percent * 100;
progress.report({
increment: percent - progressState,
});
progressState = percent;
}
);
progress.report({
increment: 100,
});
if (!result) {
installSuccess = false;
Logger.error(LoggerSource.extension, "Failed to install picotool.");
void window.showErrorMessage("Failed to install picotool.");
return;
} else {
Logger.debug(LoggerSource.extension, "Found/installed picotool.");
}
}
);
if (!installSuccess) {
return;
}
progressState = 0;
await window.withProgress(
{
location: ProgressLocation.Notification,