Skip to content

Commit 65d5044

Browse files
authored
Update the file read of the Flutter version file to check for the new format (#8466)
This resolves #8465
1 parent 57cd907 commit 65d5044

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Removed
66

7+
- The Flutter version is now read from the file ./bin/cache/flutter.version.json, required in Flutter 3.33+ (#8465)
8+
79
### Changed
810

911
- Resolved a "Slow operations are prohibited on EDT" exception on Flutter Project creation (#8446, #8447, #8448)

src/io/flutter/sdk/FlutterSdkVersion.java

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
package io.flutter.sdk;
77

88
import com.google.common.annotations.VisibleForTesting;
9+
import com.google.common.reflect.TypeToken;
10+
import com.google.gson.Gson;
911
import com.intellij.openapi.util.Version;
1012
import com.intellij.openapi.vfs.VirtualFile;
1113
import org.jetbrains.annotations.NotNull;
1214
import org.jetbrains.annotations.Nullable;
1315

1416
import java.io.IOException;
1517
import java.nio.charset.StandardCharsets;
18+
import java.util.Map;
1619
import java.util.Objects;
1720

1821
public final class FlutterSdkVersion implements Comparable<FlutterSdkVersion> {
@@ -94,6 +97,14 @@ public FlutterSdkVersion(@Nullable String versionString) {
9497

9598
@NotNull
9699
public static FlutterSdkVersion readFromSdk(@NotNull VirtualFile sdkHome) {
100+
// First check for the file at <sdkHome>/bin/cache/flutter.version.json
101+
// https://github.com/flutter/flutter-intellij/issues/8465
102+
final VirtualFile versionFile = sdkHome.findFileByRelativePath("bin/cache/flutter.version.json");
103+
if (versionFile != null && versionFile.exists() && !versionFile.isDirectory()) {
104+
return readFromFile(versionFile);
105+
}
106+
107+
// Fallback to the old location at <sdkHome>/version
97108
return readFromFile(sdkHome.findChild("version"));
98109
}
99110

@@ -109,14 +120,33 @@ private static FlutterSdkVersion readFromFile(@Nullable VirtualFile file) {
109120
private static String readVersionString(@NotNull VirtualFile file) {
110121
try {
111122
final String data = new String(file.contentsToByteArray(), StandardCharsets.UTF_8);
112-
for (String line : data.split("\n")) {
113-
line = line.trim();
114123

115-
if (line.isEmpty() || line.startsWith("#")) {
116-
continue;
124+
// Check if the content is a JSON object.
125+
// This tests to see if it is of the format provided by <sdkHome>/bin/cache/flutter.version.json files
126+
// https://github.com/flutter/flutter-intellij/issues/8465
127+
if (data.trim().startsWith("{")) {
128+
try {
129+
final Map<String, Object> json = new Gson().fromJson(data, new TypeToken<Map<String, Object>>() {
130+
}.getType());
131+
if (json != null && json.containsKey("frameworkVersion")) {
132+
System.out.println("FlutterSdkVersion.readVersionString NEW " + json.get("frameworkVersion"));
133+
return (String)json.get("frameworkVersion");
134+
}
135+
}
136+
catch (com.google.gson.JsonSyntaxException e) {
137+
return null;
138+
}
139+
}
140+
else {
141+
// Otherwise, handle the old plain text format.
142+
for (String line : data.split("\n")) {
143+
line = line.trim();
144+
145+
if (line.isEmpty() || line.startsWith("#")) {
146+
continue;
147+
}
148+
return line;
117149
}
118-
119-
return line;
120150
}
121151
return null;
122152
}
@@ -145,7 +175,9 @@ public boolean canUseDevToolsMultiEmbed() {
145175
return supportsVersion(MIN_SUPPORTS_DEVTOOLS_MULTI_EMBED);
146176
}
147177

148-
/** @noinspection BooleanMethodIsAlwaysInverted*/
178+
/**
179+
* @noinspection BooleanMethodIsAlwaysInverted
180+
*/
149181
public boolean canUseDtd() {
150182
return supportsVersion(MIN_SUPPORTS_DTD);
151183
}

0 commit comments

Comments
 (0)