1 import { readFileSync } from "fs";
2 import { CommandWithResult } from "./command.mjs";
3 import { window, workspace } from "vscode";
4 import { join } from "path";
6 export default class LaunchTargetPathCommand extends CommandWithResult<string> {
8 super("launchTargetPath");
11 private async readProjectNameFromCMakeLists(
13 ): Promise<string | null> {
15 const fileContent = readFileSync(filename, "utf-8");
17 // Match the project line using a regular expression
18 const regex = /project\(([^)\s]+)/;
19 const match = regex.exec(fileContent);
21 // Match for poll and threadsafe background inclusions
22 const regexBg = /pico_cyw43_arch_lwip_threadsafe_background/;
23 const matchBg = regexBg.exec(fileContent);
24 const regexPoll = /pico_cyw43_arch_lwip_poll/;
25 const matchPoll = regexPoll.exec(fileContent);
27 // Extract the project name from the matched result
28 if (match && match[1]) {
29 const projectName = match[1].trim();
31 if (matchBg && matchPoll) {
32 // For examples with both background and poll, let user pick which to run
33 const quickPickItems = ["Threadsafe Background", "Poll"];
34 const backgroundOrPoll = await window.showQuickPick(quickPickItems, {
35 placeHolder: "Select PicoW Architecture",
37 if (backgroundOrPoll === undefined) {
41 switch (backgroundOrPoll) {
42 case quickPickItems[0]:
43 return projectName + "_background";
44 case quickPickItems[1]:
45 return projectName + "_poll";
52 return null; // Return null if project line is not found
55 async execute(): Promise<string> {
57 workspace.workspaceFolders === undefined ||
58 workspace.workspaceFolders.length === 0
63 const fsPathFolder = workspace.workspaceFolders[0].uri.fsPath;
65 const projectName = await this.readProjectNameFromCMakeLists(
66 join(fsPathFolder, "CMakeLists.txt")
69 if (projectName === null) {
73 return join(fsPathFolder, "build", projectName + ".elf").replaceAll(