Skip to content

Commit

Permalink
refactor: Simpler and more type-safe BazelTaskInfo (#365)
Browse files Browse the repository at this point in the history
* Add the `bazelTaskInfo` member to the `BazelTaskDefinition` type so it
  can be accessed in a more typesafe manner.
* Merges `bazel_task_info.ts` into `tasks.ts`. Otherwise, adding the
  `bazelTaskInfo` member would have introduced a cyclical dependency.
  Also, the `bazel_task_info.ts` was rather small anyway, so keeping
  this in a separate file didn't provide much value anyway.

Refactoring in preparation for #362
  • Loading branch information
vogelsgesang committed Apr 4, 2024
1 parent ebe0a89 commit 09daa3d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 66 deletions.
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

0 comments on commit 09daa3d

Please sign in to comment.