writeMainHeader method

void writeMainHeader(
  1. IndentingWriter out, [
  2. OutputConfiguration config = const DefaultOutputConfiguration()
])

Writes the header and imports for the .pb.dart file.

Implementation

void writeMainHeader(IndentingWriter out,
    [OutputConfiguration config = const DefaultOutputConfiguration()]) {
  _writeHeading(out);

  final importWriter = ImportWriter();

  // We only add the dart:async import if there are generic client API
  // generators for services in the FileDescriptorProto.
  if (clientApiGenerators.isNotEmpty) {
    importWriter.addImport(_asyncImportUrl, prefix: asyncImportPrefix);
  }

  importWriter.addImport(_coreImportUrl, prefix: coreImportPrefix);

  if (_needsFixnumImport) {
    importWriter.addImport('package:fixnum/fixnum.dart',
        prefix: _fixnumImportPrefix);
  }

  if (_needsProtobufImport) {
    importWriter.addImport(_protobufImportUrl, prefix: protobufImportPrefix);
  }

  for (final libraryUri in findMixinImports()) {
    importWriter.addImport(libraryUri, prefix: mixinImportPrefix);
  }

  // Import the .pb.dart files we depend on.
  final imports = Set<FileGenerator>.identity();
  final enumImports = Set<FileGenerator>.identity();
  _findProtosToImport(imports, enumImports);

  for (final target in imports) {
    _addImport(importWriter, config, target, '.pb.dart');
  }

  for (final target in enumImports) {
    _addImport(importWriter, config, target, '.pbenum.dart');
  }

  for (final publicDependency in descriptor.publicDependency) {
    _addExport(importWriter, config,
        Uri.file(descriptor.dependency[publicDependency]), '.pb.dart');
  }

  // Export enums in main file for backward compatibility.
  if (enumCount > 0) {
    final url =
        config.resolveImport(protoFileUri, protoFileUri, '.pbenum.dart');
    importWriter.addExport(url.toString());
  }

  out.println(importWriter.emit());
}