]> Git Repo - pico-vscode.git/blob - src/commands/launchTargetPath.mts
Some code refactoring
[pico-vscode.git] / src / commands / launchTargetPath.mts
1 import { readFileSync } from "fs";
2 import { CommandWithResult } from "./command.mjs";
3 import { window, workspace } from "vscode";
4 import { join } from "path";
5
6 export default class LaunchTargetPathCommand extends CommandWithResult<string> {
7   constructor() {
8     super("launchTargetPath");
9   }
10
11   private async readProjectNameFromCMakeLists(
12     filename: string
13   ): Promise<string | null> {
14     // Read the file
15     const fileContent = readFileSync(filename, "utf-8");
16
17     // Match the project line using a regular expression
18     const regex = /project\(([^)\s]+)/;
19     const match = regex.exec(fileContent);
20
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);
26
27     // Extract the project name from the matched result
28     if (match && match[1]) {
29       const projectName = match[1].trim();
30
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",
36         });
37         if (backgroundOrPoll === undefined) {
38           return projectName;
39         }
40
41         switch (backgroundOrPoll) {
42           case quickPickItems[0]:
43             return projectName + "_background";
44           case quickPickItems[1]:
45             return projectName + "_poll";
46         }
47       }
48
49       return projectName;
50     }
51
52     return null; // Return null if project line is not found
53   }
54
55   async execute(): Promise<string> {
56     if (
57       workspace.workspaceFolders === undefined ||
58       workspace.workspaceFolders.length === 0
59     ) {
60       return "";
61     }
62
63     const fsPathFolder = workspace.workspaceFolders[0].uri.fsPath;
64
65     const projectName = await this.readProjectNameFromCMakeLists(
66       join(fsPathFolder, "CMakeLists.txt")
67     );
68
69     if (projectName === null) {
70       return "";
71     }
72
73     return join(fsPathFolder, "build", projectName + ".elf").replaceAll(
74       "\\",
75       "/"
76     );
77   }
78 }
This page took 0.035882 seconds and 4 git commands to generate.