nativePrebuiltBuild function
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.rootwithLevel.INFOoutput to stderr - Checks
BuildConfig.buildCodeAssetsand returns early if disabled - Reads the
build_from_sourceuser-defined flag fromHookInput.userDefinesto 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;
}
});
}