Skip to content

Commit

Permalink
[Pigeon] Add one_language flag to pigeon (flutter#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenqiu1101 authored Jul 29, 2021
1 parent c20951f commit 41b51d0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* [front-end] Added more errors for incorrect usage of Pigeon (previously they were just ignored).
* Moved Pigeon to using a custom codec which allows collection types to contain custom classes.
* Started allowing primitive data types as arguments and return types.
* Added one_language flag for allowing Pigeon to only generate code for one platform.

## 0.3.0

Expand Down
19 changes: 15 additions & 4 deletions packages/pigeon/lib/pigeon_lib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ class PigeonOptions {
this.javaOut,
this.javaOptions,
this.dartOptions,
this.copyrightHeader});
this.copyrightHeader,
this.oneLanguage});

/// Path to the file which will be processed.
final String? input;
Expand Down Expand Up @@ -147,6 +148,9 @@ class PigeonOptions {
/// Path to a copyright header that will get prepended to generated code.
final String? copyrightHeader;

/// If Pigeon allows generating code for one language.
final bool? oneLanguage;

/// Creates a [PigeonOptions] from a Map representation where:
/// `x = PigeonOptions.fromMap(x.toMap())`.
static PigeonOptions fromMap(Map<String, Object> map) {
Expand All @@ -167,6 +171,7 @@ class PigeonOptions {
? DartOptions.fromMap((map['dartOptions'] as Map<String, Object>?)!)
: null,
copyrightHeader: map['copyrightHeader'] as String?,
oneLanguage: map['oneLanguage'] as bool?,
);
}

Expand Down Expand Up @@ -806,7 +811,8 @@ options:
static final ArgParser _argParser = ArgParser()
..addOption('input', help: 'REQUIRED: Path to pigeon file.')
..addOption('dart_out',
help: 'REQUIRED: Path to generated Dart source file (.dart).')
help: 'Path to generated Dart source file (.dart). '
'Required if one_language is not specified.')
..addOption('dart_test_out',
help: 'Path to generated library for Dart tests, when using '
'@HostApi(dartHostTestHandler:).')
Expand All @@ -824,7 +830,10 @@ options:
help: 'Prefix for generated Objective-C classes and protocols.')
..addOption('copyright_header',
help:
'Path to file with copyright header to be prepended to generated code.');
'Path to file with copyright header to be prepended to generated code.')
..addFlag('one_language',
help: 'Allow Pigeon to only generate code for one language.',
defaultsTo: false);

/// Convert command-line arguments to [PigeonOptions].
static PigeonOptions parseArgs(List<String> args) {
Expand All @@ -851,6 +860,7 @@ options:
isNullSafe: results['dart_null_safety'],
),
copyrightHeader: results['copyright_header'],
oneLanguage: results['one_language'],
);
return opts;
}
Expand Down Expand Up @@ -891,7 +901,8 @@ options:
];
_executeConfigurePigeon(options);

if (options.input == null || options.dartOut == null) {
if (options.input == null ||
(options.oneLanguage == false && options.dartOut == null)) {
print(usage);
return 0;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/pigeon/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ test_running_without_arguments() {
$run_pigeon 1>/dev/null
}

# Test one_language flag. With this flag specified, java_out can be generated
# without dart_out.
test_one_language_flag() {
$run_pigeon \
--input pigeons/message.dart \
--one_language \
--java_out stdout \
| grep "public class Message">/dev/null
}

run_flutter_unittests() {
pushd $PWD
$run_pigeon \
Expand Down Expand Up @@ -294,6 +304,8 @@ run_ios_e2e_tests() {
}

run_android_unittests() {
test_one_language_flag

pushd $PWD
gen_android_unittests_code ./pigeons/all_datatypes.dart AllDatatypes
gen_android_unittests_code ./pigeons/all_void.dart AllVoid
Expand Down
5 changes: 5 additions & 0 deletions packages/pigeon/test/pigeon_lib_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ void main() {
expect(opts.objcSourceOut, equals('foo.m'));
});

test('parse args - one_language', () {
final PigeonOptions opts = Pigeon.parseArgs(<String>['--one_language']);
expect(opts.oneLanguage, isTrue);
});

test('simple parse api', () {
const String code = '''
class Input1 {
Expand Down

0 comments on commit 41b51d0

Please sign in to comment.