writeOutput method

FutureOr<void> writeOutput(
  1. BuildStep buildStep,
  2. String content,
  3. String extension
)
inherited

Formats and writes the generated content to the output file.

Attempts to format the content using the configured formatter. If formatting fails, logs an error but proceeds with writing the unformatted content. In development mode, allows syntax errors in the generated code.

@param buildStep The build step to use for writing @param content The content to format and write @param extension The file extension to use for the output

Implementation

FutureOr<void> writeOutput(
  BuildStep buildStep,
  String content,
  String extension,
) {
  try {
    content = formatOutput(content);
  } catch (e, stack) {
    final PackageFileResolver fileResolver = buildStep.resolver.fileResolver;
    final Uri output = buildStep.asset.uriWithExtension(extension);
    Logger.error(
      '''An error `${e.runtimeType}` occurred while formatting the generated source for `${buildStep.asset.shortUri}`
which was output to `${fileResolver.toShortUri(output)}`.
This may indicate an issue in the generator, the input source code, or in the source formatter.''',
      stackTrace: stack,
    );
    // allow syntax errors in the generated code in dev mode
    if (!_isDevMode) return Future<void>.value(null);
  }

  return buildStep.writeAsString(content, extension: extension);
}