Skip to content

Commit b929854

Browse files
authored
[pigeon] add support for multiple swift outputs (#12720)
fixes flutter/flutter#164297
1 parent 36e088a commit b929854

12 files changed

Lines changed: 240 additions & 24 deletions

File tree

‎packages/pigeon/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 28.1.0
2+
3+
* [swift] Adds support for multiple output locations in `swiftOut` and `--swift_out`.
4+
15
## 28.0.0
26

37
* **Breaking Change** Updates Kotlin and Swift generators to generate `suspend` functions and `async throws` signatures for `@FlutterApi` methods by default, and for `@HostApi` methods annotated with `@async`.

‎packages/pigeon/example/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ needed for your project.
2727
kotlinOptions: KotlinOptions(),
2828
javaOut: 'android/app/src/main/java/io/flutter/plugins/Messages.java',
2929
javaOptions: JavaOptions(),
30+
// Note that swiftOut can also be a list to output to separate iOS and macOS
31+
// locations if required.
3032
swiftOut: 'ios/Runner/Messages.g.swift',
3133
swiftOptions: SwiftOptions(),
3234
objcHeaderOut: 'macos/Runner/messages.g.h',

‎packages/pigeon/example/app/pigeons/messages.dart‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import 'package:pigeon/pigeon.dart';
1919
kotlinOptions: KotlinOptions(),
2020
javaOut: 'android/app/src/main/java/io/flutter/plugins/Messages.java',
2121
javaOptions: JavaOptions(),
22+
// Note that swiftOut can also be a list to output to separate iOS and macOS
23+
// locations if required.
2224
swiftOut: 'ios/Runner/Messages.g.swift',
2325
swiftOptions: SwiftOptions(),
2426
objcHeaderOut: 'macos/Runner/messages.g.h',

‎packages/pigeon/lib/src/generator_tools.dart‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:io';
77
import 'dart:math';
88
import 'dart:mirrors';
99

10+
import 'package:path/path.dart' as path;
1011
import 'package:yaml/yaml.dart' as yaml;
1112

1213
import 'ast.dart';
@@ -15,7 +16,7 @@ import 'generator.dart';
1516
/// The current version of pigeon.
1617
///
1718
/// This must match the version in pubspec.yaml.
18-
const String pigeonVersion = '28.0.0';
19+
const String pigeonVersion = '28.1.0';
1920

2021
/// Default plugin package name.
2122
const String defaultPluginPackageName = 'dev.flutter.pigeon';
@@ -775,6 +776,16 @@ String? deducePackageName(String mainDartFile) {
775776
}
776777
}
777778

779+
/// Extracts the base class name component from an output file path.
780+
///
781+
/// For example, extracts 'messages' from 'ios/Classes/messages.g.swift'.
782+
String deduceClassNameComponent(String? filePath) {
783+
if (filePath == null || filePath.isEmpty) {
784+
return '';
785+
}
786+
return path.basename(filePath).split('.').first;
787+
}
788+
778789
/// Enum to specify api type when generating code.
779790
enum ApiType {
780791
/// Flutter api.

‎packages/pigeon/lib/src/kotlin/kotlin_generator.dart‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ class InternalKotlinOptions extends InternalOptions {
131131
includeErrorClass = options.includeErrorClass,
132132
useGeneratedAnnotation = options.useGeneratedAnnotation,
133133
fileSpecificClassNameComponent =
134-
options.fileSpecificClassNameComponent ??
135-
kotlinOut.split('/').lastOrNull?.split('.').first;
134+
options.fileSpecificClassNameComponent ?? deduceClassNameComponent(kotlinOut);
136135

137136
/// The package where the generated class will live.
138137
final String? package;

‎packages/pigeon/lib/src/pigeon_lib.dart‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,23 @@ class PigeonOptions {
294294
/// Options that control how Java will be generated.
295295
final JavaOptions? javaOptions;
296296

297-
/// Path to the swift file that will be generated.
298-
final String? swiftOut;
297+
/// Path to the swift file(s) that will be generated.
298+
///
299+
/// Can be either a [String] for a single file, or an [Iterable<String>] for
300+
/// multiple files.
301+
final Object? swiftOut;
302+
303+
/// Returns all output paths for Swift from [swiftOut].
304+
Iterable<String>? get swiftOutPaths {
305+
final Object? out = swiftOut;
306+
if (out is String) {
307+
return <String>[out];
308+
}
309+
if (out is Iterable) {
310+
return out.whereType<String>();
311+
}
312+
return null;
313+
}
299314

300315
/// Options that control how Swift will be generated.
301316
final SwiftOptions? swiftOptions;
@@ -361,7 +376,9 @@ class PigeonOptions {
361376
javaOptions: map.containsKey('javaOptions')
362377
? JavaOptions.fromMap(map['javaOptions']! as Map<String, Object>)
363378
: null,
364-
swiftOut: map['swiftOut'] as String?,
379+
swiftOut: map['swiftOut'] is Iterable
380+
? (map['swiftOut']! as Iterable<dynamic>).cast<String>().toList()
381+
: map['swiftOut'] as String?,
365382
swiftOptions: map.containsKey('swiftOptions')
366383
? SwiftOptions.fromList(map['swiftOptions']! as Map<String, Object>)
367384
: null,
@@ -520,9 +537,9 @@ ${_argParser.usage}''';
520537
'java_use_generated_annotation',
521538
help: 'Adds the java.annotation.Generated annotation to the output.',
522539
)
523-
..addOption(
540+
..addMultiOption(
524541
'swift_out',
525-
help: 'Path to generated Swift file (.swift).',
542+
help: 'Path to generated Swift file(s) (.swift).',
526543
aliases: const <String>['experimental_swift_out'],
527544
)
528545
..addOption(
@@ -596,6 +613,7 @@ ${_argParser.usage}''';
596613
// get set in the `run` function to accommodate users that are using the
597614
// `configurePigeon` function.
598615
final ArgResults results = _argParser.parse(args);
616+
final swiftOuts = results['swift_out'] as List<String>;
599617

600618
final opts = PigeonOptions(
601619
input: results['input'] as String?,
@@ -609,7 +627,9 @@ ${_argParser.usage}''';
609627
package: results['java_package'] as String?,
610628
useGeneratedAnnotation: results['java_use_generated_annotation'] as bool?,
611629
),
612-
swiftOut: results['swift_out'] as String?,
630+
swiftOut: results.wasParsed('swift_out')
631+
? (swiftOuts.length == 1 ? swiftOuts.first : swiftOuts)
632+
: null,
613633
kotlinOut: results['kotlin_out'] as String?,
614634
kotlinOptions: KotlinOptions(
615635
package: results['kotlin_package'] as String?,

‎packages/pigeon/lib/src/pigeon_lib_internal.dart‎

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ class InternalPigeonOptions {
5656
options.objcOptions ?? const ObjcOptions(),
5757
objcHeaderOut: options.objcHeaderOut!,
5858
objcSourceOut: options.objcSourceOut!,
59-
fileSpecificClassNameComponent:
60-
options.objcSourceOut?.split('/').lastOrNull?.split('.').firstOrNull ?? '',
59+
fileSpecificClassNameComponent: deduceClassNameComponent(options.objcSourceOut),
6160
copyrightHeader: copyrightHeader,
6261
),
6362
javaOptions = options.javaOut == null
@@ -67,11 +66,11 @@ class InternalPigeonOptions {
6766
javaOut: options.javaOut!,
6867
copyrightHeader: copyrightHeader,
6968
),
70-
swiftOptions = options.swiftOut == null
69+
swiftOptions = (options.swiftOutPaths == null || options.swiftOutPaths!.isEmpty)
7170
? null
7271
: InternalSwiftOptions.fromSwiftOptions(
7372
options.swiftOptions ?? const SwiftOptions(),
74-
swiftOut: options.swiftOut!,
73+
swiftOuts: options.swiftOutPaths,
7574
copyrightHeader: copyrightHeader,
7675
),
7776
kotlinOptions = options.kotlinOut == null
@@ -171,16 +170,28 @@ Iterable<String> _lineReader(String path) sync* {
171170
}
172171
}
173172

173+
File _getFile(String output, {String basePath = ''}) {
174+
final file = File(path.posix.join(basePath, output));
175+
file.createSync(recursive: true);
176+
return file;
177+
}
178+
174179
IOSink? _openSink(String? output, {String basePath = ''}) {
175180
if (output == null) {
176181
return null;
177182
}
178183
if (output == 'stdout') {
179184
return stdout;
180185
}
181-
final file = File(path.posix.join(basePath, output));
182-
file.createSync(recursive: true);
183-
return file.openWrite();
186+
return _getFile(output, basePath: basePath).openWrite();
187+
}
188+
189+
void _writeToOutput(String output, String content, {String basePath = ''}) {
190+
if (output == 'stdout') {
191+
stdout.write(content);
192+
} else {
193+
_getFile(output, basePath: basePath).writeAsStringSync(content);
194+
}
184195
}
185196

186197
/// An adapter that will call a generator to write code to a sink
@@ -405,12 +416,35 @@ class SwiftGeneratorAdapter implements GeneratorAdapter {
405416
return;
406417
}
407418
const generator = SwiftGenerator();
408-
generator.generate(options.swiftOptions!, root, sink, dartPackageName: options.dartPackageName);
419+
final List<String> outputs = options.swiftOptions!.allSwiftOuts.toList();
420+
if (outputs.isEmpty) {
421+
generator.generate(
422+
options.swiftOptions!,
423+
root,
424+
sink,
425+
dartPackageName: options.dartPackageName,
426+
);
427+
return;
428+
}
429+
final buffer = StringBuffer();
430+
generator.generate(
431+
options.swiftOptions!,
432+
root,
433+
buffer,
434+
dartPackageName: options.dartPackageName,
435+
);
436+
final content = buffer.toString();
437+
// [sink] was opened for the first entry in [outputs] by [shouldGenerate],
438+
// so only the remaining output files need to be written here.
439+
sink.write(content);
440+
for (final String outputPath in outputs.skip(1)) {
441+
_writeToOutput(outputPath, content, basePath: options.basePath ?? '');
442+
}
409443
}
410444

411445
@override
412446
IOSink? shouldGenerate(InternalPigeonOptions options, FileType _) =>
413-
_openSink(options.swiftOptions?.swiftOut, basePath: options.basePath ?? '');
447+
_openSink(options.swiftOptions?.allSwiftOuts.firstOrNull, basePath: options.basePath ?? '');
414448

415449
@override
416450
List<Error> validate(InternalPigeonOptions options, Root root) {

‎packages/pigeon/lib/src/swift/swift_generator.dart‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class InternalSwiftOptions extends InternalOptions {
8484
const InternalSwiftOptions({
8585
this.copyrightHeader,
8686
required this.swiftOut,
87+
this.swiftOuts,
8788
this.fileSpecificClassNameComponent,
8889
this.errorClassName,
8990
this.includeErrorClass = true,
@@ -92,22 +93,33 @@ class InternalSwiftOptions extends InternalOptions {
9293
/// Creates InternalSwiftOptions from SwiftOptions.
9394
InternalSwiftOptions.fromSwiftOptions(
9495
SwiftOptions options, {
95-
required this.swiftOut,
96+
Iterable<String>? swiftOuts,
97+
String? swiftOut,
9698
Iterable<String>? copyrightHeader,
9799
}) : copyrightHeader = options.copyrightHeader ?? copyrightHeader,
98100
fileSpecificClassNameComponent =
99101
options.fileSpecificClassNameComponent ??
100-
swiftOut.split('/').lastOrNull?.split('.').firstOrNull ??
101-
'',
102+
deduceClassNameComponent(swiftOuts?.firstOrNull ?? swiftOut),
102103
errorClassName = options.errorClassName,
103-
includeErrorClass = options.includeErrorClass;
104+
includeErrorClass = options.includeErrorClass,
105+
swiftOut = swiftOut ?? swiftOuts?.firstOrNull ?? '',
106+
swiftOuts = swiftOuts ?? (swiftOut != null ? <String>[swiftOut] : const <String>[]);
104107

105108
/// A copyright header that will get prepended to generated code.
106109
final Iterable<String>? copyrightHeader;
107110

108111
/// Path to the swift file that will be generated.
112+
///
113+
/// If multiple output paths were specified, this contains the first path.
109114
final String swiftOut;
110115

116+
/// Paths to all swift files that will be generated.
117+
final Iterable<String>? swiftOuts;
118+
119+
/// Returns all output paths for Swift.
120+
Iterable<String> get allSwiftOuts =>
121+
swiftOuts ?? (swiftOut.isNotEmpty ? <String>[swiftOut] : const <String>[]);
122+
111123
/// A String to augment class names to avoid cross file collisions.
112124
final String? fileSpecificClassNameComponent;
113125

‎packages/pigeon/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: pigeon
22
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
33
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+pigeon%22
5-
version: 28.0.0 # This must match the version in lib/src/generator_tools.dart
5+
version: 28.1.0 # This must match the version in lib/src/generator_tools.dart
66

77
environment:
88
sdk: ^3.11.0

‎packages/pigeon/test/generator_tools_test.dart‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,4 +466,11 @@ void myMethod() {
466466
}
467467
''');
468468
});
469+
470+
test('deduceClassNameComponent extracts class name component', () {
471+
expect(deduceClassNameComponent('ios/Classes/messages.g.swift'), 'messages');
472+
expect(deduceClassNameComponent('Foo.swift'), 'Foo');
473+
expect(deduceClassNameComponent(''), '');
474+
expect(deduceClassNameComponent(null), '');
475+
});
469476
}

0 commit comments

Comments
 (0)