forked from flutter/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlutterSdkAction.java
More file actions
103 lines (90 loc) · 3.71 KB
/
FlutterSdkAction.java
File metadata and controls
103 lines (90 loc) · 3.71 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
/*
* 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.actions;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import io.flutter.FlutterBundle;
import io.flutter.FlutterMessages;
import io.flutter.FlutterUtils;
import io.flutter.analytics.Analytics;
import io.flutter.analytics.AnalyticsData;
import io.flutter.bazel.Workspace;
import io.flutter.pub.PubRoot;
import io.flutter.pub.PubRoots;
import io.flutter.sdk.FlutterSdk;
import io.flutter.utils.FlutterModuleUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
/**
* Base class for Flutter commands.
*/
public abstract class FlutterSdkAction extends DumbAwareAction {
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
final Project project = DumbAwareAction.getEventProject(event);
AnalyticsData analyticsData = AnalyticsData.forAction(this, event);
if (enableActionInBazelContext()) {
// See if the Bazel workspace exists for this project.
final Workspace workspace = FlutterModuleUtils.getFlutterBazelWorkspace(project);
if (workspace != null) {
FileDocumentManager.getInstance().saveAllDocuments();
startCommandInBazelContext(project, workspace, event);
analyticsData.add("inBazelContext", true);
Analytics.report(analyticsData);
return;
}
}
final FlutterSdk sdk = project != null ? FlutterSdk.getFlutterSdk(project) : null;
if (sdk == null) {
showMissingSdkDialog(project);
analyticsData.add("missingSdk", true);
Analytics.report(analyticsData);
return;
}
FileDocumentManager.getInstance().saveAllDocuments();
PubRoot root = PubRoot.forEventWithRefresh(event);
@NotNull DataContext context = event.getDataContext();
if (root != null) {
startCommand(project, sdk, root, context);
}
else {
List<PubRoot> roots = PubRoots.forProject(project);
for (PubRoot sub : roots) {
startCommand(project, sdk, sub, context);
}
}
Analytics.report(analyticsData);
}
public abstract void startCommand(@NotNull Project project,
@NotNull FlutterSdk sdk,
@Nullable PubRoot root,
@NotNull DataContext context);
/**
* Implemented by actions which are used in the Bazel context ({@link #enableActionInBazelContext()} returns true), by default this method
* throws an {@link Error}.
*/
public void startCommandInBazelContext(@NotNull Project project, @NotNull Workspace workspace, @NotNull AnActionEvent event) {
throw new Error("This method should not be called directly, but should be overridden.");
}
/**
* By default, this method returns false. For actions which can be used in the Bazel context this method should return true.
*/
public boolean enableActionInBazelContext() {
return false;
}
public static void showMissingSdkDialog(@Nullable Project project) {
final int response = FlutterMessages.showDialog(project, FlutterBundle.message("flutter.sdk.notAvailable.message"),
FlutterBundle.message("flutter.sdk.notAvailable.title"),
new String[]{"Yes, configure", "No, thanks"}, -1);
if (response == 0) {
FlutterUtils.openFlutterSettings(project);
}
}
}