-
Notifications
You must be signed in to change notification settings - Fork 76
/
tasks.ts
306 lines (284 loc) · 10 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright 2018 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as vscode from "vscode";
import { getDefaultBazelExecutablePath } from "../extension/configuration";
import { IBazelCommandOptions } from "./bazel_command";
import { BazelWorkspaceInfo } from "./bazel_workspace_info";
import { exitCodeToUserString, parseExitCode } from "./bazel_exit_code";
import { BazelInfo } from "./bazel_info";
import { showLcovCoverage } from "../test-explorer";
export const TASK_TYPE = "bazel";
/** Information about a running Bazel task. */
export class BazelTaskInfo {
/** start time (for internal performance tracking). */
public startTime: [number, number];
}
/**
* Definition of a Bazel task
*
* Must be kept in sync with the schema specified in the `taskDefinitions`
* contribution in the `package.json`.
*/
export interface BazelTaskDefinition extends vscode.TaskDefinition {
/** The Bazel command */
command: "build" | "clean" | "coverage" | "test" | "run";
/** The list of Bazel targets */
targets: string[];
/** Additional command line arguments */
options?: string[];
/** Information about the running task */
bazelTaskInfo?: BazelTaskInfo;
}
/**
* Returns a {@code ShellQuotedString} indicating how to quote the given flag
* if it contains spaces or other characters that need escaping.
*/
function quotedOption(option: string): vscode.ShellQuotedString {
return { value: option, quoting: vscode.ShellQuoting.Strong };
}
/**
* Task provider for `bazel` tasks.
*/
class BazelTaskProvider implements vscode.TaskProvider {
provideTasks(): vscode.ProviderResult<vscode.Task[]> {
// We don't auto-detect any tasks
return [];
}
async resolveTask(task: vscode.Task): Promise<vscode.Task | undefined> {
// VSCode calls this
// * when rerunning a task from the task history in "Run Task"
// * for bazel tasks in the user's tasks.json,
// We need to inform VSCode how to execute that command by creating
// a ShellExecution for it.
// Infer `BazelWorkspaceInfo` from `scope`
const workspaceInfo = await getWorkspaceInfoFromTask(task.scope);
if (!workspaceInfo) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
vscode.window.showInformationMessage(
"Please open a Bazel workspace folder to use this task.",
);
return;
}
return createBazelTaskFromDefinition(
task.definition as BazelTaskDefinition,
workspaceInfo,
);
}
}
async function getWorkspaceInfoFromTask(
scope: vscode.WorkspaceFolder | vscode.TaskScope,
) {
let workspaceInfo: BazelWorkspaceInfo;
if (
scope === vscode.TaskScope.Global ||
scope === vscode.TaskScope.Workspace
) {
workspaceInfo = await BazelWorkspaceInfo.fromWorkspaceFolders();
} else if (scope) {
workspaceInfo = BazelWorkspaceInfo.fromWorkspaceFolder(scope);
}
return workspaceInfo;
}
/**
* Keep track of running Bazel tasks
*/
function onTaskStart(event: vscode.TaskStartEvent) {
const task = event.execution.task;
if (task.definition.type !== TASK_TYPE) {
return;
}
const definition = task.definition as BazelTaskDefinition;
const bazelTaskInfo = new BazelTaskInfo();
bazelTaskInfo.startTime = process.hrtime();
definition.bazelTaskInfo = bazelTaskInfo;
}
/**
* Returns the number of seconds elapsed with a single decimal place.
*/
function measurePerformance(start: [number, number]) {
const diff = process.hrtime(start);
return (diff[0] + diff[1] / 1e9).toFixed(1);
}
/**
* Display a notification whenever a Bazel task finished
*/
async function onTaskProcessEnd(event: vscode.TaskProcessEndEvent) {
const task = event.execution.task;
if (task.definition.type !== TASK_TYPE) {
return;
}
const taskDefinition = task.definition as BazelTaskDefinition;
const command = taskDefinition.command;
const rawExitCode = event.exitCode;
const exitCode = parseExitCode(rawExitCode, command);
const bazelTaskInfo = taskDefinition.bazelTaskInfo;
// Show a notification that the build is finished
if (bazelTaskInfo) {
if (rawExitCode !== 0) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
vscode.window.showErrorMessage(
`Bazel ${command} failed: ${exitCodeToUserString(exitCode)}`,
);
} else {
const timeInSeconds = measurePerformance(bazelTaskInfo.startTime);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
vscode.window.showInformationMessage(
`Bazel ${command} completed successfully in ${timeInSeconds} seconds.`,
);
}
}
// For coverage runs: Display the coverage results
if (taskDefinition.command === "coverage" && rawExitCode === 0) {
// Find the coverage file and load it.
const workspaceInfo = await getWorkspaceInfoFromTask(task.scope);
const outputPath = await new BazelInfo(
getDefaultBazelExecutablePath(),
workspaceInfo.bazelWorkspacePath,
).getOne("output_path");
// Build a description string which will be displayed as part of the test run.
const execution = task.execution as vscode.ShellExecution;
const bazelCommandStr = JSON.stringify(
[execution.command]
.concat(execution.args)
.map((a) => (typeof a === "string" ? a : a.value)),
);
const description = `Coverage info from:\n ${bazelCommandStr}\n`;
const covFilePath = outputPath + "/_coverage/_coverage_report.dat";
const covFileUri = vscode.Uri.file(covFilePath);
try {
const covFileBytes = await vscode.workspace.fs.readFile(covFileUri);
const covFileStr = new TextDecoder("utf8").decode(covFileBytes);
if (covFileStr.trim() === "") {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
vscode.window.showWarningMessage(
"The generated LCOV coverage file was empty.\n" +
"Please ensure your toolchain is correctly setup and " +
"the instrumentation filters are set correctly.",
);
} else {
// Show the coverage date
showLcovCoverage(
description,
workspaceInfo.bazelWorkspacePath,
covFileStr,
);
}
} catch (e: any) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
vscode.window.showErrorMessage(
`Unable to open coverage report from ${covFilePath}:\n${e}`,
);
}
}
}
/**
* Activate support for `bazel` tasks
*/
export function activateTaskProvider(): vscode.Disposable[] {
return [
// Task provider
vscode.tasks.registerTaskProvider(TASK_TYPE, new BazelTaskProvider()),
// Task events
vscode.tasks.onDidStartTask(onTaskStart),
vscode.tasks.onDidEndTaskProcess(onTaskProcessEnd),
];
}
/**
* Creates a new task that invokes a build or test action.
*
* @param command The Bazel command to execute.
* @param options Describes the options used to launch Bazel.
*/
export function createBazelTaskFromDefinition(
taskDefinition: BazelTaskDefinition,
workspaceInfo: BazelWorkspaceInfo,
): vscode.Task {
const command = taskDefinition.command;
const bazelConfigCmdLine =
vscode.workspace.getConfiguration("bazel.commandLine");
const startupOptions = bazelConfigCmdLine.get<string[]>("startupOptions");
const addCommandArgs =
command === "build" || command === "test" || command === "coverage";
const commandArgs = addCommandArgs
? bazelConfigCmdLine.get<string[]>("commandArgs")
: [];
const implicitArgs = [] as string[];
let commandDescription: string;
let group: vscode.TaskGroup | undefined;
switch (command) {
case "build":
commandDescription = "Build";
group = vscode.TaskGroup.Build;
break;
case "clean":
commandDescription = "Clean";
group = vscode.TaskGroup.Clean;
break;
case "coverage":
commandDescription = "Coverage";
group = vscode.TaskGroup.Test;
// Coverage for cached tests doesn't work as expected :/
// Disable caching.
implicitArgs.push("--nocache_test_results");
// We only support lcov formats, so request this format
implicitArgs.push("--combined_report=lcov");
break;
case "test":
commandDescription = "Test";
group = vscode.TaskGroup.Test;
break;
case "run":
commandDescription = "Run";
break;
}
const args = startupOptions
.concat([command as string])
.concat(commandArgs)
.concat(implicitArgs)
.concat(taskDefinition.options ?? [])
.concat(taskDefinition.targets)
.map(quotedOption);
const targetsDescription = taskDefinition.targets.join(", ");
const task = new vscode.Task(
taskDefinition,
// TODO(allevato): Change Workspace to Global once the fix for
// Microsoft/vscode#63951 is in a stable release.
workspaceInfo.workspaceFolder || vscode.TaskScope.Workspace,
`${commandDescription} ${targetsDescription}`,
"bazel",
new vscode.ShellExecution(getDefaultBazelExecutablePath(), args, {
cwd: workspaceInfo.bazelWorkspacePath,
}),
);
task.group = group;
return task;
}
/**
* Creates a new task that invokes a build or test action.
*
* @param command The Bazel command to execute.
* @param options Describes the options used to launch Bazel.
*/
export function createBazelTask(
command: "build" | "clean" | "coverage" | "test" | "run",
options: IBazelCommandOptions,
): vscode.Task {
const taskDefinition: BazelTaskDefinition = {
type: TASK_TYPE,
command,
targets: options.targets,
options: options.options,
};
return createBazelTaskFromDefinition(taskDefinition, options.workspaceInfo);
}