forked from flutter/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlutterBuildActionGroup.java
More file actions
256 lines (223 loc) · 7.59 KB
/
FlutterBuildActionGroup.java
File metadata and controls
256 lines (223 loc) · 7.59 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
/*
* Copyright 2019 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.actions;
import com.intellij.execution.process.ColoredProcessHandler;
import com.intellij.execution.process.ProcessEvent;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.lang.dart.analytics.Analytics;
import com.jetbrains.lang.dart.analytics.AnalyticsData;
import io.flutter.FlutterMessages;
import io.flutter.pub.PubRoot;
import io.flutter.pub.PubRoots;
import io.flutter.sdk.FlutterSdk;
import io.flutter.utils.FlutterModuleUtils;
import io.flutter.utils.OpenApiUtils;
import io.flutter.utils.ProcessAdapter;
import io.flutter.utils.ProgressHelper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class FlutterBuildActionGroup extends DefaultActionGroup {
public static void build(@NotNull Project project,
@NotNull PubRoot pubRoot,
@NotNull FlutterSdk sdk,
@NotNull BuildType buildType,
@Nullable String desc) {
final ProgressHelper progressHelper = new ProgressHelper(project);
progressHelper.start(desc == null ? "building" : desc);
ProcessAdapter processAdapter = new ProcessAdapter() {
@Override
public void processTerminated(@NotNull ProcessEvent event) {
progressHelper.done();
final int exitCode = event.getExitCode();
if (exitCode != 0) {
FlutterMessages.showError("Error while building " + buildType, "`flutter build` returned: " + exitCode, project);
}
}
};
final Module module = pubRoot.getModule(project);
if (module != null) {
sdk.flutterBuild(pubRoot, buildType.type).startInModuleConsole(module, pubRoot::refresh, processAdapter);
}
else {
final ColoredProcessHandler processHandler = sdk.flutterBuild(pubRoot, buildType.type).startInConsole(project);
if (processHandler == null) {
progressHelper.done();
}
else {
processHandler.addProcessListener(processAdapter);
}
}
}
public enum BuildType {
AAR("aar"),
APK("apk"),
APP_BUNDLE("appbundle"),
BUNDLE("bundle"),
IOS("ios"),
LINUX("linux"),
MACOS("macos"),
WEB("web"),
WINDOWS("windows");
final public String type;
BuildType(String type) {
this.type = type;
}
}
@Override
public void update(@NotNull AnActionEvent event) {
final Presentation presentation = event.getPresentation();
final boolean enabled = isInFlutterModule(event);
presentation.setEnabled(enabled);
presentation.setVisible(enabled);
}
@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
private static boolean isInFlutterModule(@NotNull AnActionEvent event) {
final Project project = event.getProject();
if (project == null) {
return false;
}
return FlutterModuleUtils.hasFlutterModule(project);
}
@Nullable
public static Module findFlutterModule(@NotNull Project project, @NotNull VirtualFile file) {
Module module = ModuleUtilCore.findModuleForFile(file, project);
if (module == null) {
return null;
}
if (FlutterModuleUtils.declaresFlutter(module)) {
return module;
}
// We may get here if the file is in the Android module of a Flutter module project.
var root = OpenApiUtils.getContentRoots(module)[0];
if (root == null) return null;
final VirtualFile parent = root.getParent();
if (parent == null) return null;
module = ModuleUtilCore.findModuleForFile(parent, project);
if (module == null) {
return null;
}
return FlutterModuleUtils.declaresFlutter(module) ? module : null;
}
abstract public static class FlutterBuildAction extends AnAction {
public static String ID_PREFIX = "flutter.build";
@NotNull
abstract protected BuildType buildType();
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
final Presentation presentation = event.getPresentation();
final Project project = event.getProject();
if (project == null) {
return;
}
Analytics.report(AnalyticsData.forAction(ID_PREFIX + "." + buildType().type, event.getPlace(), project));
final FlutterSdk sdk = FlutterSdk.getFlutterSdk(project);
if (sdk == null) {
return;
}
final PubRoot pubRoot = PubRoot.forEventWithRefresh(event);
final BuildType buildType = buildType();
if (pubRoot != null) {
build(project, pubRoot, sdk, buildType, presentation.getDescription());
}
else {
var roots = PubRoots.forProject(project);
for (PubRoot sub : roots) {
build(project, sub, sdk, buildType, presentation.getDescription());
}
}
}
@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
}
public static class AAR extends FlutterBuildAction {
@Override
protected @NotNull BuildType buildType() {
return BuildType.AAR;
}
}
public static class APK extends FlutterBuildAction {
@Override
protected @NotNull BuildType buildType() {
return BuildType.APK;
}
}
public static class AppBundle extends FlutterBuildAction {
@Override
protected @NotNull BuildType buildType() {
return BuildType.APP_BUNDLE;
}
}
public static class Bundle extends FlutterBuildAction {
@Override
protected @NotNull BuildType buildType() {
return BuildType.BUNDLE;
}
}
public static class Ios extends FlutterBuildAction {
@Override
protected @NotNull BuildType buildType() {
return BuildType.IOS;
}
@Override
public void update(@NotNull AnActionEvent event) {
final Presentation presentation = event.getPresentation();
presentation.setEnabled(SystemInfo.isMac);
}
}
public static class Linux extends FlutterBuildAction {
@Override
protected @NotNull BuildType buildType() {
return BuildType.LINUX;
}
@Override
public void update(@NotNull AnActionEvent event) {
final Presentation presentation = event.getPresentation();
presentation.setEnabled(SystemInfo.isLinux);
}
}
public static class Macos extends FlutterBuildAction {
@Override
protected @NotNull BuildType buildType() {
return BuildType.MACOS;
}
@Override
public void update(@NotNull AnActionEvent event) {
final Presentation presentation = event.getPresentation();
presentation.setEnabled(SystemInfo.isMac);
}
}
public static class Web extends FlutterBuildAction {
@Override
protected @NotNull BuildType buildType() {
return BuildType.WEB;
}
}
public static class Windows extends FlutterBuildAction {
@Override
protected @NotNull BuildType buildType() {
return BuildType.WINDOWS;
}
@Override
public void update(@NotNull AnActionEvent event) {
final Presentation presentation = event.getPresentation();
presentation.setEnabled(SystemInfo.isWindows);
}
}
}