mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-04 20:29:08 -04:00
refactor(setup): simplify setup for flutter applications (#12)
This commit is contained in:
parent
c8895524be
commit
678f6208f6
9 changed files with 119 additions and 139 deletions
|
@ -4,14 +4,12 @@ import 'dart:ffi';
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:cli_util/cli_logging.dart' show Ansi, Logger;
|
||||
import 'package:ffi/ffi.dart';
|
||||
import 'package:libgit2dart/src/bindings/libgit2_bindings.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:pub_cache/pub_cache.dart';
|
||||
|
||||
const tag = 'libs-v1.3.0';
|
||||
const libUrl =
|
||||
'https://github.com/SkinnyMind/libgit2dart/releases/download/$tag/';
|
||||
const libgit2Version = '1.3.0';
|
||||
const libDir = '.dart_tool/libgit2/';
|
||||
final libDir = path.join('.dart_tool', 'libgit2');
|
||||
|
||||
String getLibName() {
|
||||
var ext = 'so';
|
||||
|
@ -20,7 +18,7 @@ String getLibName() {
|
|||
ext = 'dll';
|
||||
} else if (Platform.isMacOS) {
|
||||
ext = 'dylib';
|
||||
} else if (!(Platform.isLinux || Platform.isAndroid)) {
|
||||
} else if (!(Platform.isLinux)) {
|
||||
throw Exception('Unsupported platform.');
|
||||
}
|
||||
|
||||
|
@ -28,23 +26,43 @@ String getLibName() {
|
|||
}
|
||||
|
||||
/// Checks if [File]/[Link] exists for an [uri].
|
||||
bool _doesFileExist(Uri uri) {
|
||||
return File.fromUri(uri).existsSync() || Link.fromUri(uri).existsSync();
|
||||
bool _doesFileExist(String path) {
|
||||
return File(path).existsSync() || Link(path).existsSync();
|
||||
}
|
||||
|
||||
String? _resolveLibUri(String name) {
|
||||
var libUri = Directory.current.uri.resolve(name);
|
||||
final dartToolDir = '$libDir${Platform.operatingSystem}';
|
||||
/// Returns path to dynamic library if found.
|
||||
String? _resolveLibPath(String name) {
|
||||
var libPath = path.join(Directory.current.path, name);
|
||||
|
||||
// If lib is in Present Working Directory.
|
||||
if (_doesFileExist(libUri)) {
|
||||
return libUri.toFilePath(windows: Platform.isWindows);
|
||||
if (_doesFileExist(libPath)) {
|
||||
return libPath;
|
||||
}
|
||||
|
||||
// If lib is in Present Working Directory's .dart_tool folder.
|
||||
libUri = Directory.current.uri.resolve('$dartToolDir/$name');
|
||||
if (_doesFileExist(libUri)) {
|
||||
return libUri.toFilePath(windows: Platform.isWindows);
|
||||
// If lib is in Present Working Directory's '.dart_tool/libgit2/[platform]' folder.
|
||||
libPath = path.join(
|
||||
Directory.current.path,
|
||||
libDir,
|
||||
Platform.operatingSystem,
|
||||
name,
|
||||
);
|
||||
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 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;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -53,28 +71,15 @@ String? _resolveLibUri(String name) {
|
|||
DynamicLibrary loadLibrary(String name) {
|
||||
try {
|
||||
return DynamicLibrary.open(
|
||||
_resolveLibUri(name) ?? name,
|
||||
_resolveLibPath(name) ?? name,
|
||||
);
|
||||
} catch (e) {
|
||||
final logger = Logger.standard();
|
||||
final ansi = Ansi(Ansi.terminalSupportsAnsi);
|
||||
|
||||
logger.stderr(
|
||||
'${ansi.red}Failed to open the library. Make sure that required '
|
||||
'library is in place.${ansi.none}',
|
||||
);
|
||||
logger.stdout(
|
||||
'To download the library, please run the following command from the '
|
||||
'root of your project:',
|
||||
);
|
||||
logger.stdout(
|
||||
'${ansi.yellow}dart run libgit2dart:setup${ansi.none} for '
|
||||
'dart application',
|
||||
);
|
||||
logger.stdout(ansi.none);
|
||||
logger.stdout(
|
||||
'${ansi.yellow}flutter pub run libgit2dart:setup${ansi.none} for '
|
||||
'flutter application',
|
||||
'${ansi.red}Failed to open the library. Make sure that libgit2 '
|
||||
'library is bundled with the application.${ansi.none}',
|
||||
);
|
||||
logger.stdout(ansi.none);
|
||||
rethrow;
|
||||
|
@ -83,23 +88,6 @@ DynamicLibrary loadLibrary(String name) {
|
|||
|
||||
final libgit2 = Libgit2(loadLibrary(getLibName()));
|
||||
|
||||
String getVersionNumber() {
|
||||
libgit2.git_libgit2_init();
|
||||
|
||||
final major = calloc<Int32>();
|
||||
final minor = calloc<Int32>();
|
||||
final rev = calloc<Int32>();
|
||||
libgit2.git_libgit2_version(major, minor, rev);
|
||||
|
||||
final version = '${major.value}.${minor.value}.${rev.value}';
|
||||
|
||||
calloc.free(major);
|
||||
calloc.free(minor);
|
||||
calloc.free(rev);
|
||||
|
||||
return version;
|
||||
}
|
||||
|
||||
bool isValidShaHex(String str) {
|
||||
final hexRegExp = RegExp(r'^[0-9a-fA-F]+$');
|
||||
return hexRegExp.hasMatch(str) &&
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue