Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Simpler and more type-safe BazelTaskInfo #365

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 0 additions & 64 deletions src/bazel/bazel_task_info.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/bazel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export * from "./bazel_command";
export * from "./bazel_exit_code";
export * from "./bazel_query";
export * from "./bazel_quickpick";
export * from "./bazel_task_info";
export * from "./bazel_utils";
export * from "./bazel_workspace_info";
export * from "./query_location";
Expand Down
64 changes: 63 additions & 1 deletion src/bazel/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@
import * as vscode from "vscode";
import { getDefaultBazelExecutablePath } from "../extension/configuration";
import { IBazelCommandOptions } from "./bazel_command";
import { onTaskProcessEnd, onTaskStart } from "./bazel_task_info";
import { BazelWorkspaceInfo } from "./bazel_workspace_info";
import { exitCodeToUserString, parseExitCode } from "./bazel_exit_code";

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
*
Expand All @@ -33,6 +39,8 @@ export interface BazelTaskDefinition extends vscode.TaskDefinition {
targets: string[];
/** Additional command line arguments */
options?: string[];
/** Information about the running task */
bazelTaskInfo?: BazelTaskInfo;
}

/**
Expand Down Expand Up @@ -83,6 +91,60 @@ class BazelTaskProvider implements vscode.TaskProvider {
}
}

/**
* 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
*/
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 bazelTaskInfo = taskDefinition.bazelTaskInfo;

// Show a notification that the build is finished
if (bazelTaskInfo) {
const rawExitCode = event.exitCode;

const exitCode = parseExitCode(rawExitCode, command);
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.`,
);
}
}
}

/**
* Activate support for `bazel` tasks
*/
Expand Down