nativePrebuiltBuild function

Future<void> nativePrebuiltBuild(
  1. List<String> args, {
  2. NativeProject? project,
})

High-level convenience function for hook entrypoints.

This replaces the pattern of manually setting up logging, checking buildCodeAssets, and reading user-defined flags. It handles all of that internally so that a hook can be just a single call:

import 'package:native_prebuilt/hooks.dart';

Future<void> main(List<String> args) {
  return nativePrebuiltBuild(args);
}

The hook automatically:

  • Sets up Logger.root with Level.INFO output to stderr
  • Checks BuildConfig.buildCodeAssets and returns early if disabled
  • Reads the build_from_source user-defined flag from HookInput.userDefines to skip prebuilt resolution when needed

Implementation

Future<void> nativePrebuiltBuild(
  List<String> args, {
  NativeProject? project,
}) async {
  final buildProject = project ?? detect(Directory.current) ?? _defaultProject;
  await build(args, (input, output) async {
    Logger.root.level = Level.INFO;
    await _nativePrebuiltLogSubscription?.cancel();
    _nativePrebuiltLogSubscription = Logger.root.onRecord.listen(
      (record) => stderr.writeln(
        '[${buildProject.name}] ${record.level.name}: ${record.message}',
      ),
    );
    try {
      if (!input.config.buildCodeAssets) {
        Logger.root.info(
          'Skipping native_prebuilt: buildCodeAssets is disabled.',
        );
        return;
      }

      final buildFromSource = shouldBuildFromSource(input);

      final builder = NativeProjectBuilder(
        project: buildProject,
        resolvers: buildFromSource ? <PrebuiltResolver>[] : null,
      );

      await builder.run(input: input, output: output, logger: Logger.root);
    } finally {
      await _nativePrebuiltLogSubscription?.cancel();
      _nativePrebuiltLogSubscription = null;
    }
  });
}