forked from flutter/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevToolsService.java
388 lines (343 loc) · 14.1 KB
/
DevToolsService.java
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright 2020 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package io.flutter.run.daemon;
import com.google.common.collect.ImmutableList;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.process.ProcessAdapter;
import com.intellij.execution.process.ProcessEvent;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.process.ProcessOutput;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.project.ProjectManagerListener;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Version;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.registry.Registry;
import com.jetbrains.lang.dart.ide.devtools.DartDevToolsService;
import com.jetbrains.lang.dart.ide.toolingDaemon.DartToolingDaemonService;
import com.jetbrains.lang.dart.sdk.DartSdk;
import io.flutter.FlutterUtils;
import io.flutter.bazel.Workspace;
import io.flutter.bazel.WorkspaceCache;
import io.flutter.console.FlutterConsoles;
import io.flutter.dart.DtdUtils;
import io.flutter.sdk.FlutterCommand;
import io.flutter.sdk.FlutterSdk;
import io.flutter.sdk.FlutterSdkUtil;
import io.flutter.utils.JsonUtils;
import io.flutter.utils.MostlySilentColoredProcessHandler;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
public class DevToolsService {
private static final Logger LOG = Logger.getInstance(DevToolsService.class);
public static final String LOCAL_DEVTOOLS_DIR = "flutter.local.devtools.dir";
public static final String LOCAL_DEVTOOLS_ARGS = "flutter.local.devtools.args";
private static class DevToolsServiceListener implements DaemonEvent.Listener {
}
@NotNull private final Project project;
private DaemonApi daemonApi;
private ProcessHandler process;
private AtomicReference<CompletableFuture<DevToolsInstance>> devToolsFutureRef = new AtomicReference<>(null);
@NotNull
public static DevToolsService getInstance(@NotNull final Project project) {
return Objects.requireNonNull(project.getService(DevToolsService.class));
}
private DevToolsService(@NotNull final Project project) {
this.project = project;
}
public CompletableFuture<DevToolsInstance> getDevToolsInstance() {
// Create instance if it doesn't exist yet, or if the previous attempt failed.
if (devToolsFutureRef.compareAndSet(null, new CompletableFuture<>())) {
startServer();
}
if (devToolsFutureRef.updateAndGet((future) -> {
if (future.isCompletedExceptionally()) {
return null;
}
else {
return future;
}
}) == null) {
devToolsFutureRef.set(new CompletableFuture<>());
startServer();
}
return devToolsFutureRef.get();
}
public CompletableFuture<DevToolsInstance> getDevToolsInstanceWithForcedRestart() {
final CompletableFuture<DevToolsInstance> futureInstance = devToolsFutureRef.updateAndGet((future) -> {
if (future.isCompletedExceptionally() || future.isCancelled()) {
return null;
}
else {
return future;
}
});
if (futureInstance == null) {
devToolsFutureRef.set(new CompletableFuture<>());
startServer();
}
else if (!futureInstance.isDone()) {
futureInstance.cancel(true);
devToolsFutureRef.set(new CompletableFuture<>());
startServer();
}
return devToolsFutureRef.get();
}
private void startServer() {
ApplicationManager.getApplication().executeOnPooledThread(() -> {
final FlutterSdk sdk = FlutterSdk.getFlutterSdk(project);
boolean dartDevToolsSupported = false;
final DartSdk dartSdk = DartSdk.getDartSdk(project);
if (dartSdk != null) {
final Version version = Version.parseVersion(dartSdk.getVersion());
assert version != null;
dartDevToolsSupported = version.compareTo(2, 15, 0) >= 0;
}
if (dartDevToolsSupported) {
// This condition means we can use `dart devtools` to start.
final WorkspaceCache workspaceCache = WorkspaceCache.getInstance(project);
if (workspaceCache.isBazel()) {
// This is only for internal usages.
setUpWithDart(createCommand(workspaceCache.get().getRoot().getPath(), workspaceCache.get().getDevToolsScript(),
ImmutableList.of("--machine")));
}
else {
final String localDevToolsDir = Registry.stringValue(LOCAL_DEVTOOLS_DIR);
if (!localDevToolsDir.isEmpty()) {
// This is only for development to check integration with a locally run DevTools server.
// The plugin will run `devtools_tool serve` and assumes that setup from
// https://github.com/flutter/devtools/blob/master/CONTRIBUTING.md has been done.
// To use this option, go to Help > Find action > Registry > Set "flutter.local.devtools.dir" to your local DevTools directory,
// e.g. "/Users/username/Documents/devtools".
// To go back to using DevTools from the SDK (the standard way), clear out the registry setting.
final DtdUtils dtdUtils = new DtdUtils();
try {
final DartToolingDaemonService dtdService = dtdUtils.readyDtdService(project).get();
final String dtdUri = dtdService.getUri();
final List<String> args = new ArrayList<>();
args.add("serve");
args.add("--machine");
args.add("--dtd-uri=" + dtdUri);
final String localDevToolsArgs = Registry.stringValue(LOCAL_DEVTOOLS_ARGS);
if (!localDevToolsArgs.isEmpty()) {
args.addAll(Arrays.stream(localDevToolsArgs.split(" ")).toList());
}
setUpInDevMode(createCommand(localDevToolsDir, "devtools_tool",
args));
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
catch (java.util.concurrent.ExecutionException e) {
throw new RuntimeException(e);
}
return;
}
// The Dart plugin should start DevTools with DTD, so try to use this instance of DevTools before trying to start another.
final String dartPluginUri = DartDevToolsService.getInstance(project).getDevToolsHostAndPort();
if (dartPluginUri != null) {
String[] parts = dartPluginUri.split(":");
String host = parts[0];
Integer port = Integer.parseInt(parts[1]);
if (host != null && port != null) {
devToolsFutureRef.get().complete(new DevToolsInstance(host, port));
return;
}
}
setUpWithDart(createCommand(DartSdk.getDartSdk(project).getHomePath(),
DartSdk.getDartSdk(project).getHomePath() + File.separatorChar + "bin" + File.separatorChar + "dart",
ImmutableList.of("devtools", "--machine")));
}
}
else {
setUpWithDaemon();
}
});
}
private void setUpInDevMode(GeneralCommandLine command) {
try {
this.process = new MostlySilentColoredProcessHandler(command);
this.process.addProcessListener(new ProcessAdapter() {
@Override
public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType) {
final String text = event.getText().trim();
// Keep this printout so developers can see DevTools startup output in idea.log.
System.out.println("DevTools startup: " + text);
tryParseStartupText(text);
}
});
process.startNotify();
}
catch (ExecutionException e) {
logExceptionAndComplete(e);
}
}
private void setUpWithDart(GeneralCommandLine command) {
try {
this.process = new MostlySilentColoredProcessHandler(command);
this.process.addProcessListener(new ProcessAdapter() {
@Override
public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType) {
tryParseStartupText(event.getText().trim());
}
});
process.startNotify();
ProjectManager.getInstance().addProjectManagerListener(project, new ProjectManagerListener() {
@Override
public void projectClosing(@NotNull Project project) {
devToolsFutureRef.set(null);
process.destroyProcess();
}
});
}
catch (ExecutionException e) {
logExceptionAndComplete(e);
}
}
private void tryParseStartupText(@NotNull String text) {
if (text.startsWith("{") && text.endsWith("}")) {
try {
final JsonElement element = JsonUtils.parseString(text);
final JsonObject obj = element.getAsJsonObject();
if (Objects.equals(JsonUtils.getStringMember(obj, "event"), "server.started")) {
final JsonObject params = obj.getAsJsonObject("params");
final String host = JsonUtils.getStringMember(params, "host");
final int port = JsonUtils.getIntMember(params, "port");
if (port != -1) {
devToolsFutureRef.get().complete(new DevToolsInstance(host, port));
}
else {
logExceptionAndComplete("DevTools port was invalid");
}
}
}
catch (JsonSyntaxException e) {
logExceptionAndComplete(e);
}
}
}
private void setUpWithDaemon() {
try {
final GeneralCommandLine command = chooseCommand(project);
if (command == null) {
logExceptionAndComplete("Unable to find daemon command for project");
return;
}
this.process = new MostlySilentColoredProcessHandler(command);
daemonApi = new DaemonApi(process);
daemonApi.listen(process, new DevToolsServiceListener());
daemonApi.devToolsServe().thenAccept((DaemonApi.DevToolsAddress address) -> {
if (!project.isOpen()) {
// We should skip starting DevTools (and doing any UI work) if the project has been closed.
return;
}
if (address == null) {
logExceptionAndComplete("DevTools address was null");
}
else {
devToolsFutureRef.get().complete(new DevToolsInstance(address.host, address.port));
}
});
}
catch (ExecutionException e) {
logExceptionAndComplete(e);
}
ProjectManager.getInstance().addProjectManagerListener(project, new ProjectManagerListener() {
@Override
public void projectClosing(@NotNull Project project) {
devToolsFutureRef.set(null);
try {
daemonApi.daemonShutdown().get(5, TimeUnit.SECONDS);
}
catch (InterruptedException | java.util.concurrent.ExecutionException | TimeoutException e) {
LOG.error("DevTools daemon did not shut down normally: " + e);
if (!process.isProcessTerminated()) {
process.destroyProcess();
}
}
}
});
}
private CompletableFuture<Boolean> pubActivateDevTools(FlutterSdk sdk) {
final FlutterCommand command = sdk.flutterPub(null, "global", "activate", "devtools");
final CompletableFuture<Boolean> result = new CompletableFuture<>();
final Process process = command.start((ProcessOutput output) -> {
if (output.getExitCode() != 0) {
final String message = (output.getStdout() + "\n" + output.getStderr()).trim();
FlutterConsoles.displayMessage(project, null, message, true);
}
}, null);
try {
final int resultCode = process.waitFor();
result.complete(resultCode == 0);
}
catch (RuntimeException | InterruptedException re) {
if (!result.isDone()) {
result.complete(false);
}
}
return result;
}
private void logExceptionAndComplete(String message) {
logExceptionAndComplete(new Exception(message));
}
private void logExceptionAndComplete(Exception exception) {
LOG.info(exception);
final CompletableFuture<DevToolsInstance> future = devToolsFutureRef.get();
if (future != null) {
future.completeExceptionally(exception);
}
}
private static GeneralCommandLine chooseCommand(@NotNull final Project project) {
// Use daemon script if this is a bazel project.
final Workspace workspace = WorkspaceCache.getInstance(project).get();
if (workspace != null) {
final String script = workspace.getDaemonScript();
if (script != null) {
return createCommand(workspace.getRoot().getPath(), script, ImmutableList.of());
}
}
// Otherwise, use the Flutter SDK.
final FlutterSdk sdk = FlutterSdk.getFlutterSdk(project);
if (sdk == null) {
return null;
}
try {
final String path = FlutterSdkUtil.pathToFlutterTool(sdk.getHomePath());
return createCommand(sdk.getHomePath(), path, ImmutableList.of("daemon"));
}
catch (ExecutionException e) {
FlutterUtils.warn(LOG, "Unable to calculate command to start Flutter daemon", e);
return null;
}
}
private static GeneralCommandLine createCommand(String workDir, String command, List<String> arguments) {
final GeneralCommandLine result = new GeneralCommandLine().withWorkDirectory(workDir);
result.setCharset(StandardCharsets.UTF_8);
result.setExePath(FileUtil.toSystemDependentName(command));
result.withEnvironment(FlutterSdkUtil.FLUTTER_HOST_ENV, (new FlutterSdkUtil()).getFlutterHostEnvValue());
for (String argument : arguments) {
result.addParameter(argument);
}
return result;
}
}