packagesWithBuildHooks static method

List<String>? packagesWithBuildHooks(
  1. Directory rootDirectory
)

The packages with build hooks (hook/build.dart) in the .dart_tool/package_config.json of rootDirectory.

Only the runtime dependencies count, as for dart compile exe: if there is a .dart_tool/package_graph.json, the devDependencies of the root packages (and the packages only they use) are ignored.

Returns null if the packages are not resolved yet.

Implementation

static List<String>? packagesWithBuildHooks(Directory rootDirectory) {
  var dartToolDir = Directory(
    pack_path.join(rootDirectory.path, '.dart_tool'),
  );

  var config = _readJson(
    File(pack_path.join(dartToolDir.path, 'package_config.json')),
  );

  var packages = config is Map ? config['packages'] : null;
  if (packages is! List) return null;

  var runtimePackages = _runtimePackages(
    _readJson(File(pack_path.join(dartToolDir.path, 'package_graph.json'))),
  );

  var configDirUri = Uri.directory(dartToolDir.absolute.path);

  var withHooks = <String>[];

  for (var p in packages.whereType<Map>()) {
    var name = p['name'];
    var rootUri = p['rootUri'];
    if (name is! String || rootUri is! String) continue;

    if (runtimePackages != null && !runtimePackages.contains(name)) {
      continue;
    }

    var packageUri = configDirUri.resolve(
      rootUri.endsWith('/') ? rootUri : '$rootUri/',
    );

    var hookFile = File.fromUri(packageUri.resolve('hook/build.dart'));
    if (hookFile.existsSync()) {
      withHooks.add(name);
    }
  }

  return withHooks;
}