-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathApi.ts
120 lines (104 loc) · 3.48 KB
/
Api.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
import * as vscode from "vscode";
import { Output } from "../proto/gradle_pb";
import { Logger, logger } from "../logger";
import { GradleTasksTreeDataProvider } from "../views";
import { GradleClient } from "../client";
import { Icons } from "../icons";
import { getRunBuildCancellationKey } from "../client/CancellationKeys";
import { GradleTaskProvider } from "../tasks";
export interface RunTaskOpts {
projectFolder: string;
taskName: string;
args?: ReadonlyArray<string>;
input?: string;
onOutput?: (output: Output) => void;
showOutputColors: boolean;
cancellationKey?: string;
}
export interface RunBuildOpts {
projectFolder: string;
args: ReadonlyArray<string>;
input?: string;
onOutput?: (output: Output) => void;
showOutputColors: boolean;
cancellationKey?: string;
}
export interface CancelTaskOpts {
projectFolder?: string;
taskName?: string;
cancellationKey?: string;
}
export interface CancelBuildOpts {
projectFolder?: string;
args?: ReadonlyArray<string>;
cancellationKey?: string;
}
export class Api {
constructor(
private readonly client: GradleClient,
private readonly tasksTreeDataProvider: GradleTasksTreeDataProvider,
private readonly gradleTaskProvider: GradleTaskProvider,
private readonly icons: Icons
) {}
public onReady(callback: () => void): vscode.Disposable {
return this.client.onDidConnect(callback);
}
public runTask(opts: RunTaskOpts): Promise<void> {
const taskArgs = (opts.args || []).filter(Boolean);
const runBuildArgs = [opts.taskName].concat(taskArgs);
const runBuildOpts = {
...opts,
args: runBuildArgs,
};
return this.runBuild(runBuildOpts);
}
public runBuild(opts: RunBuildOpts): Promise<void> {
return this.client.runBuild(
opts.projectFolder,
opts.cancellationKey || getRunBuildCancellationKey(opts.projectFolder, opts.args),
opts.args,
opts.input,
0,
undefined,
opts.onOutput,
opts.showOutputColors
);
}
public cancelRunTask(opts: CancelTaskOpts): Promise<void> {
const args = opts.taskName ? [opts.taskName] : [];
const cancelBuildOpts = {
projectFolder: opts.projectFolder,
args,
cancellationKey: opts.cancellationKey,
};
return this.cancelRunBuild(cancelBuildOpts);
}
public cancelRunBuild(opts: CancelBuildOpts): Promise<void> {
const cancellationKey = this.getRunBuildCancellationKey(opts);
return this.client.cancelBuild(cancellationKey);
}
public cancelAllBuilds(): Promise<void> {
return this.client.cancelBuilds();
}
private getRunBuildCancellationKey(opts: CancelBuildOpts): string {
if (opts.cancellationKey) {
return opts.cancellationKey;
}
if (!opts.args || !opts.projectFolder) {
throw new Error("args and projectFolder are required to build the cancellation key");
}
return getRunBuildCancellationKey(opts.projectFolder, opts.args);
}
public getTasksTreeProvider(): GradleTasksTreeDataProvider {
return this.tasksTreeDataProvider;
}
public getTaskProvider(): GradleTaskProvider {
return this.gradleTaskProvider;
}
public getIcons(): Icons {
return this.icons;
}
public getLogger(): Logger {
return logger;
}
}