writeOutput method

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

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

@override
FutureOr<void> writeOutput(
  BuildStep buildStep,
  String content,
  String extension,
) {
  if (outputExtensions.length != 1 || outputExtensions.first != SharedPartBuilder.extension) {
    throw ArgumentError(
      'The output extension must be ${SharedPartBuilder.extension} '
      'but was ${outputExtensions.join(', ')}',
    );
  }

  if (extension != SharedPartBuilder.extension) {
    throw ArgumentError('The Shared extension must be $extension');
  }
  if (!buildStep.hasValidPartDirectiveFor(extension)) {
    final Uri outputUri = buildStep.asset.uriWithExtension(extension);
    final String part = p.relative(
      outputUri.path,
      from: p.dirname(buildStep.asset.uri.path),
    );
    throw ArgumentError(
      'The input library must have a part directive for the generated part\n'
      'file. Please add a part directive (part \'$part\';) to the input library ${buildStep.asset.shortUri}',
    );
  }
  return super.writeOutput(buildStep, content, extension);
}