generateForLibrary method

Future<void> generateForLibrary(
  1. LibraryElement library,
  2. BuildStep buildStep
)
inherited

Generates code for the given library using all registered generators.

Collects outputs from all generators, formats them, and writes them to the appropriate output file with proper headers and separators.

@param library The library to generate code for @param buildStep The build step providing context for generation

Implementation

Future<void> generateForLibrary(
  LibraryElement library,
  BuildStep buildStep,
) async {
  final List<GeneratedOutput> generatedOutputs = await _generate(library, _generators, buildStep).toList();
  if (generatedOutputs.isEmpty) return;

  final StringBuffer contentBuffer = StringBuffer();
  if (_header.isNotEmpty) {
    contentBuffer.writeln(_header);
  }

  final String extension = buildStep.allowedExtensions.first;
  for (GeneratedOutput item in generatedOutputs) {
    if (_writeDescriptions) {
      contentBuffer
        ..writeln()
        ..writeln(_headerLine)
        ..writeAll(
          LineSplitter.split(
            item.generatorDescription,
          ).map((String line) => '// $line\n'),
        )
        ..writeln(_headerLine)
        ..writeln();
    }

    contentBuffer.write(item.output);
  }
  await writeOutput(buildStep, contentBuffer.toString(), extension);
}