fromBrickYaml static method

Future<GeneratorHooks> fromBrickYaml(
  1. BrickYaml brick
)

Creates GeneratorHooks from a provided BrickYaml.

Implementation

static Future<GeneratorHooks> fromBrickYaml(BrickYaml brick) async {
  HookFile? preGenHook;
  HookFile? postGenHook;
  List<int>? pubspec;

  final accumulator = AccumulatorSink<Digest>();
  final sink = sha1.startChunkedConversion(accumulator);

  try {
    final brickRoot = File(brick.path!).parent.path;
    final hooksDirectory = Directory(p.join(brickRoot, BrickYaml.hooks));
    final pubspecFilePath = p.join(hooksDirectory.path, 'pubspec.yaml');
    final preGenFilePath = p.join(
      hooksDirectory.path,
      GeneratorHook.preGen.toFileName(),
    );
    final postGenFilePath = p.join(
      hooksDirectory.path,
      GeneratorHook.postGen.toFileName(),
    );
    final dartFiles = hooksDirectory.existsSync()
        ? hooksDirectory
            .listSync(recursive: true)
            .where(_isHookFile)
            .cast<File>()
        : const <File>[];

    for (final file in dartFiles) {
      final bytes = await file.readAsBytes();
      sink.add(bytes);
      if (file.path == pubspecFilePath) {
        pubspec ??= bytes;
      } else if (file.path == preGenFilePath) {
        preGenHook ??= HookFile.fromBytes(file.path, bytes);
      } else if (file.path == postGenFilePath) {
        postGenHook ??= HookFile.fromBytes(file.path, bytes);
      }
    }
  } finally {
    sink.close();
  }

  return GeneratorHooks(
    preGenHook: preGenHook,
    postGenHook: postGenHook,
    pubspec: pubspec,
    checksum: accumulator.events.firstOrNull?.toString() ?? '',
  );
}