]> Git Repo - pico-vscode.git/blob - src/commands/launchTargetPath.mts
Update dependencies + auto-format
[pico-vscode.git] / src / commands / launchTargetPath.mts
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";
6
7 export default class LaunchTargetPathCommand extends CommandWithResult<string> {
8   constructor() {
9     super("launchTargetPath");
10   }
11
12   private async readProjectNameFromCMakeLists(
13     filename: string
14   ): Promise<string | null> {
15     // Read the file
16     const fileContent = readFileSync(filename, "utf-8");
17
18     // Match the project line using a regular expression
19     const regex = /project\(([^)\s]+)/;
20     const match = regex.exec(fileContent);
21
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);
27
28     // Extract the project name from the matched result
29     if (match && match[1]) {
30       const projectName = match[1].trim();
31
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",
37         });
38         if (backgroundOrPoll === undefined) {
39           return projectName;
40         }
41
42         switch (backgroundOrPoll) {
43           case quickPickItems[0]:
44             return projectName + "_background";
45           case quickPickItems[1]:
46             return projectName + "_poll";
47         }
48       }
49
50       return projectName;
51     }
52
53     return null; // Return null if project line is not found
54   }
55
56   async execute(): Promise<string> {
57     if (
58       workspace.workspaceFolders === undefined ||
59       workspace.workspaceFolders.length === 0
60     ) {
61       return "";
62     }
63
64     const settings = Settings.getInstance();
65     if (
66       settings !== undefined &&
67       settings.getBoolean(SettingsKey.useCmakeTools)
68     ) {
69       // Compile with CMake Tools
70       const path: string = await commands.executeCommand(
71         "cmake.launchTargetPath"
72       );
73       if (path) {
74         return path;
75       }
76     }
77
78     const fsPathFolder = workspace.workspaceFolders[0].uri.fsPath;
79
80     const projectName = await this.readProjectNameFromCMakeLists(
81       join(fsPathFolder, "CMakeLists.txt")
82     );
83
84     if (projectName === null) {
85       return "";
86     }
87
88     // Compile before returning
89     const compiled = await commands.executeCommand(
90       "raspberry-pi-pico.compileProject"
91     );
92
93     if (!compiled) {
94       throw new Error(
95         "Failed to compile project - check output from the Compile Project task"
96       );
97     }
98
99     return join(fsPathFolder, "build", projectName + ".elf").replaceAll(
100       "\\",
101       "/"
102     );
103   }
104 }
This page took 0.035536 seconds and 4 git commands to generate.