Skip to content

Commit acad411

Browse files
authored
Run flutter pub get after changing SDK in preferences (#5773)
1 parent cf03c4a commit acad411

File tree

1 file changed

+79
-11
lines changed

1 file changed

+79
-11
lines changed

src/io/flutter/sdk/FlutterSettingsConfigurable.java

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import io.flutter.bazel.Workspace;
3636
import io.flutter.bazel.WorkspaceCache;
3737
import io.flutter.font.FontPreviewProcessor;
38+
import io.flutter.pub.PubRoot;
39+
import io.flutter.pub.PubRoots;
3840
import io.flutter.settings.FlutterSettings;
3941
import org.jetbrains.annotations.Nls;
4042
import org.jetbrains.annotations.NotNull;
@@ -46,6 +48,8 @@
4648
import java.awt.datatransfer.StringSelection;
4749
import java.net.URI;
4850
import java.net.URISyntaxException;
51+
import java.util.List;
52+
import java.util.concurrent.Semaphore;
4953

5054
// Note: when updating the settings here, update FlutterSearchableOptionContributor as well.
5155

@@ -85,6 +89,13 @@ public class FlutterSettingsConfigurable implements SearchableConfigurable {
8589
private boolean ignoringSdkChanges = false;
8690

8791
private String fullVersionString;
92+
private FlutterSdkVersion previousSdkVersion;
93+
94+
/**
95+
* Semaphore used to synchronize flutter commands so we don't try to do two at once.
96+
*/
97+
private final Semaphore lock = new Semaphore(1, true);
98+
private Process updater;
8899

89100
FlutterSettingsConfigurable(@NotNull Project project) {
90101
this.myProject = project;
@@ -95,6 +106,10 @@ public class FlutterSettingsConfigurable implements SearchableConfigurable {
95106
}
96107

97108
private void init() {
109+
final FlutterSdk sdk = FlutterSdk.getFlutterSdk(myProject);
110+
if (sdk != null) {
111+
previousSdkVersion = sdk.getVersion();
112+
}
98113
mySdkCombo.getComboBox().setEditable(true);
99114

100115
myCopyButton.setSize(ActionToolbar.DEFAULT_MINIMUM_BUTTON_SIZE);
@@ -264,13 +279,22 @@ public void apply() throws ConfigurationException {
264279

265280
final String sdkHomePath = getSdkPathText();
266281
if (FlutterSdkUtil.isFlutterSdkHome(sdkHomePath)) {
282+
267283
ApplicationManager.getApplication().runWriteAction(() -> {
268284
FlutterSdkUtil.setFlutterSdkPath(myProject, sdkHomePath);
269285
FlutterSdkUtil.enableDartSdk(myProject);
286+
270287
ApplicationManager.getApplication().executeOnPooledThread(() -> {
271288
final FlutterSdk sdk = FlutterSdk.forPath(sdkHomePath);
272289
if (sdk != null) {
273-
sdk.queryFlutterChannel(false);
290+
try {
291+
lock.acquire();
292+
sdk.queryFlutterChannel(false);
293+
lock.release();
294+
}
295+
catch (InterruptedException e) {
296+
// do nothing
297+
}
274298
}
275299
});
276300
});
@@ -320,6 +344,27 @@ public void reset() {
320344
}
321345

322346
onVersionChanged();
347+
if (sdk != null) {
348+
if (previousSdkVersion != null) {
349+
if (previousSdkVersion.compareTo(sdk.getVersion()) != 0) {
350+
final List<PubRoot> roots = PubRoots.forProject(myProject);
351+
try {
352+
lock.acquire();
353+
for (PubRoot root : roots) {
354+
sdk.startPubGet(root, myProject);
355+
}
356+
lock.release();
357+
}
358+
catch (InterruptedException e) {
359+
// do nothing
360+
}
361+
previousSdkVersion = sdk.getVersion();
362+
}
363+
}
364+
}
365+
else {
366+
previousSdkVersion = null;
367+
}
323368

324369
myReportUsageInformationCheckBox.setSelected(FlutterInitializer.getCanReportAnalytics());
325370

@@ -375,17 +420,40 @@ private void onVersionChanged() {
375420
return;
376421
}
377422

423+
// Moved launching the version updater to a background thread to avoid deadlock
424+
// when the semaphone was locked for a long time on the EDT.
378425
final ModalityState modalityState = ModalityState.current();
379-
380-
// TODO(devoncarew): Switch this to expecting json output.
381-
sdk.flutterVersion().start((ProcessOutput output) -> {
382-
final String fullVersionText = output.getStdout();
383-
fullVersionString = fullVersionText;
384-
385-
final String[] lines = StringUtil.splitByLines(fullVersionText);
386-
final String singleLineVersion = lines.length > 0 ? lines[0] : "";
387-
ApplicationManager.getApplication().invokeLater(() -> updateVersionTextIfCurrent(sdk, singleLineVersion), modalityState);
388-
}, null);
426+
ApplicationManager.getApplication().executeOnPooledThread(() -> {
427+
try {
428+
if (updater != null) {
429+
// If we get back here before the previous one finished then just kill it.
430+
// This isn't perfect, but does help avoid printing this message most times:
431+
// Waiting for another flutter command to release the startup lock...
432+
updater.destroy();
433+
lock.release();
434+
}
435+
Thread.sleep(100L);
436+
lock.acquire();
437+
438+
ApplicationManager.getApplication().invokeLater(() -> {
439+
// "flutter --version" can take a long time on a slow network.
440+
updater = sdk.flutterVersion().start((ProcessOutput output) -> {
441+
fullVersionString = output.getStdout();
442+
final String[] lines = StringUtil.splitByLines(fullVersionString);
443+
final String singleLineVersion = lines.length > 0 ? lines[0] : "";
444+
445+
ApplicationManager.getApplication().invokeLater(() -> {
446+
updater = null;
447+
lock.release();
448+
updateVersionTextIfCurrent(sdk, singleLineVersion);
449+
}, modalityState);
450+
}, null);
451+
}, modalityState);
452+
}
453+
catch (InterruptedException e) {
454+
// do nothing
455+
}
456+
});
389457
}
390458

391459
/***

0 commit comments

Comments
 (0)