downloadIfNeeded static method
Ensures the native library is available before NativeLibrary is used.
Returns true immediately when any of the resolution paths (env var,
resolver seam, sibling adapter, cache, system loader) already provide
the library. Otherwise downloads the matching GitHub release asset into
~/.html_to_markdown_ffi/. (The legacy native/ package bundle was
retired — issue #6: the SPM xcframework is the only macOS delivery
surface — so the download fallback is what a plain-Dart macOS host
ultimately relies on.)
Implementation
static Future<bool> downloadIfNeeded() async {
final libName = _platformLibraryName();
final target = _platformTarget();
final home = Platform.environment['HOME'] ?? '/tmp';
final cachePath = '$home/.html_to_markdown_ffi/$libName';
if (File(cachePath).existsSync()) return true;
final envPath = Platform.environment['HTML_TO_MARKDOWN_FFI_LIB_PATH'];
if (envPath != null && File(envPath).existsSync()) return true;
final repoRoot = _findRepoRoot();
if (repoRoot != null && File('${repoRoot.path}/target/release/$libName').existsSync()) return true;
try {
DynamicLibrary.open(libName);
return true;
} catch (_) {}
final version = Platform.environment['HTML_TO_MARKDOWN_FFI_VERSION'] ?? _defaultVersion;
final ext = Platform.isWindows ? 'dll' : libName.split('.').last;
final url = 'https://github.com/$_repo/releases/download/v$version/libhtml_to_markdown_ffi-$target.$ext';
try {
final r = await http.get(Uri.parse(url));
if (r.statusCode == 200) {
await Directory('$home/.html_to_markdown_ffi').create(recursive: true);
await File(cachePath).writeAsBytes(r.bodyBytes);
return true;
}
} catch (_) {}
return false;
}