]> Git Repo - pico-vscode.git/blob - src/commands/launchTargetPath.mts
Fix linting errors
[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 && settings.getBoolean(SettingsKey.useCmakeTools)
67     ) {
68       // Compile with CMake Tools
69       const path: string = await commands.executeCommand(
70         "cmake.launchTargetPath"
71       );
72       if (path) {
73         return path;
74       }
75     }
76
77     const fsPathFolder = workspace.workspaceFolders[0].uri.fsPath;
78
79     const projectName = await this.readProjectNameFromCMakeLists(
80       join(fsPathFolder, "CMakeLists.txt")
81     );
82
83     if (projectName === null) {
84       return "";
85     }
86
87     // Compile before returning
88     await commands.executeCommand("raspberry-pi-pico.compileProject");
89
90     return join(fsPathFolder, "build", projectName + ".elf").replaceAll(
91       "\\",
92       "/"
93     );
94   }
95 }
This page took 0.02714 seconds and 4 git commands to generate.