forked from flutter/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBazelFields.java
More file actions
377 lines (326 loc) · 13.7 KB
/
BazelFields.java
File metadata and controls
377 lines (326 loc) · 13.7 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
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
/*
* Copyright 2017 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.bazel;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.configurations.CommandLineTokenizer;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.configurations.RuntimeConfigurationError;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.lang.dart.sdk.DartConfigurable;
import com.jetbrains.lang.dart.sdk.DartSdk;
import com.jetbrains.lang.dart.analytics.Analytics;
import io.flutter.FlutterBundle;
import io.flutter.FlutterMessages;
import io.flutter.bazel.Workspace;
import io.flutter.bazel.WorkspaceCache;
import io.flutter.dart.DartPlugin;
import io.flutter.logging.PluginLogger;
import io.flutter.run.FlutterDevice;
import io.flutter.run.common.RunMode;
import io.flutter.run.daemon.DevToolsInstance;
import io.flutter.run.daemon.DevToolsService;
import io.flutter.settings.FlutterSettings;
import io.flutter.utils.ElementIO;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import static io.flutter.run.common.RunMode.DEBUG;
import static io.flutter.run.common.RunMode.PROFILE;
/**
* The fields in a Bazel run configuration.
* <p>
* This class is immutable.
*/
public class BazelFields {
private static final @NotNull Logger LOG = PluginLogger.createLogger(BazelFields.class);
/**
* The Bazel target or Dart file to invoke.
*/
@Nullable
private final String target;
/**
* Whether or not to run the app with --define flutter_build_mode=release.
*
* <p>
* If this is not set, then the flutter_build_mode will depend on which button
* the user pressed to run the app.
* <ul>
* <li>If the user pressed 'run' or 'debug', then flutter_build_mode=debug.</li>
* <li>If the user pressed 'profile', then flutter_build_mode=profile.</li>
* </ul>
*
* <p>
* If the user overrides --define flutter_build_mode in {@link #bazelArgs}, then this field will be ignored.
*/
private final boolean enableReleaseMode;
/**
* Parameters to pass to Bazel, such as --define release_channel=beta3.
*/
@Nullable
private final String bazelArgs;
/**
* This is to set a DevToolsService ahead of time, intended for testing.
*/
@Nullable
private final DevToolsService devToolsService;
/**
* Parameters to pass to Flutter, such as --start-paused.
*/
@Nullable
private final String additionalArgs;
BazelFields(@Nullable String target, @Nullable String bazelArgs, @Nullable String additionalArgs, boolean enableReleaseMode) {
this(target, bazelArgs, additionalArgs, enableReleaseMode, null);
}
BazelFields(@Nullable String target,
@Nullable String bazelArgs,
@Nullable String additionalArgs,
boolean enableReleaseMode,
@Nullable DevToolsService devToolsService) {
this.target = target;
this.bazelArgs = bazelArgs;
this.additionalArgs = additionalArgs;
this.enableReleaseMode = enableReleaseMode;
this.devToolsService = devToolsService;
}
/**
* Copy constructor
*/
BazelFields(@NotNull BazelFields original) {
target = original.target;
enableReleaseMode = original.enableReleaseMode;
bazelArgs = original.bazelArgs;
additionalArgs = original.additionalArgs;
devToolsService = original.devToolsService;
}
@Nullable
public String getBazelArgs() {
return bazelArgs;
}
@Nullable
public String getAdditionalArgs() {
return additionalArgs;
}
/**
* This can be either a bazel or a dart target.
*/
@Nullable
public String getTarget() {
return target;
}
public boolean getEnableReleaseMode() {
return enableReleaseMode;
}
BazelFields copy() {
return new BazelFields(this);
}
@Nullable
private String getRunScriptFromWorkspace(@NotNull final Project project) {
final Workspace workspace = getWorkspace(project);
String runScript = workspace == null ? null : workspace.getRunScript();
if (runScript != null) {
runScript = workspace.getRoot().getPath() + "/" + runScript;
}
return runScript;
}
private String getToolsScriptFromWorkspace(@NotNull final Project project) {
final Workspace workspace = getWorkspace(project);
String toolsScript = workspace == null ? null : workspace.getToolsScript();
if (toolsScript != null) {
toolsScript = workspace.getRoot().getPath() + File.separatorChar + toolsScript;
}
return toolsScript;
}
// TODO(djshuckerow): this is dependency injection; switch this to a framework as we need more DI.
@Nullable
protected Workspace getWorkspace(@NotNull Project project) {
return WorkspaceCache.getInstance(project).get();
}
/**
* Reports an error in the run config that the user should correct.
* <p>
* This will be called while the user is typing into a non-template run config.
* (See RunConfiguration.checkConfiguration.)
*
* @throws RuntimeConfigurationError for an error that the user must correct before running.
*/
void checkRunnable(@NotNull final Project project) throws RuntimeConfigurationError {
// The UI only shows one error message at a time.
// The order we do the checks here determines priority.
final DartSdk sdk = DartPlugin.getDartSdk(project);
if (sdk == null) {
throw new RuntimeConfigurationError(FlutterBundle.message("dart.sdk.is.not.configured"),
() -> DartConfigurable.openDartSettings(project));
}
final String runScript = getRunScriptFromWorkspace(project);
if (runScript == null) {
throw new RuntimeConfigurationError(FlutterBundle.message("flutter.run.bazel.noLaunchingScript"));
}
final VirtualFile scriptFile = LocalFileSystem.getInstance().findFileByPath(runScript);
if (scriptFile == null) {
throw new RuntimeConfigurationError(
FlutterBundle.message("flutter.run.bazel.launchingScriptNotFound", FileUtil.toSystemDependentName(runScript)));
}
// Check that target field is populated.
if (StringUtil.isEmptyOrSpaces(target)) {
throw new RuntimeConfigurationError(FlutterBundle.message("flutter.run.bazel.noBazelOrDartTargetSet"));
}
else if (!target.endsWith("dart") && !target.startsWith("//")) {
throw new RuntimeConfigurationError(FlutterBundle.message("flutter.run.bazel.startWithSlashSlash"));
}
}
GeneralCommandLine getLaunchCommand(@NotNull Project project,
@Nullable FlutterDevice device,
@NotNull RunMode mode) throws ExecutionException {
return getLaunchCommand(project, device, mode, false);
}
/**
* Returns the command to use to launch the Flutter app. (Via running the Bazel target.)
*/
GeneralCommandLine getLaunchCommand(@NotNull Project project,
@Nullable FlutterDevice device,
@NotNull RunMode mode, boolean isAttach)
throws ExecutionException {
try {
checkRunnable(project);
}
catch (RuntimeConfigurationError e) {
throw new ExecutionException(e);
}
final Workspace workspace = getWorkspace(project);
final String launchingScript = isAttach ? getToolsScriptFromWorkspace(project) : getRunScriptFromWorkspace(project);
assert launchingScript != null; // already checked
assert workspace != null; // if the workspace is null, then so is the launching script, therefore this was already checked.
final String target = getTarget();
assert target != null; // already checked
final String additionalArgs = getAdditionalArgs();
final GeneralCommandLine commandLine = new GeneralCommandLine()
.withWorkDirectory(workspace.getRoot().getPath());
commandLine.setCharset(StandardCharsets.UTF_8);
commandLine.setExePath(FileUtil.toSystemDependentName(launchingScript));
if (isAttach) {
commandLine.addParameter("attach");
}
final String inputBazelArgs = StringUtil.notNullize(bazelArgs);
if (!inputBazelArgs.isEmpty()) {
commandLine.addParameter(String.format("--bazel-options=%s", inputBazelArgs));
}
// Potentially add the flag related to build mode.
if (enableReleaseMode) {
commandLine.addParameter("--release");
}
else if (mode.equals(PROFILE)) {
commandLine.addParameter("--profile");
}
// Tell the flutter command-line tools that we want a machine interface on stdio.
commandLine.addParameter("--machine");
// Pause the app at startup in order to set breakpoints.
if (!enableReleaseMode && mode == DEBUG) {
commandLine.addParameter("--start-paused");
}
// User specified additional target arguments.
final CommandLineTokenizer additionalArgsTokenizer = new CommandLineTokenizer(
StringUtil.notNullize(additionalArgs));
while (additionalArgsTokenizer.hasMoreTokens()) {
commandLine.addParameter(additionalArgsTokenizer.nextToken());
}
final String enableBazelHotRestartParam = "--enable-google3-hot-reload";
final String disableBazelHotRestartParam = "--no-enable-google3-hot-reload";
final boolean hasEnabledArg = StringUtil.notNullize(additionalArgs).contains(enableBazelHotRestartParam);
final boolean hasDisabledArg = StringUtil.notNullize(additionalArgs).contains(disableBazelHotRestartParam);
if (!FlutterSettings.getInstance().isEnableBazelHotRestart() && hasDisabledArg) {
final Notification notification = new Notification(
FlutterMessages.FLUTTER_NOTIFICATION_GROUP_ID,
"Google3-specific hot restart is disabled by default",
"You can now remove this flag from your configuration's additional args: " + disableBazelHotRestartParam,
NotificationType.INFORMATION);
Notifications.Bus.notify(notification, project);
}
else if (FlutterSettings.getInstance().isEnableBazelHotRestart() && !hasEnabledArg && !hasDisabledArg) {
commandLine.addParameter(enableBazelHotRestartParam);
}
// Send in the deviceId.
if (device != null) {
commandLine.addParameter("-d");
commandLine.addParameter(device.deviceId());
final String message = workspace.getUpdatedIosRunMessage();
if (message != null && FlutterSettings.getInstance().isShowBazelIosRunNotification()) {
final String title = device.isIOS() ? "Running iOS apps has improved!" : "Try running an iOS app!";
final Notification notification = new Notification(
FlutterMessages.FLUTTER_NOTIFICATION_GROUP_ID,
title,
message,
NotificationType.INFORMATION);
Notifications.Bus.notify(notification, project);
FlutterSettings.getInstance().setShowBazelIosRunNotification(false);
}
}
try {
final ProgressManager progress = ProgressManager.getInstance();
final CompletableFuture<DevToolsInstance> devToolsFuture = new CompletableFuture<>();
progress.runProcessWithProgressSynchronously(() -> {
progress.getProgressIndicator().setIndeterminate(true);
try {
final DevToolsService service = this.devToolsService == null ? DevToolsService.getInstance(project) : this.devToolsService;
final DevToolsInstance instance = service.getDevToolsInstance().get(30, TimeUnit.SECONDS);
if (instance != null) {
devToolsFuture.complete(instance);
}
else {
devToolsFuture.completeExceptionally(new Exception("DevTools instance not available."));
}
}
catch (Exception e) {
devToolsFuture.completeExceptionally(e);
}
}, "Starting DevTools", false, project);
final DevToolsInstance instance = devToolsFuture.get();
//noinspection HttpUrlsUsage
commandLine.addParameter("--devtools-server-address=http://" + instance.host() + ":" + instance.port());
}
catch (Exception e) {
LOG.info(e);
}
commandLine.addParameter(target);
Analytics.updateEnvironment(commandLine);
return commandLine;
}
public void writeTo(Element element) {
ElementIO.addOption(element, "target", target);
ElementIO.addOption(element, "bazelArgs", bazelArgs);
ElementIO.addOption(element, "additionalArgs", additionalArgs);
ElementIO.addOption(element, "enableReleaseMode", Boolean.toString(enableReleaseMode));
}
public static BazelFields readFrom(Element element) {
final Map<String, String> options = ElementIO.readOptions(element);
// Use old field name of bazelTarget if the newer one has not been set.
final String bazelOrDartTarget =
options.get("target") != null ? options.get("target") : options.get("bazelTarget");
final String bazelArgs = options.get("bazelArgs");
final String additionalArgs = options.get("additionalArgs");
final String enableReleaseMode = options.get("enableReleaseMode");
try {
return new BazelFields(bazelOrDartTarget, bazelArgs, additionalArgs, Boolean.parseBoolean(enableReleaseMode));
}
catch (IllegalArgumentException e) {
throw new InvalidDataException(e.getMessage());
}
}
}