1 import { readFileSync } from "fs";
2 import { CommandWithResult } from "./command.mjs";
3 import { commands, window, workspace } from "vscode";
4 import { join } from "path";
5 import Settings, { SettingsKey } from "../settings.mjs";
7 export default class LaunchTargetPathCommand extends CommandWithResult<string> {
9 super("launchTargetPath");
12 private async readProjectNameFromCMakeLists(
14 ): Promise<string | null> {
16 const fileContent = readFileSync(filename, "utf-8");
18 // Match the project line using a regular expression
19 const regex = /project\(([^)\s]+)/;
20 const match = regex.exec(fileContent);
22 // Match for poll and threadsafe background inclusions
23 const regexBg = /pico_cyw43_arch_lwip_threadsafe_background/;
24 const matchBg = regexBg.exec(fileContent);
25 const regexPoll = /pico_cyw43_arch_lwip_poll/;
26 const matchPoll = regexPoll.exec(fileContent);
28 // Extract the project name from the matched result
29 if (match && match[1]) {
30 const projectName = match[1].trim();
32 if (matchBg && matchPoll) {
33 // For examples with both background and poll, let user pick which to run
34 const quickPickItems = ["Threadsafe Background", "Poll"];
35 const backgroundOrPoll = await window.showQuickPick(quickPickItems, {
36 placeHolder: "Select PicoW Architecture",
38 if (backgroundOrPoll === undefined) {
42 switch (backgroundOrPoll) {
43 case quickPickItems[0]:
44 return projectName + "_background";
45 case quickPickItems[1]:
46 return projectName + "_poll";
53 return null; // Return null if project line is not found
56 async execute(): Promise<string> {
58 workspace.workspaceFolders === undefined ||
59 workspace.workspaceFolders.length === 0
64 const settings = Settings.getInstance();
66 settings !== undefined &&
67 settings.getBoolean(SettingsKey.useCmakeTools)
69 // Compile with CMake Tools
70 const path: string = await commands.executeCommand(
71 "cmake.launchTargetPath"
78 const fsPathFolder = workspace.workspaceFolders[0].uri.fsPath;
80 const projectName = await this.readProjectNameFromCMakeLists(
81 join(fsPathFolder, "CMakeLists.txt")
84 if (projectName === null) {
88 // Compile before returning
89 const compiled = await commands.executeCommand(
90 "raspberry-pi-pico.compileProject"
95 "Failed to compile project - check output from the Compile Project task"
99 return join(fsPathFolder, "build", projectName + ".elf").replaceAll(