1 import { readFileSync } from "fs";
2 import { CommandWithResult } from "./command.mjs";
3 import { workspace } from "vscode";
4 import { join } from "path";
6 export default class LaunchTargetPathCommand extends CommandWithResult<string> {
8 super("launchTargetPath");
11 private readProjectNameFromCMakeLists(filename: string): string | null {
13 const fileContent = readFileSync(filename, "utf-8");
15 // Match the project line using a regular expression
16 const regex = /project\(([^)\s]+)/;
17 const match = regex.exec(fileContent);
19 // Extract the project name from the matched result
20 if (match && match[1]) {
21 const projectName = match[1].trim();
26 return null; // Return null if project line is not found
31 workspace.workspaceFolders === undefined ||
32 workspace.workspaceFolders.length === 0
37 const fsPathFolder = workspace.workspaceFolders[0].uri.fsPath;
39 const projectName = this.readProjectNameFromCMakeLists(
40 join(fsPathFolder, "CMakeLists.txt")
43 if (projectName === null) {
47 return join(fsPathFolder, "build", projectName + ".elf");