forked from flutter/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSdkFields.java
More file actions
290 lines (252 loc) · 9.69 KB
/
SdkFields.java
File metadata and controls
290 lines (252 loc) · 9.69 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
/*
* Copyright 2016 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;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.configurations.RuntimeConfigurationError;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ArrayUtil;
import com.intellij.util.execution.ParametersListUtil;
import com.intellij.util.xmlb.annotations.OptionTag;
import com.intellij.util.xmlb.annotations.XMap;
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.FlutterUtils;
import io.flutter.dart.DartPlugin;
import io.flutter.logging.PluginLogger;
import io.flutter.pub.PubRoot;
import io.flutter.pub.PubRootCache;
import io.flutter.run.common.RunMode;
import io.flutter.run.daemon.DevToolsInstance;
import io.flutter.run.daemon.DevToolsService;
import io.flutter.sdk.FlutterCommand;
import io.flutter.sdk.FlutterSdk;
import io.flutter.settings.FlutterSettings;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
/**
* Fields used when launching an app using the Flutter SDK (non-bazel).
*/
public class SdkFields {
private static final @NotNull Logger LOG = PluginLogger.createLogger(SdkFields.class);
private @Nullable String filePath;
private @Nullable String buildFlavor;
private @Nullable String additionalArgs;
private @Nullable String attachArgs;
private @NotNull Map<String, String> envs = new LinkedHashMap<>();
public SdkFields() {
}
/**
* Creates SDK fields from a Dart file containing a main method.
*/
public SdkFields(@NotNull VirtualFile launchFile) {
filePath = launchFile.getPath();
}
@Nullable
public String getFilePath() {
return filePath;
}
public void setFilePath(final @Nullable String path) {
filePath = path;
}
@Nullable
public String getBuildFlavor() {
return buildFlavor;
}
public void setBuildFlavor(final @Nullable String buildFlavor) {
this.buildFlavor = buildFlavor;
}
@Nullable
public String getAdditionalArgs() {
return additionalArgs;
}
public void setAdditionalArgs(final @Nullable String additionalArgs) {
this.additionalArgs = additionalArgs;
}
public boolean hasAdditionalArgs() {
return additionalArgs != null;
}
public String[] getAdditionalArgsParsed() {
if (hasAdditionalArgs()) {
assert additionalArgs != null;
return ParametersListUtil.parse(additionalArgs, false, false, true).stream().filter(s -> !s.isEmpty())
.toArray(String[]::new);
}
return new String[0];
}
@Nullable
public String getAttachArgs() {
return attachArgs;
}
public void setAttachArgs(@Nullable String attachArgs) {
this.attachArgs = attachArgs;
}
public boolean hasAttachArgs() {
return attachArgs != null;
}
public String[] getAttachArgsParsed() {
if (hasAttachArgs()) {
assert attachArgs != null;
return ParametersListUtil.parse(attachArgs, false, false, true).stream().filter(s -> !s.isEmpty())
.toArray(String[]::new);
}
return new String[0];
}
/**
* Present only for deserializing old run configs.
*/
@SuppressWarnings("SameReturnValue")
@Deprecated
@Nullable
public String getWorkingDirectory() {
return null;
}
/**
* Present only for deserializing old run configs.
*/
@SuppressWarnings("EmptyMethod")
@Deprecated
public void setWorkingDirectory(final @Nullable String dir) {
}
@NotNull
@OptionTag
@XMap
public Map<String, String> getEnvs() {
return envs;
}
public void setEnvs(final Map<String, String> envs) {
if (envs != null) { // null comes from old projects or if storage corrupted
this.envs = envs;
}
}
/**
* Reports any errors that the user should correct.
* <p>This will be called while the user is typing; see RunConfiguration.checkConfiguration.
*
* @throws RuntimeConfigurationError for an error that the user must correct before running.
*/
void checkRunnable(@NotNull Project project) throws RuntimeConfigurationError {
// TODO(pq): consider validating additional args values
checkSdk(project);
final MainFile.Result main = MainFile.verify(filePath, project);
if (!main.canLaunch()) {
throw new RuntimeConfigurationError(main.getError());
}
if (PubRootCache.getInstance(project).getRoot(main.get().getAppDir()) == null) {
throw new RuntimeConfigurationError("Entrypoint isn't within a Flutter pub root");
}
}
/**
* Create a command to run 'flutter run --machine'.
*/
public GeneralCommandLine createFlutterSdkRunCommand(
@NotNull Project project,
@NotNull RunMode runMode,
@NotNull FlutterLaunchMode flutterLaunchMode,
@NotNull FlutterDevice device,
boolean firstRun) throws ExecutionException {
final MainFile main = MainFile.verify(filePath, project).get();
final FlutterSdk flutterSdk = FlutterSdk.getFlutterSdk(project);
if (flutterSdk == null) {
throw new ExecutionException(FlutterBundle.message("flutter.sdk.is.not.configured"));
}
final PubRoot root = PubRoot.forDirectory(main.getAppDir());
if (root == null) {
throw new ExecutionException("Entrypoint isn't within a Flutter pub root");
}
final FlutterCommand command;
String[] args = getAdditionalArgsParsed();
if (buildFlavor != null) {
args = ArrayUtil.append(args, "--flavor=" + buildFlavor);
}
if (FlutterSettings.getInstance().isShowStructuredErrors()) {
args = ArrayUtil.append(args, "--dart-define=flutter.inspector.structuredErrors=true");
}
try {
final ProgressManager progress = ProgressManager.getInstance();
final CompletableFuture<DevToolsInstance> devToolsFuture = new CompletableFuture<>();
progress.runProcessWithProgressSynchronously(() -> {
progress.getProgressIndicator().setIndeterminate(true);
try {
final CompletableFuture<DevToolsInstance> futureInstance = DevToolsService.getInstance(project).getDevToolsInstance();
if (firstRun) {
devToolsFuture.complete(futureInstance.get(30, TimeUnit.SECONDS));
}
else {
// Skip waiting if this isn't the first time running this project. If DevTools isn't available by now, there's likely to be
// something wrong that won't be fixed by restarting, so we don't want to keep delaying run.
final DevToolsInstance instance = futureInstance.getNow(null);
if (instance == null) {
devToolsFuture.completeExceptionally(new Exception("DevTools instance not available after first run."));
}
else {
devToolsFuture.complete(instance);
}
}
}
catch (Exception e) {
devToolsFuture.completeExceptionally(e);
}
}, "Starting DevTools", false, project);
final DevToolsInstance instance = devToolsFuture.get();
//noinspection HttpUrlsUsage
args = ArrayUtil.append(args, "--devtools-server-address=http://" + instance.host() + ":" + instance.port());
}
catch (Exception e) {
FlutterUtils.warn(LOG, "Error while starting DevTools", e, true);
}
command = flutterSdk.flutterRun(root, main.getFile(), device, runMode, flutterLaunchMode, project, args);
final GeneralCommandLine commandLine = command.createGeneralCommandLine(project);
commandLine.getEnvironment().putAll(getEnvs());
Analytics.updateEnvironment(commandLine);
commandLine.withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE);
return commandLine;
}
/**
* Create a command to run 'flutter attach --machine'.
*/
public GeneralCommandLine createFlutterSdkAttachCommand(@NotNull Project project,
@NotNull FlutterLaunchMode flutterLaunchMode,
@Nullable FlutterDevice device) throws ExecutionException {
final MainFile main = MainFile.verify(filePath, project).get();
final FlutterSdk flutterSdk = FlutterSdk.getFlutterSdk(project);
if (flutterSdk == null) {
throw new ExecutionException(FlutterBundle.message("flutter.sdk.is.not.configured"));
}
final PubRoot root = PubRoot.forDirectory(main.getAppDir());
if (root == null) {
throw new ExecutionException("Entrypoint isn't within a Flutter pub root");
}
final String[] args = getAttachArgsParsed();
final FlutterCommand command = flutterSdk.flutterAttach(root, main.getFile(), device, flutterLaunchMode, args);
return command.createGeneralCommandLine(project);
}
SdkFields copy() {
final SdkFields copy = new SdkFields();
copy.setFilePath(filePath);
copy.setAdditionalArgs(additionalArgs);
copy.setBuildFlavor(buildFlavor);
copy.envs.putAll(envs);
return copy;
}
private static void checkSdk(@NotNull Project project) throws RuntimeConfigurationError {
// TODO(skybrian) shouldn't this be flutter SDK?
final DartSdk sdk = DartPlugin.getDartSdk(project);
if (sdk == null) {
throw new RuntimeConfigurationError(FlutterBundle.message("dart.sdk.is.not.configured"),
() -> DartConfigurable.openDartSettings(project));
}
}
}