forked from swiftlang/vscode-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwiftBuildStatus.ts
149 lines (139 loc) · 5.12 KB
/
SwiftBuildStatus.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
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2024 the VS Code Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import stripAnsi = require("strip-ansi");
import * as vscode from "vscode";
import configuration, { ShowBuildStatusOptions } from "../configuration";
import { RunningTask, StatusItem } from "./StatusItem";
import { SwiftExecution } from "../tasks/SwiftExecution";
import { checkIfBuildComplete } from "../utilities/tasks";
/**
* Progress of `swift` build, parsed from the
* output, ex. `[6/7] Building main.swift`
*/
interface SwiftProgress {
completed: number;
total: number;
}
/**
* This class will handle detecting and updating the status
* bar message as the `swift` process executes.
*
* @see {@link SwiftExecution} to see what and where the events come from
*/
export class SwiftBuildStatus implements vscode.Disposable {
private onDidStartTaskDisposible: vscode.Disposable;
constructor(private statusItem: StatusItem) {
this.onDidStartTaskDisposible = vscode.tasks.onDidStartTask(event => {
if (!configuration.showBuildStatus) {
return;
}
this.handleTaskStatus(event.execution.task);
});
}
dispose() {
this.onDidStartTaskDisposible.dispose();
}
private handleTaskStatus(task: vscode.Task): void {
// Only care about swift tasks
if (task.definition.type !== "swift") {
return;
}
// Default to setting if task doesn't overwrite
const showBuildStatus: ShowBuildStatusOptions =
task.definition.showBuildStatus || configuration.showBuildStatus;
if (showBuildStatus === "never") {
return;
}
const execution = task.execution as SwiftExecution;
const disposables: vscode.Disposable[] = [];
const handleTaskOutput = (update: (message: string) => void) =>
new Promise<void>(res => {
const done = () => {
disposables.forEach(d => d.dispose());
res();
};
disposables.push(
execution.onDidWrite(data => {
if (this.parseEvents(task, data, update)) {
done();
}
}),
execution.onDidClose(done),
vscode.tasks.onDidEndTask(e => {
if (e.execution.task === task) {
done();
}
})
);
});
if (showBuildStatus === "progress" || showBuildStatus === "notification") {
vscode.window.withProgress<void>(
{
location:
showBuildStatus === "progress"
? vscode.ProgressLocation.Window
: vscode.ProgressLocation.Notification,
},
progress => handleTaskOutput(message => progress.report({ message }))
);
} else {
this.statusItem.showStatusWhileRunning(task, () =>
handleTaskOutput(message => this.statusItem.update(task, message))
);
}
}
/**
* @param data
* @returns true if done, false otherwise
*/
private parseEvents(
task: vscode.Task,
data: string,
update: (message: string) => void
): boolean {
const name = new RunningTask(task).name;
const sanitizedData = stripAnsi(data);
// We'll process data one line at a time, in reverse order
// since the latest interesting message is all we need to
// be concerned with
const lines = sanitizedData.split(/\r\n|\n|\r/gm).reverse();
for (const line of lines) {
if (checkIfBuildComplete(line)) {
return true;
}
const progress = this.findBuildProgress(line);
if (progress) {
update(`${name} [${progress.completed}/${progress.total}]`);
return false;
}
if (this.checkIfFetching(line)) {
// this.statusItem.update(task, `Fetching dependencies "${task.name}"`);
update(`${name} fetching dependencies`);
return false;
}
}
return false;
}
private checkIfFetching(line: string): boolean {
const fetchRegex = /^Fetching\s/gm;
return !!fetchRegex.exec(line);
}
private findBuildProgress(line: string): SwiftProgress | undefined {
const buildingRegex = /^\[(\d+)\/(\d+)\]/g;
const match = buildingRegex.exec(line);
if (match) {
return { completed: parseInt(match[1]), total: parseInt(match[2]) };
}
}
}