]> Git Repo - pico-vscode.git/blob - src/commands/compileProject.mts
d25c20495319bdc00613225a7f70ed5b217d55db
[pico-vscode.git] / src / commands / compileProject.mts
1 import { commands, tasks, window } from "vscode";
2 import { EventEmitter } from 'events';
3 import { CommandWithResult } from "./command.mjs";
4 import Logger from "../logger.mjs";
5 import Settings, { SettingsKey } from "../settings.mjs";
6
7 export default class CompileProjectCommand extends CommandWithResult<boolean> {
8   private _logger: Logger = new Logger("CompileProjectCommand");
9
10   public static readonly id = "compileProject";
11
12   constructor() {
13     super(CompileProjectCommand.id);
14   }
15
16   async execute(): Promise<boolean> {
17     // Get the task with the specified name
18     const task = (await tasks.fetchTasks()).find(task => () => {
19       console.log(`[TASK] ${task.name}`);
20
21       return task.name === "Compile Project";
22     });
23
24     const settings = Settings.getInstance();
25     if (
26       settings !== undefined && settings.getBoolean(SettingsKey.useCmakeTools)
27     ) {
28       // Compile with CMake Tools
29       await commands.executeCommand(
30         "cmake.launchTargetPath"
31       );
32
33       return true;
34     }
35
36     if (task) {
37       // Execute the task
38       const emitter = new EventEmitter();
39
40       // add callbacks for task completion
41       const end = tasks.onDidEndTaskProcess(e => {
42         emitter.emit(
43           "terminated",
44           e.exitCode === undefined ? -1 : e.exitCode
45         );
46       });
47       // eslint-disable-next-line @typescript-eslint/no-unused-vars
48       const end2 = tasks.onDidEndTask(e => {
49         emitter.emit("terminated", -1);
50       });
51
52       await tasks.executeTask(task);
53       // eslint-disable-next-line @typescript-eslint/no-unused-vars
54       const code = await new Promise<number>((resolve, reject) => {
55         emitter.on("terminated", code => {
56           if (typeof(code) === 'number') {
57             resolve(code);
58           } else {
59             resolve(-1);
60           }
61         });
62       });
63
64       // dispose of callbacks
65       end.dispose();
66       end2.dispose();
67       this._logger.debug(
68         "Task 'Compile Project' completed with code " + code.toString()
69       );
70
71       return code === 0;
72     } else {
73       // Task not found
74       this._logger.error("Task 'Compile Project' not found.");
75       void window.showErrorMessage("Task 'Compile Project' not found.");
76
77       return false;
78     }
79
80   }
81 }
This page took 0.019648 seconds and 2 git commands to generate.