-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtasks.ts
218 lines (200 loc) · 7.17 KB
/
tasks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import * as vscode from "vscode";
import { getMesonTargets, getMesonTests, getMesonBenchmarks } from "./introspection";
import { extensionConfiguration, getOutputChannel, getTargetName } from "./utils";
import { Test, Target } from "./types";
import { checkMesonIsConfigured } from "./utils";
import { mesonProgram } from "./utils";
interface MesonTaskDefinition extends vscode.TaskDefinition {
type: "meson";
target?: string;
mode?: "build" | "run" | "test" | "benchmark" | "clean" | "reconfigure" | "install";
filename?: string;
}
function createTestTask(meson: string, t: Test, buildDir: string, isBenchmark: boolean) {
const project = t.suite[0].split(":")[0];
const name = `${project}:${t.name}`;
const mode = isBenchmark ? "benchmark" : "test";
const benchmarkSwitch = isBenchmark ? ["--benchmark"] : [];
const args = ["test", ...benchmarkSwitch, ...extensionConfiguration(`${mode}Options`), name];
const testTask = new vscode.Task(
{ type: "meson", mode, target: name },
`Test ${name}`,
"Meson",
new vscode.ProcessExecution(meson, args, {
cwd: buildDir,
}),
);
testTask.group = vscode.TaskGroup.Test;
testTask.detail = `Timeout: ${t.timeout}s, ${!isBenchmark && t.is_parallel ? "run in parallel" : "run serially"}`;
return testTask;
}
function createRunTask(meson: string, t: Target, targetName: string, buildDir: string) {
const targetDisplayName = targetName.split(":")[0];
let runTask = new vscode.Task(
{
type: "meson",
target: targetName,
filename: t.filename[0],
mode: "run",
},
`Run ${targetDisplayName}`,
"Meson",
new vscode.ProcessExecution(meson, ["devenv", t.filename[0]], {
cwd: buildDir,
}),
);
runTask.group = vscode.TaskGroup.Test;
return runTask;
}
function createReconfigureTask(meson: string, buildDir: string, sourceDir: string) {
const configureOpts = extensionConfiguration("configureOptions");
const setupOpts = extensionConfiguration("setupOptions");
const reconfigureOpts = checkMesonIsConfigured(buildDir) ? ["--reconfigure"] : [];
const args = ["setup", ...reconfigureOpts, ...configureOpts, ...setupOpts, buildDir, sourceDir];
const env = extensionConfiguration("configureEnvironment");
const options = env ? { env } : {};
return new vscode.Task(
{ type: "meson", mode: "reconfigure" },
"Reconfigure",
"Meson",
new vscode.ProcessExecution(meson, args, options),
);
}
export async function getMesonTasks(buildDir: string, sourceDir: string) {
try {
const meson = mesonProgram();
const defaultBuildTask = new vscode.Task(
{ type: "meson", mode: "build" },
"Build all targets",
"Meson",
new vscode.ProcessExecution(meson, ["compile", "-C", buildDir]),
"$meson-gcc",
);
const defaultTestTask = new vscode.Task(
{ type: "meson", mode: "test" },
"Run all tests",
"Meson",
new vscode.ProcessExecution(meson, ["test", ...extensionConfiguration("testOptions")], {
cwd: buildDir,
env: extensionConfiguration("testEnvironment"),
}),
);
const defaultBenchmarkTask = new vscode.Task(
{ type: "meson", mode: "benchmark" },
"Run all benchmarks",
"Meson",
new vscode.ProcessExecution(meson, ["test", "--benchmark", ...extensionConfiguration("benchmarkOptions")], {
cwd: buildDir,
}),
);
const defaultReconfigureTask = createReconfigureTask(meson, buildDir, sourceDir);
const defaultInstallTask = new vscode.Task(
{ type: "meson", mode: "install" },
"Run install",
"Meson",
new vscode.ProcessExecution(meson, ["install"], { cwd: buildDir }),
);
const defaultCleanTask = new vscode.Task(
{ type: "meson", mode: "clean" },
"Clean",
"Meson",
new vscode.ProcessExecution(meson, ["compile", "--clean"], { cwd: buildDir }),
);
defaultBuildTask.group = vscode.TaskGroup.Build;
defaultTestTask.group = vscode.TaskGroup.Test;
defaultBenchmarkTask.group = vscode.TaskGroup.Test;
defaultReconfigureTask.group = vscode.TaskGroup.Rebuild;
defaultCleanTask.group = vscode.TaskGroup.Clean;
const tasks = [
defaultBuildTask,
defaultTestTask,
defaultBenchmarkTask,
defaultReconfigureTask,
defaultCleanTask,
defaultInstallTask,
];
// Remaining tasks needs a valid configuration
if (!checkMesonIsConfigured(buildDir)) {
return tasks;
}
const [targets, tests, benchmarks] = await Promise.all([
getMesonTargets(buildDir),
getMesonTests(buildDir),
getMesonBenchmarks(buildDir),
]);
tasks.push(
...(
await Promise.all(
targets.map(async (t) => {
const targetName = await getTargetName(t);
const def: MesonTaskDefinition = {
type: "meson",
target: targetName,
mode: "build",
};
const buildTask = new vscode.Task(
def,
`Build ${targetName}`,
"Meson",
new vscode.ProcessExecution(meson, ["compile", targetName], {
cwd: buildDir,
}),
"$meson-gcc",
);
buildTask.group = vscode.TaskGroup.Build;
if (t.type == "executable") {
// Create run tasks for executables that are not tests,
// both installed and uninstalled (eg: examples)
if (!tests.some((test) => test.name === t.name)) {
return [buildTask, createRunTask(meson, t, targetName, buildDir)];
}
}
return buildTask;
}),
)
).flat(1),
...tests.map((t) => createTestTask(meson, t, buildDir, false)),
...benchmarks.map((b) => createTestTask(meson, b, buildDir, true)),
);
return tasks;
} catch (e: any) {
if ("error" in e) {
getOutputChannel().appendLine(e.error.message);
}
if ("stderr" in e) {
getOutputChannel().appendLine(e.stderr);
}
vscode.window.showErrorMessage("Could not fetch targets. See Meson Build output tab for more info.");
return [];
}
}
export async function getTask(mode: string, name?: string) {
const tasks = await vscode.tasks.fetchTasks({ type: "meson" });
const filtered = tasks.filter((t) => t.definition["mode"] == mode && (!name || t.definition["target"] == name));
if (filtered.length === 0) {
throw new Error(`Cannot find ${mode} target ${name}.`);
}
return filtered[0];
}
export async function getTasks(mode: string) {
const tasks = await vscode.tasks.fetchTasks({ type: "meson" });
return tasks.filter((t) => t.definition["mode"] === mode);
}
export async function runTask(task: vscode.Task) {
try {
await vscode.tasks.executeTask(task);
} catch (e: any) {
vscode.window.showErrorMessage(`Could not ${task.definition["mode"]} ${task.name}`);
getOutputChannel().appendLine(`Running task ${task.name}:`);
if ("error" in e) {
getOutputChannel().appendLine(e.error.message);
}
if ("stderr" in e) {
getOutputChannel().appendLine(e.stderr);
}
getOutputChannel().show(true);
}
}
export async function runFirstTask(mode: string, name?: string) {
runTask(await getTask(mode, name));
}