diff --git a/src/bazel/bazel_task_info.ts b/src/bazel/bazel_task_info.ts deleted file mode 100644 index 501379f7..00000000 --- a/src/bazel/bazel_task_info.ts +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright 2019 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 { TASK_TYPE, type BazelTaskDefinition } from "./tasks"; -import { exitCodeToUserString, parseExitCode } from "./bazel_exit_code"; - -/** Information about a Bazel task. */ -export class BazelTaskInfo { - /** start time (for internal performance tracking). */ - public startTime: [number, number]; -} - -export function onTaskStart(event: vscode.TaskStartEvent) { - const definition = event.execution.task.definition; - if (definition.type === TASK_TYPE) { - const bazelTaskInfo = new BazelTaskInfo(); - bazelTaskInfo.startTime = process.hrtime(); - definition.bazelTaskInfo = bazelTaskInfo; - } -} - -export function onTaskProcessEnd(event: vscode.TaskProcessEndEvent) { - const task = event.execution.task; - const command = (task.definition as BazelTaskDefinition).command; - const bazelTaskInfo = task.definition.bazelTaskInfo as BazelTaskInfo; - 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.`, - ); - } - } -} - -/** - * 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); -} diff --git a/src/bazel/index.ts b/src/bazel/index.ts index bdb88cfc..9661db0d 100644 --- a/src/bazel/index.ts +++ b/src/bazel/index.ts @@ -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"; diff --git a/src/bazel/tasks.ts b/src/bazel/tasks.ts index ead1e7ca..760753ea 100644 --- a/src/bazel/tasks.ts +++ b/src/bazel/tasks.ts @@ -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 * @@ -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; } /** @@ -83,6 +91,58 @@ class BazelTaskProvider implements vscode.TaskProvider { } } +/** + * Keep track of running Bazel tasks + */ +function onTaskStart(event: vscode.TaskStartEvent) { + const definition = event.execution.task.definition; + if (definition.type === TASK_TYPE) { + 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 */