forked from smooth80/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFlutterModuleUtils.java
409 lines (353 loc) · 13.9 KB
/
FlutterModuleUtils.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* 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.utils;
import com.intellij.execution.RunManager;
import com.intellij.execution.RunnerAndConfigurationSettings;
import com.intellij.facet.Facet;
import com.intellij.facet.FacetManager;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.*;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.serviceContainer.AlreadyDisposedException;
import com.intellij.ui.EditorNotifications;
import com.intellij.util.PlatformUtils;
import com.jetbrains.lang.dart.sdk.DartSdk;
import io.flutter.FlutterUtils;
import io.flutter.actions.FlutterBuildActionGroup;
import io.flutter.bazel.Workspace;
import io.flutter.bazel.WorkspaceCache;
import io.flutter.dart.DartPlugin;
import io.flutter.pub.PubRoot;
import io.flutter.pub.PubRootCache;
import io.flutter.pub.PubRoots;
import io.flutter.run.FlutterRunConfigurationType;
import io.flutter.run.SdkFields;
import io.flutter.run.SdkRunConfig;
import io.flutter.sdk.FlutterSdk;
import io.flutter.sdk.FlutterSdkUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class FlutterModuleUtils {
public static final String DEPRECATED_FLUTTER_MODULE_TYPE_ID = "WEB_MODULE";
private FlutterModuleUtils() {
}
/**
* This provides the {@link ModuleType} ID for Flutter modules to be assigned by the {@link io.flutter.module.FlutterModuleBuilder} and
* elsewhere in the Flutter plugin.
* <p/>
* For Flutter module detection however, {@link ModuleType}s should not be used to determine Flutterness.
*/
@SuppressWarnings("SameReturnValue")
@NotNull
public static String getModuleTypeIDForFlutter() {
return "JAVA_MODULE";
}
public static ModuleType<?> getFlutterModuleType() {
return ModuleTypeManager.getInstance().findByID(getModuleTypeIDForFlutter());
}
/**
* Return true if the passed module is of a Flutter type. Before version M16 this plugin had its own Flutter {@link ModuleType}.
* Post M16 a Flutter module is defined by the following:
* <p>
* <code>
* [Flutter support enabled for a module] ===
* [Dart support enabled && referenced Dart SDK is the one inside a Flutter SDK]
* </code>
*/
public static boolean isFlutterModule(@Nullable final Module module) {
if (module == null || module.isDisposed()) return false;
if (PlatformUtils.isIntelliJ() || FlutterUtils.isAndroidStudio()) {
// [Flutter support enabled for a module] ===
// [Dart support enabled && referenced Dart SDK is the one inside a Flutter SDK]
final DartSdk dartSdk = DartPlugin.getDartSdk(module.getProject());
final String dartSdkPath = dartSdk != null ? dartSdk.getHomePath() : null;
return validDartSdkPath(dartSdkPath) && DartPlugin.isDartSdkEnabled(module);
}
else {
// If not IntelliJ, assume a small IDE (no multi-module project support).
return declaresFlutter(module);
}
}
private static boolean validDartSdkPath(String path) {
return path != null &&
(path.endsWith(FlutterSdk.DART_SDK_SUFFIX) ||
path.endsWith(FlutterSdk.LINUX_DART_SUFFIX) ||
path.endsWith(FlutterSdk.LOCAL_DART_SUFFIX) ||
path.endsWith(FlutterSdk.MAC_DART_SUFFIX));
}
public static boolean hasInternalDartSdkPath(Project project) {
final DartSdk dartSdk = DartPlugin.getDartSdk(project);
final String dartSdkPath = dartSdk != null ? dartSdk.getHomePath() : "";
return dartSdkPath.endsWith(FlutterSdk.LINUX_DART_SUFFIX) || dartSdkPath.endsWith(FlutterSdk.MAC_DART_SUFFIX);
}
public static boolean hasFlutterModule(@NotNull Project project) {
if (project.isDisposed()) return false;
return CollectionUtils.anyMatch(getModules(project), FlutterModuleUtils::isFlutterModule);
}
public static boolean isInFlutterModule(@NotNull PsiElement element) {
return isFlutterModule(ModuleUtilCore.findModuleForPsiElement(element));
}
/**
* Return the Flutter {@link Workspace} if there is at least one module that is determined to be a Flutter module by the workspace, and
* has the Dart SDK enabled module.
*/
@Nullable
public static Workspace getFlutterBazelWorkspace(@Nullable Project project) {
if (project == null || project.isDisposed()) return null;
final Workspace workspace = WorkspaceCache.getInstance(project).get();
if (workspace == null) return null;
for (Module module : getModules(project)) {
if (DartPlugin.isDartSdkEnabled(module)) {
return workspace;
}
}
return null;
}
/**
* Return true if the passed {@link Project} is a Bazel Flutter {@link Project}. If the {@link Workspace} is needed after this call,
* {@link #getFlutterBazelWorkspace(Project)} should be used.
*/
public static boolean isFlutterBazelProject(@Nullable Project project) {
return getFlutterBazelWorkspace(project) != null;
}
@Nullable
public static VirtualFile findXcodeProjectFile(@NotNull Project project) {
if (project.isDisposed()) return null;
// Look for Xcode metadata file in `ios/`.
for (PubRoot root : PubRoots.forProject(project)) {
final VirtualFile dir = root.getiOsDir();
final VirtualFile file = findPreferedXcodeMetadataFile(dir);
if (file != null) {
return file;
}
}
// Look for Xcode metadata in `example/ios/`.
for (PubRoot root : PubRoots.forProject(project)) {
final VirtualFile exampleDir = root.getExampleDir();
if (exampleDir != null) {
VirtualFile iosDir = exampleDir.findChild("ios");
if (iosDir == null) {
iosDir = exampleDir.findChild(".ios");
}
final VirtualFile file = findPreferedXcodeMetadataFile(iosDir);
if (file != null) {
return file;
}
}
}
return null;
}
@Nullable
private static VirtualFile findPreferedXcodeMetadataFile(@Nullable VirtualFile iosDir) {
if (iosDir != null) {
// Prefer .xcworkspace.
for (VirtualFile child : iosDir.getChildren()) {
if (FlutterUtils.isXcodeWorkspaceFileName(child.getName())) {
return child;
}
}
// But fall-back to a project.
for (VirtualFile child : iosDir.getChildren()) {
if (FlutterUtils.isXcodeProjectFileName(child.getName())) {
return child;
}
}
}
return null;
}
@NotNull
public static Module[] getModules(@NotNull Project project) {
// A disposed project has no modules.
if (project.isDisposed()) return Module.EMPTY_ARRAY;
return ModuleManager.getInstance(project).getModules();
}
/**
* Check if any module in this project {@link #declaresFlutter(Module)}.
*/
public static boolean declaresFlutter(@NotNull Project project) {
if (project.isDisposed()) return false;
return CollectionUtils.anyMatch(getModules(project), FlutterModuleUtils::declaresFlutter);
}
/**
* Ensures a Flutter run configuration is selected in the run pull down.
*/
public static void ensureRunConfigSelected(@NotNull Project project) {
if (project.isDisposed()) return;
final FlutterRunConfigurationType configType = FlutterRunConfigurationType.getInstance();
final RunManager runManager = RunManager.getInstance(project);
if (!runManager.getConfigurationsList(configType).isEmpty()) {
if (runManager.getSelectedConfiguration() == null) {
final List<RunnerAndConfigurationSettings> flutterConfigs = runManager.getConfigurationSettingsList(configType);
if (!flutterConfigs.isEmpty()) {
runManager.setSelectedConfiguration(flutterConfigs.get(0));
}
}
}
}
/**
* Creates a Flutter run configuration if none exists.
*/
public static void autoCreateRunConfig(@NotNull Project project, @NotNull PubRoot root) {
assert ApplicationManager.getApplication().isReadAccessAllowed();
if (project.isDisposed()) return;
VirtualFile main = root.getLibMain();
if (main == null || !main.exists()) {
// Check for example main.dart in plugins
main = root.getExampleLibMain();
if (main == null || !main.exists()) {
return;
}
}
final FlutterRunConfigurationType configType = FlutterRunConfigurationType.getInstance();
final RunManager runManager = RunManager.getInstance(project);
if (!runManager.getConfigurationsList(configType).isEmpty()) {
// Don't create a run config if one already exists.
return;
}
final RunnerAndConfigurationSettings settings = runManager.createConfiguration(project.getName(), configType.getFactory());
final SdkRunConfig config = (SdkRunConfig)settings.getConfiguration();
// Set config name.
config.setName("main.dart");
// Set fields.
final SdkFields fields = new SdkFields();
fields.setFilePath(main.getPath());
config.setFields(fields);
runManager.addConfiguration(settings, false);
runManager.setSelectedConfiguration(settings);
}
/**
* If no files are open, show lib/main.dart for the given PubRoot.
*/
public static void autoShowMain(@NotNull Project project, @NotNull PubRoot root) {
if (project.isDisposed()) return;
final VirtualFile main = root.getFileToOpen();
if (main == null) return;
DumbService.getInstance(project).runWhenSmart(() -> {
final FileEditorManager manager = FileEditorManager.getInstance(project);
if (manager.getAllEditors().length == 0) {
manager.openFile(main, true);
}
});
}
/**
* Introspect into the module's content roots, looking for a pubspec.yaml that references flutter.
* <p/>
* True is returned if any of the PubRoots associated with the {@link Module} have a pubspec that declares flutter.
*/
public static boolean declaresFlutter(@NotNull Module module) {
try {
final PubRootCache cache = PubRootCache.getInstance(module.getProject());
for (PubRoot root : cache.getRoots(module)) {
if (root.declaresFlutter()) {
return true;
}
}
return false;
} catch (AlreadyDisposedException ignored) {
return false;
}
}
/**
* Find flutter modules.
* <p>
* Flutter modules are defined as:
* 1. being tagged with the #FlutterModuleType, or
* 2. containing a pubspec that #declaresFlutterDependency
*/
@NotNull
public static List<Module> findModulesWithFlutterContents(@NotNull Project project) {
return CollectionUtils.filter(getModules(project), module -> isFlutterModule(module) || declaresFlutter(module));
}
public static boolean convertFromDeprecatedModuleType(@NotNull Project project) {
boolean modulesConverted = false;
// Only automatically convert from older module types to JAVA_MODULE types if we're running in Android Studio.
if (FlutterUtils.isAndroidStudio()) {
for (Module module : getModules(project)) {
if (isDeprecatedFlutterModuleType(module)) {
setFlutterModuleType(module);
modulesConverted = true;
}
}
}
return modulesConverted;
}
public static boolean isDeprecatedFlutterModuleType(@NotNull Module module) {
if (!DEPRECATED_FLUTTER_MODULE_TYPE_ID.equals(module.getOptionValue("type"))) {
return false;
}
// Validate that the pubspec references flutter.
return declaresFlutter(module);
}
public static boolean isInFlutterAndroidModule(@NotNull Project project, @NotNull VirtualFile file) {
final Module module = FlutterBuildActionGroup.findFlutterModule(project, file);
if (module != null) {
for (Facet<?> facet : FacetManager.getInstance(module).getAllFacets()) {
if ("Android".equals(facet.getName())) {
return declaresFlutter(project);
}
}
}
return false;
}
/**
* Set the passed module to the module type used by Flutter, defined by {@link #getModuleTypeIDForFlutter()}.
*/
public static void setFlutterModuleType(@NotNull Module module) {
module.setOption(Module.ELEMENT_TYPE, getModuleTypeIDForFlutter());
}
public static void setFlutterModuleAndReload(@NotNull Module module, @NotNull Project project) {
if (project.isDisposed()) return;
setFlutterModuleType(module);
enableDartSDK(module);
project.save();
EditorNotifications.getInstance(project).updateAllNotifications();
ProjectManager.getInstance().reloadProject(project);
}
public static void enableDartSDK(@NotNull Module module) {
if (FlutterSdk.getFlutterSdk(module.getProject()) != null) {
return;
}
// parse the .dart_tool/flutter_config.json or .packages file
String sdkPath = FlutterSdkUtil.guessFlutterSdkFromPackagesFile(module);
if (sdkPath != null) {
FlutterSdkUtil.updateKnownSdkPaths(sdkPath);
}
// try and locate flutter on the path
if (sdkPath == null) {
sdkPath = FlutterSdkUtil.locateSdkFromPath();
if (sdkPath != null) {
FlutterSdkUtil.updateKnownSdkPaths(sdkPath);
}
}
if (sdkPath == null) {
final String[] flutterSdkPaths = FlutterSdkUtil.getKnownFlutterSdkPaths();
if (flutterSdkPaths.length > 0) {
sdkPath = flutterSdkPaths[0];
}
}
if (sdkPath != null) {
final FlutterSdk flutterSdk = FlutterSdk.forPath(sdkPath);
if (flutterSdk == null) {
return;
}
final String dartSdkPath = flutterSdk.getDartSdkPath();
if (dartSdkPath == null) {
return; // Not cached. TODO call flutterSdk.sync() here?
}
ApplicationManager.getApplication().runWriteAction(() -> {
DartPlugin.ensureDartSdkConfigured(module.getProject(), dartSdkPath);
DartPlugin.enableDartSdk(module);
});
}
}
}