resolveNativeSource function

Future<String> resolveNativeSource(
  1. BuildInput input,
  2. BuildOutputBuilder output,
  3. String triple
)

The root of an unpacked archive for triple: a local directory (see the library comment), else the release's, downloaded and verified.

Implementation

Future<String> resolveNativeSource(BuildInput input, BuildOutputBuilder output, String triple) async {
  final local = input.userDefines.path('native_dir')?.toFilePath() ?? _nonEmpty(Platform.environment['EMON_NATIVE_DIR']);
  final shared = Directory.fromUri(input.outputDirectoryShared.resolve('emon-native/'))..createSync(recursive: true);
  if (local != null) {
    final source = await _localSource(local, triple);
    if (source.isDir) {
      output.dependencies.add(Directory(source.path).uri);
      return source.path;
    }
    output.dependencies.add(File(source.path).uri); // a rebuilt archive runs the hook again
    return _unpack(File(source.path), shared, 'local-$triple');
  }
  final entry = kNativeAssets.archives[triple];
  if (entry == null) {
    throw StateError(
      'emon has no verified prebuilt library for $triple in this package (version ${kNativeAssets.version}): a build from a checkout names a local '
      'directory of libraries: hooks: user_defines: <package>: native_dir: <dir with emon-c-*-$triple.tar.gz> in pubspec.yaml, or EMON_NATIVE_DIR.',
    );
  }
  final archive = File(p.join(shared.path, entry.file));
  if (!archive.existsSync() || _sha256(archive) != entry.sha256) {
    final bytes = await _download(kNativeAssets.urlFor(entry.file));
    final got = sha256.convert(bytes).toString();
    if (got != entry.sha256) {
      throw StateError('${entry.file} downloaded from ${kNativeAssets.urlFor(entry.file)} does not match the SHA-256 this package pins ($got, not ${entry.sha256}): refusing to use it');
    }
    archive.writeAsBytesSync(bytes);
  }
  return _unpack(archive, shared, entry.sha256.substring(0, 16));
}