forked from microsoft/vscode-gradle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGradleServer.ts
192 lines (174 loc) · 6.86 KB
/
GradleServer.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import * as vscode from "vscode";
import * as path from "path";
import * as cp from "child_process";
import * as getPort from "get-port";
import * as kill from "tree-kill";
import { commands } from "vscode";
import { sendInfo } from "vscode-extension-telemetry-wrapper";
import { getGradleServerCommand, getGradleServerEnv, quoteArg } from "./serverUtil";
import { Logger } from "../logger/index";
import { NO_JAVA_EXECUTABLE, OPT_RESTART } from "../constant";
import { redHatJavaInstalled } from "../util/config";
import { BspProxy } from "../bs/BspProxy";
import { getRandomPipeName } from "../util/generateRandomPipeName";
const SERVER_LOGLEVEL_REGEX = /^\[([A-Z]+)\](.*)$/;
const DOWNLOAD_PROGRESS_CHAR = ".";
export interface ServerOptions {
host: string;
}
export class GradleServer {
private readonly _onDidStart: vscode.EventEmitter<null> = new vscode.EventEmitter<null>();
private readonly _onDidStop: vscode.EventEmitter<null> = new vscode.EventEmitter<null>();
private ready = false;
private taskServerPort: number | undefined;
private restarting = false;
public readonly onDidStart: vscode.Event<null> = this._onDidStart.event;
public readonly onDidStop: vscode.Event<null> = this._onDidStop.event;
private process?: cp.ChildProcessWithoutNullStreams;
private languageServerPipePath: string;
constructor(
private readonly opts: ServerOptions,
private readonly context: vscode.ExtensionContext,
private readonly logger: Logger,
private bspProxy: BspProxy
) {
this.setLanguageServerPipePath();
}
private setLanguageServerPipePath(): void {
this.languageServerPipePath = getRandomPipeName();
if (this.languageServerPipePath === "") {
this.logger.error("Failed to generate language server pipe path, language server will not start");
}
}
public getLanguageServerPipePath(): string {
return this.languageServerPipePath;
}
public async start(): Promise<void> {
const isPrepared = this.bspProxy.prepareToStart();
if (!isPrepared) {
this.logger.error("Failed to generate build server pipe path, build server will not start");
}
let startBuildServer = isPrepared && redHatJavaInstalled();
if (process.env.DEBUG_VSCODE_JAVA) {
const debugBuildServer = process.env.DEBUG_START_BUILD_SERVER === "true";
startBuildServer = startBuildServer && debugBuildServer;
}
this.bspProxy.setBuildServerStarted(startBuildServer);
this.taskServerPort = await getPort();
const cwd = this.context.asAbsolutePath("lib");
const cmd = path.join(cwd, getGradleServerCommand());
const env = await getGradleServerEnv();
const bundleDirectory = this.context.asAbsolutePath("server");
if (!env) {
sendInfo("", {
kind: "GradleServerEnvMissing",
});
await vscode.window.showErrorMessage(NO_JAVA_EXECUTABLE);
return;
}
const args = [
quoteArg(`--port=${this.taskServerPort}`),
quoteArg(`--startBuildServer=${startBuildServer}`),
quoteArg(`--languageServerPipePath=${this.languageServerPipePath}`),
];
if (startBuildServer) {
const buildServerPipeName = this.bspProxy.getBuildServerPipeName();
args.push(quoteArg(`--pipeName=${buildServerPipeName}`));
args.push(quoteArg(`--bundleDir=${bundleDirectory}`));
}
this.logger.debug(`Gradle Server cmd: ${cmd} ${args.join(" ")}`);
this.process = cp.spawn(`"${cmd}"`, args, {
cwd,
env,
shell: true,
});
this.process.stdout.on("data", this.logOutput);
this.process.stderr.on("data", this.logOutput);
this.process
.on("error", (err: Error) => this.logger.error(err.message))
.on("exit", async (code) => {
this.logger.warn("Gradle server stopped");
this._onDidStop.fire(null);
this.ready = false;
this.process?.removeAllListeners();
this.bspProxy.closeConnection();
if (this.restarting) {
this.restarting = false;
await this.start();
} else if (code !== 0) {
await this.handleServerStartError(code);
}
});
this.fireOnStart();
}
public isReady(): boolean {
return this.ready;
}
public async showRestartMessage(): Promise<void> {
const selection = await vscode.window.showErrorMessage(
"No connection to gradle server. Try restarting the server.",
OPT_RESTART
);
sendInfo("", {
kind: "serverProcessExitRestart",
data2: selection === OPT_RESTART ? "true" : "false",
});
if (selection === OPT_RESTART) {
await commands.executeCommand("workbench.action.restartExtensionHost");
}
}
public async restart(): Promise<void> {
this.logger.info("Restarting gradle server");
this.restarting = true;
this.killProcess();
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private logOutput = (data: any): void => {
const str = data.toString().trim();
if (!str || str === DOWNLOAD_PROGRESS_CHAR) {
return;
}
const logLevelMatches = str.match(SERVER_LOGLEVEL_REGEX);
if (logLevelMatches && logLevelMatches.length) {
const [, serverLogLevel, serverLogMessage] = logLevelMatches;
const logLevel = serverLogLevel.toLowerCase() as "debug" | "info" | "warn" | "error";
this.logger[logLevel](serverLogMessage.trim());
} else {
this.logger.info(str);
}
};
private async killProcess(): Promise<void> {
if (this.process) {
return new Promise((resolve, _reject) => {
if (this.process?.pid) {
kill(this.process.pid, () => resolve);
}
});
}
}
private async handleServerStartError(code: number | null): Promise<void> {
sendInfo("", {
kind: "serverProcessExit",
data2: code ? code.toString() : "",
});
await this.showRestartMessage();
}
private fireOnStart(): void {
this.ready = true;
this._onDidStart.fire(null);
}
public async asyncDispose(): Promise<void> {
this.bspProxy.closeConnection();
this.process?.removeAllListeners();
await this.killProcess();
this.ready = false;
this._onDidStart.dispose();
this._onDidStop.dispose();
}
public getPort(): number | undefined {
return this.taskServerPort;
}
public getOpts(): ServerOptions {
return this.opts;
}
}