6
6
package io .flutter .sdk ;
7
7
8
8
import com .google .common .annotations .VisibleForTesting ;
9
+ import com .google .common .reflect .TypeToken ;
10
+ import com .google .gson .Gson ;
9
11
import com .intellij .openapi .util .Version ;
10
12
import com .intellij .openapi .vfs .VirtualFile ;
11
13
import org .jetbrains .annotations .NotNull ;
12
14
import org .jetbrains .annotations .Nullable ;
13
15
14
16
import java .io .IOException ;
15
17
import java .nio .charset .StandardCharsets ;
18
+ import java .util .Map ;
16
19
import java .util .Objects ;
17
20
18
21
public final class FlutterSdkVersion implements Comparable <FlutterSdkVersion > {
@@ -94,6 +97,14 @@ public FlutterSdkVersion(@Nullable String versionString) {
94
97
95
98
@ NotNull
96
99
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
97
108
return readFromFile (sdkHome .findChild ("version" ));
98
109
}
99
110
@@ -109,14 +120,33 @@ private static FlutterSdkVersion readFromFile(@Nullable VirtualFile file) {
109
120
private static String readVersionString (@ NotNull VirtualFile file ) {
110
121
try {
111
122
final String data = new String (file .contentsToByteArray (), StandardCharsets .UTF_8 );
112
- for (String line : data .split ("\n " )) {
113
- line = line .trim ();
114
123
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 ;
117
149
}
118
-
119
- return line ;
120
150
}
121
151
return null ;
122
152
}
@@ -145,7 +175,9 @@ public boolean canUseDevToolsMultiEmbed() {
145
175
return supportsVersion (MIN_SUPPORTS_DEVTOOLS_MULTI_EMBED );
146
176
}
147
177
148
- /** @noinspection BooleanMethodIsAlwaysInverted*/
178
+ /**
179
+ * @noinspection BooleanMethodIsAlwaysInverted
180
+ */
149
181
public boolean canUseDtd () {
150
182
return supportsVersion (MIN_SUPPORTS_DTD );
151
183
}
0 commit comments