runWithOptions static method

Future<int> runWithOptions(
  1. PigeonOptions options, {
  2. List<GeneratorAdapter>? adapters,
  3. String? sdkPath,
})

The 'main' entrypoint used by external packages. options is used when running the code generator. The optional parameter adapters allows you to customize the generators that pigeon will use. The optional parameter sdkPath allows you to specify the Dart SDK path.

Implementation

static Future<int> runWithOptions(PigeonOptions options,
    {List<GeneratorAdapter>? adapters, String? sdkPath}) async {
  final Pigeon pigeon = Pigeon.setup();
  if (options.debugGenerators ?? false) {
    generator_tools.debugGenerators = true;
  }
  final List<GeneratorAdapter> safeGeneratorAdapters = adapters ??
      <GeneratorAdapter>[
        DartGeneratorAdapter(),
        JavaGeneratorAdapter(),
        SwiftGeneratorAdapter(),
        KotlinGeneratorAdapter(),
        CppGeneratorAdapter(),
        DartTestGeneratorAdapter(),
        ObjcGeneratorAdapter(),
        AstGeneratorAdapter(),
      ];
  _executeConfigurePigeon(options);

  if (options.input == null) {
    print(usage);
    return 0;
  }

  final ParseResults parseResults =
      pigeon.parseFile(options.input!, sdkPath: sdkPath);

  final List<Error> errors = <Error>[];
  errors.addAll(parseResults.errors);

  // Helper to clean up non-Stdout sinks.
  Future<void> releaseSink(IOSink sink) async {
    if (sink is! Stdout) {
      await sink.close();
    }
  }

  for (final GeneratorAdapter adapter in safeGeneratorAdapters) {
    final IOSink? sink = adapter.shouldGenerate(options, FileType.source);
    if (sink != null) {
      final List<Error> adapterErrors =
          adapter.validate(options, parseResults.root);
      errors.addAll(adapterErrors);
      await releaseSink(sink);
    }
  }

  if (errors.isNotEmpty) {
    printErrors(errors
        .map((Error err) => Error(
            message: err.message,
            filename: options.input,
            lineNumber: err.lineNumber))
        .toList());
    return 1;
  }

  if (parseResults.pigeonOptions != null) {
    options = PigeonOptions.fromMap(
        mergeMaps(options.toMap(), parseResults.pigeonOptions!));
  }

  if (options.oneLanguage == false && options.dartOut == null) {
    print(usage);
    return 1;
  }

  if (options.objcHeaderOut != null) {
    options = options.merge(PigeonOptions(
        objcOptions: (options.objcOptions ?? const ObjcOptions()).merge(
            ObjcOptions(
                headerIncludePath: path.basename(options.objcHeaderOut!)))));
  }

  if (options.cppHeaderOut != null) {
    options = options.merge(PigeonOptions(
        cppOptions: (options.cppOptions ?? const CppOptions()).merge(
            CppOptions(
                headerIncludePath: path.basename(options.cppHeaderOut!)))));
  }

  for (final GeneratorAdapter adapter in safeGeneratorAdapters) {
    for (final FileType fileType in adapter.fileTypeList) {
      final IOSink? sink = adapter.shouldGenerate(options, fileType);
      if (sink != null) {
        adapter.generate(sink, options, parseResults.root, fileType);
        await sink.flush();
        await releaseSink(sink);
      }
    }
  }

  return 0;
}