40
40
import javax .swing .*;
41
41
import java .io .File ;
42
42
import java .io .IOException ;
43
- import java .util .Arrays ;
44
- import java .util .HashSet ;
45
- import java .util .LinkedHashSet ;
46
- import java .util .Set ;
43
+ import java .net .URI ;
44
+ import java .net .URISyntaxException ;
45
+ import java .util .*;
47
46
48
47
public class FlutterSdkUtil {
49
48
/**
@@ -260,40 +259,19 @@ public static void enableDartSdk(@NotNull final Project project) {
260
259
@ Nullable
261
260
public static String guessFlutterSdkFromPackagesFile (@ NotNull Module module ) {
262
261
// First, look for .dart_tool/package_config.json
263
- for (PubRoot pubRoot : PubRoots .forModule (module )) {
264
- final VirtualFile configFile = pubRoot .getPackageConfigFile ();
265
- if (configFile == null ) {
266
- continue ;
267
- }
268
- // parse it
269
- try {
270
- final String contents = new String (configFile .contentsToByteArray (true /* cache contents */ ));
271
- final JsonElement element = new JsonParser ().parse (contents );
272
- if (element == null ) {
262
+ final JsonArray packages = getPackagesFromPackageConfig (PubRoots .forModule (module ));
263
+ for (int i = 0 ; i < packages .size (); i ++) {
264
+ final JsonObject pack = packages .get (i ).getAsJsonObject ();
265
+ if ("flutter" .equals (JsonUtils .getStringMember (pack , "name" ))) {
266
+ final String uri = JsonUtils .getStringMember (pack , "rootUri" );
267
+ if (uri == null ) {
273
268
continue ;
274
269
}
275
- final JsonObject json = element .getAsJsonObject ();
276
- if (JsonUtils .getIntMember (json , "configVersion" ) < 2 ) continue ;
277
- final JsonArray packages = json .getAsJsonArray ("packages" );
278
- if (packages == null || packages .size () == 0 ) {
270
+ final String path = extractSdkPathFromUri (uri , false );
271
+ if (path == null ) {
279
272
continue ;
280
273
}
281
- for (int i = 0 ; i < packages .size (); i ++) {
282
- final JsonObject pack = packages .get (i ).getAsJsonObject ();
283
- if ("flutter" .equals (JsonUtils .getStringMember (pack , "name" ))) {
284
- final String uri = JsonUtils .getStringMember (pack , "rootUri" );
285
- if (uri == null ) {
286
- continue ;
287
- }
288
- final String path = extractSdkPathFromUri (uri , false );
289
- if (path == null ) {
290
- continue ;
291
- }
292
- return path ;
293
- }
294
- }
295
- }
296
- catch (IOException ignored ) {
274
+ return path ;
297
275
}
298
276
}
299
277
@@ -315,6 +293,54 @@ public static String guessFlutterSdkFromPackagesFile(@NotNull Module module) {
315
293
return null ;
316
294
}
317
295
296
+ @ Nullable
297
+ public static String getPathToCupertinoIconsPackage (@ NotNull Project project ) {
298
+ final JsonArray packages = getPackagesFromPackageConfig (PubRoots .forProject (project ));
299
+ for (int i = 0 ; i < packages .size (); i ++) {
300
+ final JsonObject pack = packages .get (i ).getAsJsonObject ();
301
+ if ("cupertino_icons" .equals (JsonUtils .getStringMember (pack , "name" ))) {
302
+ final String uri = JsonUtils .getStringMember (pack , "rootUri" );
303
+ if (uri == null ) {
304
+ continue ;
305
+ }
306
+ try {
307
+ return new URI (uri ).getPath ();
308
+ }
309
+ catch (URISyntaxException ignored ) {
310
+ }
311
+ }
312
+ }
313
+ return null ;
314
+ }
315
+
316
+ private static JsonArray getPackagesFromPackageConfig (@ NotNull List <PubRoot > pubRoots ) {
317
+ final JsonArray entries = new JsonArray ();
318
+ for (PubRoot pubRoot : pubRoots ) {
319
+ final VirtualFile configFile = pubRoot .getPackageConfigFile ();
320
+ if (configFile == null ) {
321
+ continue ;
322
+ }
323
+ // parse it
324
+ try {
325
+ final String contents = new String (configFile .contentsToByteArray (true /* cache contents */ ));
326
+ final JsonElement element = new JsonParser ().parse (contents );
327
+ if (element == null ) {
328
+ continue ;
329
+ }
330
+ final JsonObject json = element .getAsJsonObject ();
331
+ if (JsonUtils .getIntMember (json , "configVersion" ) < 2 ) continue ;
332
+ final JsonArray packages = json .getAsJsonArray ("packages" );
333
+ if (packages == null || packages .size () == 0 ) {
334
+ continue ;
335
+ }
336
+ entries .addAll (packages );
337
+ }
338
+ catch (IOException ignored ) {
339
+ }
340
+ }
341
+ return entries ;
342
+ }
343
+
318
344
@ VisibleForTesting
319
345
public static String parseFlutterSdkPath (String packagesFileContent ) {
320
346
for (String line : packagesFileContent .split ("\n " )) {
0 commit comments