mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-04 12:19:09 -04:00
fix: lookup package in correct location of Dart/Flutter cached packages (#81)
This commit is contained in:
parent
675872c3ef
commit
ed67a4e307
3 changed files with 38 additions and 31 deletions
|
@ -5,7 +5,6 @@ import 'package:libgit2dart/libgit2dart.dart';
|
||||||
import 'package:libgit2dart/src/libgit2.dart';
|
import 'package:libgit2dart/src/libgit2.dart';
|
||||||
import 'package:libgit2dart/src/util.dart';
|
import 'package:libgit2dart/src/util.dart';
|
||||||
import 'package:path/path.dart' as path;
|
import 'package:path/path.dart' as path;
|
||||||
import 'package:pub_cache/pub_cache.dart';
|
|
||||||
|
|
||||||
/// Copies prebuilt libgit2 library from package in '.pub_cache' into correct
|
/// Copies prebuilt libgit2 library from package in '.pub_cache' into correct
|
||||||
/// directory for [platform].
|
/// directory for [platform].
|
||||||
|
@ -23,27 +22,24 @@ Future<void> copyLibrary(String platform) async {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
String? checkCache(PubCache pubCache) =>
|
final libPath = checkCache();
|
||||||
pubCache.getLatestVersion('libgit2dart')?.resolve()?.location.path;
|
|
||||||
|
|
||||||
final libPath = checkCache(PubCache()) ??
|
|
||||||
checkCache(
|
|
||||||
PubCache(
|
|
||||||
Directory(
|
|
||||||
path.join(Platform.environment['FLUTTER_ROOT']!, '.pub-cache'),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
final libName = getLibName();
|
final libName = getLibName();
|
||||||
|
|
||||||
stdout.writeln('Copying libgit2 for $platform');
|
stdout.writeln('Copying libgit2 for $platform');
|
||||||
final destination = path.join(libDir, platform);
|
if (libPath == null) {
|
||||||
Directory(destination).createSync(recursive: true);
|
stdout.writeln(
|
||||||
File(path.join(libPath!, platform, libName)).copySync(
|
"Couldn't find libgit2dart package.\n"
|
||||||
path.join(destination, libName),
|
"Make sure to run 'dart pub get' to resolve dependencies.",
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
final destination = path.join(libDir, platform);
|
||||||
|
Directory(destination).createSync(recursive: true);
|
||||||
|
File(path.join(libPath, platform, libName)).copySync(
|
||||||
|
path.join(destination, libName),
|
||||||
|
);
|
||||||
|
|
||||||
stdout.writeln('Done! libgit2 for $platform is now available!');
|
stdout.writeln('Done! libgit2 for $platform is now available!');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,7 +53,9 @@ class CleanCommand extends Command<void> {
|
||||||
@override
|
@override
|
||||||
void run() {
|
void run() {
|
||||||
stdout.writeln('Cleaning...');
|
stdout.writeln('Cleaning...');
|
||||||
Directory(libDir).deleteSync(recursive: true);
|
if (Directory(libDir).existsSync()) {
|
||||||
|
Directory(libDir).deleteSync(recursive: true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
// coverage:ignore-file
|
// coverage:ignore-file
|
||||||
|
|
||||||
|
import 'dart:convert';
|
||||||
import 'dart:ffi';
|
import 'dart:ffi';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:libgit2dart/src/bindings/libgit2_bindings.dart';
|
import 'package:libgit2dart/src/bindings/libgit2_bindings.dart';
|
||||||
import 'package:libgit2dart/src/bindings/libgit2_opts_bindings.dart';
|
import 'package:libgit2dart/src/bindings/libgit2_opts_bindings.dart';
|
||||||
import 'package:path/path.dart' as path;
|
import 'package:path/path.dart' as path;
|
||||||
import 'package:pub_cache/pub_cache.dart';
|
import 'package:pub_semver/pub_semver.dart';
|
||||||
|
|
||||||
const libgit2Version = '1.5.0';
|
const libgit2Version = '1.5.0';
|
||||||
final libDir = path.join('.dart_tool', 'libgit2');
|
final libDir = path.join('.dart_tool', 'libgit2');
|
||||||
|
@ -25,6 +26,20 @@ String getLibName() {
|
||||||
return 'libgit2-$libgit2Version.$ext';
|
return 'libgit2-$libgit2Version.$ext';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns location of the most recent verison of the libgit2dart package
|
||||||
|
/// contained in the cache.
|
||||||
|
String? checkCache() {
|
||||||
|
final cache = json.decode(
|
||||||
|
Process.runSync('dart', ['pub', 'cache', 'list']).stdout as String,
|
||||||
|
) as Map<String, dynamic>;
|
||||||
|
final packages = cache['packages'] as Map<String, dynamic>;
|
||||||
|
final libPackages = packages['libgit2dart'] as Map<String, dynamic>?;
|
||||||
|
final versions = libPackages?.keys.map((e) => Version.parse(e)).toList();
|
||||||
|
final latestVersion = libPackages?[Version.primary(versions!).toString()]
|
||||||
|
as Map<String, dynamic>?;
|
||||||
|
return latestVersion?['location'] as String?;
|
||||||
|
}
|
||||||
|
|
||||||
/// Checks if [File]/[Link] exists for [path].
|
/// Checks if [File]/[Link] exists for [path].
|
||||||
bool _doesFileExist(String path) {
|
bool _doesFileExist(String path) {
|
||||||
return File(path).existsSync() || Link(path).existsSync();
|
return File(path).existsSync() || Link(path).existsSync();
|
||||||
|
@ -65,16 +80,10 @@ String? _resolveLibPath(String name) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String checkCache(PubCache pubCache) {
|
// If lib is in '.pub_cache' folder.
|
||||||
final pubCacheDir =
|
final cachedLocation = checkCache();
|
||||||
pubCache.getLatestVersion('libgit2dart')!.resolve()!.location;
|
if (cachedLocation != null) {
|
||||||
return path.join(pubCacheDir.path, Platform.operatingSystem, name);
|
libPath = path.join(cachedLocation, Platform.operatingSystem, name);
|
||||||
}
|
|
||||||
|
|
||||||
// If lib is in Flutter's '.pub_cache' folder.
|
|
||||||
final env = Platform.environment;
|
|
||||||
if (env.containsKey('FLUTTER_ROOT')) {
|
|
||||||
libPath = checkCache(PubCache());
|
|
||||||
if (_doesFileExist(libPath)) return libPath;
|
if (_doesFileExist(libPath)) return libPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ dependencies:
|
||||||
ffi: ^2.0.0
|
ffi: ^2.0.0
|
||||||
meta: ^1.7.0
|
meta: ^1.7.0
|
||||||
path: ^1.8.1
|
path: ^1.8.1
|
||||||
pub_cache: ^0.3.1
|
pub_semver: ^2.1.3
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
ffigen: ^6.0.1
|
ffigen: ^6.0.1
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue