forked from flutter/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevToolsServerTask.java
More file actions
300 lines (265 loc) · 11.5 KB
/
DevToolsServerTask.java
File metadata and controls
300 lines (265 loc) · 11.5 KB
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
/*
* Copyright 2025 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.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
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.FlutterMessages;
import io.flutter.FlutterUtils;
import io.flutter.bazel.Workspace;
import io.flutter.bazel.WorkspaceCache;
import io.flutter.dart.DtdUtils;
import io.flutter.logging.PluginLogger;
import io.flutter.sdk.FlutterSdk;
import io.flutter.sdk.FlutterSdkUtil;
import io.flutter.utils.JsonUtils;
import io.flutter.utils.MostlySilentColoredProcessHandler;
import io.flutter.utils.OpenApiUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
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;
class DevToolsServerTask extends Task.Backgroundable {
private @NotNull static final Logger LOG = PluginLogger.createLogger(DevToolsServerTask.class);
public @NotNull static final String LOCAL_DEVTOOLS_DIR = "flutter.local.devtools.dir";
public @NotNull static final String LOCAL_DEVTOOLS_ARGS = "flutter.local.devtools.args";
private @NotNull final Project project;
private @NotNull final AtomicReference<CompletableFuture<DevToolsInstance>> devToolsFutureRef;
private @Nullable DaemonApi daemonApi;
private @Nullable ProcessHandler process;
public DevToolsServerTask(@NotNull Project project,
@NotNull String title,
@NotNull AtomicReference<CompletableFuture<DevToolsInstance>> devToolsFutureRef) {
super(project, title, true);
this.project = project;
this.devToolsFutureRef = devToolsFutureRef;
}
@Override
public void run(@NotNull ProgressIndicator progressIndicator) {
try {
progressIndicator.setFraction(30);
progressIndicator.setText2("Init");
LOG.info("Finding or starting DevTools");
// If we are in a Bazel workspace, start the server.
// Note: This is only for internal usages.
final WorkspaceCache workspaceCache = WorkspaceCache.getInstance(project);
if (workspaceCache.isBazel()) {
progressIndicator.setFraction(60);
progressIndicator.setText2("Starting server");
setUpWithDart(createCommand(workspaceCache.get().getRoot().getPath(), workspaceCache.get().getDevToolsScript(),
ImmutableList.of("--machine")));
return;
}
// This is only for development to check integration with a locally run DevTools server.
// To enable, follow the instructions in:
// https://github.com/flutter/flutter-intellij/blob/master/CONTRIBUTING.md#developing-with-local-devtools
final String localDevToolsDir = Registry.stringValue(LOCAL_DEVTOOLS_DIR);
if (!localDevToolsDir.isEmpty()) {
LOG.info("Starting local DevTools server at: " + localDevToolsDir);
progressIndicator.setFraction(60);
progressIndicator.setText2("Starting local server");
setUpLocalServer(localDevToolsDir);
}
// Wait for the Dart Plugin to start the DevTools server.
final CompletableFuture<DevToolsInstance> devToolsFuture = checkForDartPluginInitiatedDevToolsWithRetries(progressIndicator);
devToolsFuture.whenComplete((devTools, error) -> {
if (error != null) {
cancelWithError(new Exception(error));
}
else {
devToolsFutureRef.get().complete(devTools);
}
});
}
catch (java.util.concurrent.ExecutionException | InterruptedException e) {
cancelWithError(e);
}
}
@Override
public void onThrowable(@NotNull Throwable error) {
cancelWithError(new Exception(error));
}
private void setUpLocalServer(@NotNull String localDevToolsDir) throws java.util.concurrent.ExecutionException, InterruptedException {
final DtdUtils dtdUtils = new DtdUtils();
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, "dt", args));
}
private void setUpInDevMode(@NotNull 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) {
cancelWithError(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) {
cancelWithError(e);
}
}
private CompletableFuture<DevToolsInstance> checkForDartPluginInitiatedDevToolsWithRetries(@NotNull ProgressIndicator progressIndicator)
throws InterruptedException {
progressIndicator.setText2("Waiting for server with DTD");
final CompletableFuture<DevToolsInstance> devToolsFuture = new CompletableFuture<>();
final long msBetweenRetries = 1500;
int retries = 0;
while (retries < 10) {
retries++;
final double currentProgress = progressIndicator.getFraction();
progressIndicator.setFraction(Math.min(currentProgress + 10, 95));
final @Nullable DevToolsInstance devTools = createDevToolsInstanceFromDartPluginUri();
if (devTools != null) {
LOG.debug("Dart plugin DevTools ready after " + retries + " tries.");
devToolsFuture.complete(devTools);
return devToolsFuture;
}
else {
LOG.debug("Dart plugin DevTools not ready after " + retries + " tries.");
Thread.sleep(msBetweenRetries);
}
}
devToolsFuture.completeExceptionally(new Exception("Timed out waiting for Dart plugin to start DevTools."));
return devToolsFuture;
}
private @Nullable DevToolsInstance createDevToolsInstanceFromDartPluginUri() {
final String dartPluginUri = DartDevToolsService.getInstance(project).getDevToolsHostAndPort();
if (dartPluginUri == null) {
return null;
}
String[] parts = dartPluginUri.split(":");
String host = parts[0];
Integer port = Integer.parseInt(parts[1]);
if (host == null || port == null) {
return null;
}
return new DevToolsInstance(host, port);
}
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 {
cancelWithError("DevTools port was invalid");
}
}
}
catch (JsonSyntaxException e) {
cancelWithError(e);
}
}
}
private static @NotNull 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;
}
private void cancelWithError(String message) {
cancelWithError(new Exception(message));
}
private void cancelWithError(Exception exception) {
final String errorTitle = "DevTools server start-up failure.";
FlutterUtils.warn(LOG, errorTitle, exception);
devToolsFutureRef.get().completeExceptionally(new Exception(errorTitle));
showErrorNotification(errorTitle, exception.getMessage());
throw new ProcessCanceledException(exception);
}
private void showErrorNotification(String errorTitle, String errorDetails) {
OpenApiUtils.safeInvokeLater(() -> {
final Notification notification = new Notification(FlutterMessages.FLUTTER_NOTIFICATION_GROUP_ID,
"DevTools",
errorTitle + " " + errorDetails,
NotificationType.WARNING);
notification.addAction(new AnAction("Dismiss") {
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
notification.expire();
}
});
Notifications.Bus.notify(notification, project);
});
}
}