From 12ee9f3b53b095dbf3620c797d20ac02f6947eda Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Thu, 17 Feb 2022 19:43:11 +0300 Subject: [PATCH] fix: lookup library in Dart's and Flutter's `.pub-cache` --- lib/src/util.dart | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/src/util.dart b/lib/src/util.dart index b63d4a4..ef1910f 100644 --- a/lib/src/util.dart +++ b/lib/src/util.dart @@ -35,9 +35,7 @@ String? _resolveLibPath(String name) { var libPath = path.join(Directory.current.path, name); // If lib is in Present Working Directory. - if (_doesFileExist(libPath)) { - return libPath; - } + if (_doesFileExist(libPath)) return libPath; // If lib is in Present Working Directory's '.dart_tool/libgit2/[platform]' folder. libPath = path.join( @@ -46,23 +44,29 @@ String? _resolveLibPath(String name) { Platform.operatingSystem, name, ); - if (_doesFileExist(libPath)) { - return libPath; - } + if (_doesFileExist(libPath)) return libPath; // If lib is in Present Working Directory's '[platform]' folder. libPath = path.join(Directory.current.path, Platform.operatingSystem, name); - if (_doesFileExist(libPath)) { - return libPath; + if (_doesFileExist(libPath)) return libPath; + + String checkCache(PubCache pubCache) { + final pubCacheDir = + pubCache.getLatestVersion('libgit2dart')!.resolve()!.location; + return path.join(pubCacheDir.path, Platform.operatingSystem, name); } - // If lib is in '.pub_cache' folder. - final pubCache = PubCache(); - final pubCacheDir = - pubCache.getLatestVersion('libgit2dart')!.resolve()!.location; - libPath = path.join(pubCacheDir.path, Platform.operatingSystem, name); - if (_doesFileExist(libPath)) { - return libPath; + // If lib is in Dart's '.pub_cache' folder. + libPath = checkCache(PubCache()); + if (_doesFileExist(libPath)) return libPath; + + // If lib is in Flutter's '.pub_cache' folder. + final env = Platform.environment; + if (env.containsKey('FLUTTER_ROOT')) { + final flutterPubCache = + PubCache(Directory(path.join(env['FLUTTER_ROOT']!, '.pub-cache'))); + libPath = checkCache(flutterPubCache); + if (_doesFileExist(libPath)) return libPath; } return null;