Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bindings for dart #136

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Prev Previous commit
refactor: dont search parents for native library
Mensch committed Mar 10, 2023
commit 5000d3430514476ef553424e73903b22bdb8305e
52 changes: 23 additions & 29 deletions dart/lib/src/locator.dart
Original file line number Diff line number Diff line change
@@ -5,33 +5,14 @@ import 'dart:io';
///
/// Throws [MinifyHtmlLocatorError] if the dynamic library could not be found.
DynamicLibrary loadDynamicLibrary() {
String locate(String libName) {
final path = _packageRootUri(Platform.script.resolve('./'), libName) ??
_packageRootUri(Directory.current.uri, libName);

if (path != null) {
return path;
}

final toolLib = Directory.current.uri
.resolve("$_minifyhtmlToolDir$libName")
.toFilePath();

if (FileSystemEntity.isFileSync(toolLib)) {
return toolLib;
}

throw MinifyHtmlLocatorError('MinifyHtml library not found');
}

if (Platform.isIOS) {
return DynamicLibrary.process();
} else if (Platform.isMacOS) {
return DynamicLibrary.open(locate(appleLib));
return _locateOrError(appleLib);
} else if (Platform.isWindows) {
return DynamicLibrary.open(locate(windowsLib));
return _locate(windowsLib) ?? DynamicLibrary.executable();
} else if (Platform.isLinux) {
return DynamicLibrary.open(locate(linuxLib));
return _locateOrError(linuxLib);
} else if (Platform.isAndroid) {
return DynamicLibrary.open(linuxLib);
} else if (Platform.isFuchsia) {
@@ -71,12 +52,25 @@ const windowsLib = 'minifyhtml.dll';

const _minifyhtmlToolDir = '.dart_tool/minifyhtml/';

String? _packageRootUri(Uri root, String libName) {
do {
final filePath = root.resolve(libName).toFilePath();
if (FileSystemEntity.isFileSync(filePath)) {
return filePath;
}
} while (root != (root = root.resolve('..')));
DynamicLibrary? _locate(String libName) {
if (FileSystemEntity.isFileSync(libName)) {
return DynamicLibrary.open(libName);
}

final toolLib =
Directory.current.uri.resolve("$_minifyhtmlToolDir$libName").toFilePath();
if (FileSystemEntity.isFileSync(toolLib)) {
return DynamicLibrary.open(toolLib);
}

return null;
}

DynamicLibrary _locateOrError(String libName) {
final value = _locate(libName);
if (value != null) {
return value;
} else {
throw MinifyHtmlLocatorError('MinifyHtml library not found');
}
}