fix: lookup library in correct locations

This commit is contained in:
Aleksey Kulikov 2022-06-08 15:12:42 +03:00
parent 5739de545b
commit 7aadefc7b5
3 changed files with 21 additions and 32 deletions

View file

@ -49,13 +49,13 @@ print(Libgit2.version);
**Note**: The following steps only required if you are using package in Dart application (Flutter application will have libgit2 library bundled automatically when you build for release). **Note**: The following steps only required if you are using package in Dart application (Flutter application will have libgit2 library bundled automatically when you build for release).
After compiling the application you should run: After adding the package as dependency you should run:
```shell ```shell
dart run libgit2dart:setup dart run libgit2dart:setup
``` ```
That'll copy the prebuilt libgit2 library for your platform into `.dart_tool/libgit2/<platform>/` which you'll need to add to the same folder as your executable. That'll copy the prebuilt libgit2 library for your platform into `.dart_tool/libgit2/<platform>/` which you'll need to add to the same folder as your executable after compilation.
If you upgrade the version of libgit2dart package in your dependencies you should run the following commands to have the latest libgit2 library for your platform to provide with your application: If you upgrade the version of libgit2dart package in your dependencies you should run the following commands to have the latest libgit2 library for your platform to provide with your application:

View file

@ -23,15 +23,23 @@ Future<void> copyLibrary(String platform) async {
); );
} }
} else { } else {
final pubCache = PubCache(); String? checkCache(PubCache pubCache) =>
final pubCacheDir = pubCache.getLatestVersion('libgit2dart')?.resolve()?.location.path;
pubCache.getLatestVersion('libgit2dart')!.resolve()!.location;
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); final destination = path.join(libDir, platform);
Directory(destination).createSync(recursive: true); Directory(destination).createSync(recursive: true);
File(path.join(pubCacheDir.path, platform, libName)).copySync( File(path.join(libPath!, platform, libName)).copySync(
path.join(destination, libName), path.join(destination, libName),
); );

View file

@ -6,7 +6,6 @@ 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';
const libgit2Version = '1.4.3'; const libgit2Version = '1.4.3';
final libDir = path.join('.dart_tool', 'libgit2'); final libDir = path.join('.dart_tool', 'libgit2');
@ -32,13 +31,8 @@ bool _doesFileExist(String path) {
/// Returns path to dynamic library if found. /// Returns path to dynamic library if found.
String? _resolveLibPath(String name) { 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 lib is in Present Working Directory's '.dart_tool/libgit2/[platform]' folder. // If lib is in Present Working Directory's '.dart_tool/libgit2/[platform]' folder.
libPath = path.join( var libPath = path.join(
Directory.current.path, Directory.current.path,
libDir, libDir,
Platform.operatingSystem, Platform.operatingSystem,
@ -50,33 +44,20 @@ String? _resolveLibPath(String name) {
libPath = path.join(Directory.current.path, Platform.operatingSystem, name); libPath = path.join(Directory.current.path, Platform.operatingSystem, name);
if (_doesFileExist(libPath)) return libPath; if (_doesFileExist(libPath)) return libPath;
String checkCache(PubCache pubCache) { // If lib is in executable's folder.
final pubCacheDir = libPath = path.join(path.dirname(Platform.resolvedExecutable), name);
pubCache.getLatestVersion('libgit2dart')!.resolve()!.location;
return path.join(pubCacheDir.path, Platform.operatingSystem, name);
}
// If lib is in Dart's '.pub_cache' folder.
libPath = checkCache(PubCache());
if (_doesFileExist(libPath)) return libPath; if (_doesFileExist(libPath)) return libPath;
// If lib is in Flutter's '.pub_cache' folder. // If lib is in executable's bundled 'lib' folder.
final env = Platform.environment; libPath = path.join(path.dirname(Platform.resolvedExecutable), 'lib', name);
if (env.containsKey('FLUTTER_ROOT')) { if (_doesFileExist(libPath)) return libPath;
final flutterPubCache =
PubCache(Directory(path.join(env['FLUTTER_ROOT']!, '.pub-cache')));
libPath = checkCache(flutterPubCache);
if (_doesFileExist(libPath)) return libPath;
}
return null; return null;
} }
DynamicLibrary loadLibrary(String name) { DynamicLibrary loadLibrary(String name) {
try { try {
return DynamicLibrary.open( return DynamicLibrary.open(_resolveLibPath(name) ?? name);
_resolveLibPath(name) ?? name,
);
} catch (e) { } catch (e) {
stderr.writeln( stderr.writeln(
'Failed to open the library. Make sure that libgit2 library is bundled ' 'Failed to open the library. Make sure that libgit2 library is bundled '