runWithOptions static method
Future<int>
runWithOptions(
- PigeonOptions options, {
- List<
GeneratorAdapter> ? adapters, - String? sdkPath,
- bool injectOverflowTypes = false,
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,
bool injectOverflowTypes = false,
}) 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(),
GObjectGeneratorAdapter(),
DartTestGeneratorAdapter(),
ObjcGeneratorAdapter(),
AstGeneratorAdapter(),
];
_executeConfigurePigeon(options);
if (options.input == null) {
print(usage);
return 0;
}
final ParseResults parseResults =
pigeon.parseFile(options.input!, sdkPath: sdkPath);
if (injectOverflowTypes) {
final List<Enum> addedEnums = List<Enum>.generate(
totalCustomCodecKeysAllowed - 1,
(final int tag) {
return Enum(
name: 'FillerEnum$tag',
members: <EnumMember>[EnumMember(name: 'FillerMember$tag')]);
},
);
addedEnums.addAll(parseResults.root.enums);
parseResults.root.enums = addedEnums;
}
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) {
if (injectOverflowTypes && adapter is GObjectGeneratorAdapter) {
continue;
}
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: options.objcOptions?.headerIncludePath ??
path.basename(options.objcHeaderOut!)))));
}
if (options.cppHeaderOut != null) {
options = options.merge(PigeonOptions(
cppOptions: (options.cppOptions ?? const CppOptions()).merge(
CppOptions(
headerIncludePath: options.cppOptions?.headerIncludePath ??
path.basename(options.cppHeaderOut!)))));
}
if (options.gobjectHeaderOut != null) {
options = options.merge(PigeonOptions(
gobjectOptions: (options.gobjectOptions ?? const GObjectOptions())
.merge(GObjectOptions(
headerIncludePath:
path.basename(options.gobjectHeaderOut!)))));
}
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;
}