Skip to content

Commit

Permalink
Add basePath
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrinneal committed Jun 29, 2023
1 parent f889c3b commit b206b56
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 102 deletions.
2 changes: 2 additions & 0 deletions packages/pigeon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ the threading model for handling HostApi methods can be selected with the
`void`.
1) Generics are supported, but can currently only be used with nullable types
(example: `List<int?>`).
1) Objc and Swift have special naming conventions that can be utilized with the
`@ObjCSelector` and `@SwiftFunction` respectively.

### Flutter calling into iOS steps

Expand Down
34 changes: 0 additions & 34 deletions packages/pigeon/example/app/pigeons/messages_test.dart

This file was deleted.

112 changes: 66 additions & 46 deletions packages/pigeon/lib/pigeon_lib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,27 +168,29 @@ class Error {
/// Options used when running the code generator.
class PigeonOptions {
/// Creates a instance of PigeonOptions
const PigeonOptions(
{this.input,
this.dartOut,
this.dartTestOut,
this.objcHeaderOut,
this.objcSourceOut,
this.objcOptions,
this.javaOut,
this.javaOptions,
this.swiftOut,
this.swiftOptions,
this.kotlinOut,
this.kotlinOptions,
this.cppHeaderOut,
this.cppSourceOut,
this.cppOptions,
this.dartOptions,
this.copyrightHeader,
this.oneLanguage,
this.astOut,
this.debugGenerators});
const PigeonOptions({
this.input,
this.dartOut,
this.dartTestOut,
this.objcHeaderOut,
this.objcSourceOut,
this.objcOptions,
this.javaOut,
this.javaOptions,
this.swiftOut,
this.swiftOptions,
this.kotlinOut,
this.kotlinOptions,
this.cppHeaderOut,
this.cppSourceOut,
this.cppOptions,
this.dartOptions,
this.copyrightHeader,
this.oneLanguage,
this.astOut,
this.debugGenerators,
this.basePath,
});

/// Path to the file which will be processed.
final String? input;
Expand Down Expand Up @@ -250,6 +252,9 @@ class PigeonOptions {
/// True means print out line number of generators in comments at newlines.
final bool? debugGenerators;

/// A base path to be prepended to all provided output paths.
final String? basePath;

/// Creates a [PigeonOptions] from a Map representation where:
/// `x = PigeonOptions.fromMap(x.toMap())`.
static PigeonOptions fromMap(Map<String, Object> map) {
Expand Down Expand Up @@ -286,6 +291,7 @@ class PigeonOptions {
oneLanguage: map['oneLanguage'] as bool?,
astOut: map['astOut'] as String?,
debugGenerators: map['debugGenerators'] as bool?,
basePath: map['basePath'] as String?,
);
}

Expand Down Expand Up @@ -313,6 +319,7 @@ class PigeonOptions {
if (astOut != null) 'astOut': astOut!,
if (oneLanguage != null) 'oneLanguage': oneLanguage!,
if (debugGenerators != null) 'debugGenerators': debugGenerators!,
if (basePath != null) 'basePath': basePath!,
};
return result;
}
Expand Down Expand Up @@ -353,7 +360,7 @@ Iterable<String> _lineReader(String path) sync* {
}
}

IOSink? _openSink(String? output) {
IOSink? _openSink(String? output, {String basePath = ''}) {
if (output == null) {
return null;
}
Expand All @@ -362,7 +369,7 @@ IOSink? _openSink(String? output) {
if (output == 'stdout') {
sink = stdout;
} else {
file = File(output);
file = File(basePath + output);
sink = file.openWrite();
}
return sink;
Expand Down Expand Up @@ -394,14 +401,19 @@ abstract class GeneratorAdapter {
}

DartOptions _dartOptionsWithCopyrightHeader(
DartOptions? dartOptions, String? copyrightHeader,
{String? dartOutPath, String? testOutPath}) {
DartOptions? dartOptions,
String? copyrightHeader, {
String? dartOutPath,
String? testOutPath,
String basePath = '',
}) {
dartOptions = dartOptions ?? const DartOptions();
return dartOptions.merge(DartOptions(
sourceOutPath: dartOutPath,
testOutPath: testOutPath,
copyrightHeader:
copyrightHeader != null ? _lineReader(copyrightHeader) : null));
copyrightHeader: copyrightHeader != null
? _lineReader(basePath + copyrightHeader)
: null));
}

/// A [GeneratorAdapter] that generates the AST.
Expand All @@ -420,7 +432,7 @@ class AstGeneratorAdapter implements GeneratorAdapter {

@override
IOSink? shouldGenerate(PigeonOptions options, FileType _) =>
_openSink(options.astOut);
_openSink(options.astOut, basePath: options.basePath ?? '');

@override
List<Error> validate(PigeonOptions options, Root root) => <Error>[];
Expand All @@ -438,14 +450,17 @@ class DartGeneratorAdapter implements GeneratorAdapter {
void generate(
StringSink sink, PigeonOptions options, Root root, FileType fileType) {
final DartOptions dartOptionsWithHeader = _dartOptionsWithCopyrightHeader(
options.dartOptions, options.copyrightHeader);
options.dartOptions,
options.copyrightHeader,
basePath: options.basePath ?? '',
);
const DartGenerator generator = DartGenerator();
generator.generate(dartOptionsWithHeader, root, sink);
}

@override
IOSink? shouldGenerate(PigeonOptions options, FileType _) =>
_openSink(options.dartOut);
_openSink(options.dartOut, basePath: options.basePath ?? '');

@override
List<Error> validate(PigeonOptions options, Root root) => <Error>[];
Expand All @@ -467,6 +482,7 @@ class DartTestGeneratorAdapter implements GeneratorAdapter {
options.copyrightHeader,
dartOutPath: options.dartOut,
testOutPath: options.dartTestOut,
basePath: options.basePath ?? '',
);
const DartGenerator testGenerator = DartGenerator();
testGenerator.generateTest(dartOptionsWithHeader, root, sink);
Expand All @@ -475,7 +491,7 @@ class DartTestGeneratorAdapter implements GeneratorAdapter {
@override
IOSink? shouldGenerate(PigeonOptions options, FileType _) {
if (options.dartTestOut != null) {
return _openSink(options.dartTestOut);
return _openSink(options.dartTestOut, basePath: options.basePath ?? '');
} else {
return null;
}
Expand All @@ -500,7 +516,7 @@ class ObjcGeneratorAdapter implements GeneratorAdapter {
final ObjcOptions objcOptions = options.objcOptions ?? const ObjcOptions();
final ObjcOptions objcOptionsWithHeader = objcOptions.merge(ObjcOptions(
copyrightHeader: options.copyrightHeader != null
? _lineReader(options.copyrightHeader!)
? _lineReader((options.basePath ?? '') + options.copyrightHeader!)
: null,
));
final OutputFileOptions<ObjcOptions> outputFileOptions =
Expand All @@ -513,9 +529,9 @@ class ObjcGeneratorAdapter implements GeneratorAdapter {
@override
IOSink? shouldGenerate(PigeonOptions options, FileType fileType) {
if (fileType == FileType.source) {
return _openSink(options.objcSourceOut);
return _openSink(options.objcSourceOut, basePath: options.basePath ?? '');
} else {
return _openSink(options.objcHeaderOut);
return _openSink(options.objcHeaderOut, basePath: options.basePath ?? '');
}
}

Expand All @@ -539,15 +555,15 @@ class JavaGeneratorAdapter implements GeneratorAdapter {
className: javaOptions.className ??
path.basenameWithoutExtension(options.javaOut!),
copyrightHeader: options.copyrightHeader != null
? _lineReader(options.copyrightHeader!)
? _lineReader((options.basePath ?? '') + options.copyrightHeader!)
: null));
const JavaGenerator generator = JavaGenerator();
generator.generate(javaOptions, root, sink);
}

@override
IOSink? shouldGenerate(PigeonOptions options, FileType _) =>
_openSink(options.javaOut);
_openSink(options.javaOut, basePath: options.basePath ?? '');

@override
List<Error> validate(PigeonOptions options, Root root) => <Error>[];
Expand All @@ -567,15 +583,15 @@ class SwiftGeneratorAdapter implements GeneratorAdapter {
SwiftOptions swiftOptions = options.swiftOptions ?? const SwiftOptions();
swiftOptions = swiftOptions.merge(SwiftOptions(
copyrightHeader: options.copyrightHeader != null
? _lineReader(options.copyrightHeader!)
? _lineReader((options.basePath ?? '') + options.copyrightHeader!)
: null));
const SwiftGenerator generator = SwiftGenerator();
generator.generate(swiftOptions, root, sink);
}

@override
IOSink? shouldGenerate(PigeonOptions options, FileType _) =>
_openSink(options.swiftOut);
_openSink(options.swiftOut, basePath: options.basePath ?? '');

@override
List<Error> validate(PigeonOptions options, Root root) => <Error>[];
Expand All @@ -596,7 +612,7 @@ class CppGeneratorAdapter implements GeneratorAdapter {
final CppOptions cppOptions = options.cppOptions ?? const CppOptions();
final CppOptions cppOptionsWithHeader = cppOptions.merge(CppOptions(
copyrightHeader: options.copyrightHeader != null
? _lineReader(options.copyrightHeader!)
? _lineReader((options.basePath ?? '') + options.copyrightHeader!)
: null,
));
final OutputFileOptions<CppOptions> outputFileOptions =
Expand All @@ -609,9 +625,9 @@ class CppGeneratorAdapter implements GeneratorAdapter {
@override
IOSink? shouldGenerate(PigeonOptions options, FileType fileType) {
if (fileType == FileType.source) {
return _openSink(options.cppSourceOut);
return _openSink(options.cppSourceOut, basePath: options.basePath ?? '');
} else {
return _openSink(options.cppHeaderOut);
return _openSink(options.cppHeaderOut, basePath: options.basePath ?? '');
}
}

Expand All @@ -635,15 +651,15 @@ class KotlinGeneratorAdapter implements GeneratorAdapter {
kotlinOptions = kotlinOptions.merge(KotlinOptions(
errorClassName: kotlinOptions.errorClassName ?? 'FlutterError',
copyrightHeader: options.copyrightHeader != null
? _lineReader(options.copyrightHeader!)
? _lineReader((options.basePath ?? '') + options.copyrightHeader!)
: null));
const KotlinGenerator generator = KotlinGenerator();
generator.generate(kotlinOptions, root, sink);
}

@override
IOSink? shouldGenerate(PigeonOptions options, FileType _) =>
_openSink(options.kotlinOut);
_openSink(options.kotlinOut, basePath: options.basePath ?? '');

@override
List<Error> validate(PigeonOptions options, Root root) => <Error>[];
Expand Down Expand Up @@ -897,7 +913,7 @@ class _RootBuilder extends dart_ast_visitor.RecursiveAstVisitor<Object?> {
} else {
_errors.add(Error(
message:
'unrecongized expression type ${expression.runtimeType} $expression',
'unrecognized expression type ${expression.runtimeType} $expression',
lineNumber: _calculateLineNumber(source, expression.offset),
));
return 0;
Expand Down Expand Up @@ -1356,14 +1372,17 @@ ${_argParser.usage}''';
help:
'Path to generated AST debugging info. (Warning: format subject to change)')
..addFlag('debug_generators',
help: 'Print the line number of the generator in comments at newlines.')
..addOption('base_path',
help:
'Print the line number of the generator in comments at newlines.');
'A base path to be prefixed to all outputs and copyright header path. Generally used for testing',
hide: true);

/// Convert command-line arguments to [PigeonOptions].
static PigeonOptions parseArgs(List<String> args) {
// Note: This function shouldn't perform any logic, just translate the args
// to PigeonOptions. Synthesized values inside of the PigeonOption should
// get set in the `run` function to accomodate users that are using the
// get set in the `run` function to accommodate users that are using the
// `configurePigeon` function.
final ArgResults results = _argParser.parse(args);

Expand Down Expand Up @@ -1396,6 +1415,7 @@ ${_argParser.usage}''';
oneLanguage: results['one_language'] as bool?,
astOut: results['ast_out'] as String?,
debugGenerators: results['debug_generators'] as bool?,
basePath: results['base_path'] as String?,
);
return opts;
}
Expand Down
Loading

0 comments on commit b206b56

Please sign in to comment.