66package io .flutter .sdk ;
77
88import com .google .common .annotations .VisibleForTesting ;
9+ import com .google .common .reflect .TypeToken ;
10+ import com .google .gson .Gson ;
911import com .intellij .openapi .util .Version ;
1012import com .intellij .openapi .vfs .VirtualFile ;
1113import org .jetbrains .annotations .NotNull ;
1214import org .jetbrains .annotations .Nullable ;
1315
1416import java .io .IOException ;
1517import java .nio .charset .StandardCharsets ;
18+ import java .util .Map ;
1619import java .util .Objects ;
1720
1821public 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