checkBuildHooks method

void checkBuildHooks(
  1. Directory rootDirectory, {
  2. String? platform,
  3. bool warnings = true,
})

Checks that the commands for platform support the build hooks of the packages of rootDirectory.

dart compile exe (and aot-snapshot) does NOT support build hooks, so a ReleasePackerError is thrown, suggesting dart_build_cli, before running a build that will fail. A dart compile kernel builds, but can't find the dynamic libraries of the hooks: only a warning, if warnings (the check before prepare doesn't repeat it).

Implementation

void checkBuildHooks(
  Directory rootDirectory, {
  String? platform,
  bool warnings = true,
}) {
  var withHooks = packagesWithBuildHooks(rootDirectory);
  if (withHooks == null || withHooks.isEmpty) return;

  var commands = allCommands(platform: platform);

  bool isCompile(ReleasePackerCommand c, List<String> kinds) =>
      c is ReleasePackerDartCommand &&
      c.command == 'compile' &&
      c.args.isNotEmpty &&
      kinds.contains(c.args.first);

  var kernelCommands = warnings
      ? commands.where((c) => isCompile(c, ['kernel']))
      : const <ReleasePackerCommand>[];
  for (var c in kernelCommands) {
    print(
      '  ▒  WARNING: packages with build hooks (${withHooks.join(', ')}): '
      "a `dart compile kernel` builds, but can't load their dynamic "
      'libraries: $c',
    );
  }

  var unsupported = commands
      .where((c) => isCompile(c, ['exe', 'aot-snapshot']))
      .toList();
  if (unsupported.isEmpty) return;

  var message =
      '`dart compile exe` does NOT support build hooks '
      '(packages with build hooks: ${withHooks.join(', ')}). '
      'Use `dart_build_cli` (`dart build cli`) instead: '
      '${unsupported.join(' ; ')}';

  print('  ▒▒  $message\n');

  throw ReleasePackerError(message, failedCommands: unsupported);
}