Skip to content

Commit ff82995

Browse files
authored
[pigeon] Allows swift generator to skip error class generation (#7849)
Since the Kotlin generator allows skipping error class generation, it makes sense for the Swift generator to have the same option. Related to: #6183 flutter/flutter#142099
1 parent 567b0e1 commit ff82995

File tree

6 files changed

+25
-4
lines changed

6 files changed

+25
-4
lines changed

packages/pigeon/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 22.6.0
2+
3+
* [swift] Adds `includeErrorClass` to `SwiftOptions`.
4+
15
## 22.5.0
26

37
* [swift] Adds implementation for `@ProxyApi`.

packages/pigeon/lib/generator_tools.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import 'ast.dart';
1414
/// The current version of pigeon.
1515
///
1616
/// This must match the version in pubspec.yaml.
17-
const String pigeonVersion = '22.5.0';
17+
const String pigeonVersion = '22.6.0';
1818

1919
/// Read all the content from [stdin] to a String.
2020
String readStdin() {

packages/pigeon/lib/pigeon_lib.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,7 @@ class SwiftGeneratorAdapter implements GeneratorAdapter {
745745
path.posix.join(options.basePath ?? '', options.copyrightHeader))
746746
: null,
747747
errorClassName: swiftOptions.errorClassName,
748+
includeErrorClass: swiftOptions.includeErrorClass,
748749
));
749750
const SwiftGenerator generator = SwiftGenerator();
750751
generator.generate(

packages/pigeon/lib/swift_generator.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class SwiftOptions {
2828
this.copyrightHeader,
2929
this.fileSpecificClassNameComponent,
3030
this.errorClassName,
31+
this.includeErrorClass = true,
3132
});
3233

3334
/// A copyright header that will get prepended to generated code.
@@ -39,6 +40,12 @@ class SwiftOptions {
3940
/// The name of the error class used for passing custom error parameters.
4041
final String? errorClassName;
4142

43+
/// Whether to include the error class in generation.
44+
///
45+
/// This should only ever be set to false if you have another generated
46+
/// Swift file in the same directory.
47+
final bool includeErrorClass;
48+
4249
/// Creates a [SwiftOptions] from a Map representation where:
4350
/// `x = SwiftOptions.fromList(x.toMap())`.
4451
static SwiftOptions fromList(Map<String, Object> map) {
@@ -47,6 +54,7 @@ class SwiftOptions {
4754
fileSpecificClassNameComponent:
4855
map['fileSpecificClassNameComponent'] as String?,
4956
errorClassName: map['errorClassName'] as String?,
57+
includeErrorClass: map['includeErrorClass'] as bool? ?? true,
5058
);
5159
}
5260

@@ -58,6 +66,7 @@ class SwiftOptions {
5866
if (fileSpecificClassNameComponent != null)
5967
'fileSpecificClassNameComponent': fileSpecificClassNameComponent!,
6068
if (errorClassName != null) 'errorClassName': errorClassName!,
69+
'includeErrorClass': includeErrorClass,
6170
};
6271
return result;
6372
}
@@ -1303,7 +1312,9 @@ private func nilOrValue<T>(_ value: Any?) -> T? {
13031312
.any((Api api) => api.methods.isNotEmpty);
13041313
final bool hasProxyApi = root.apis.any((Api api) => api is AstProxyApi);
13051314

1306-
_writePigeonError(generatorOptions, indent);
1315+
if (generatorOptions.includeErrorClass) {
1316+
_writePigeonError(generatorOptions, indent);
1317+
}
13071318

13081319
if (hasHostApi || hasProxyApi) {
13091320
_writeWrapResult(indent);

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: 22.5.0 # This must match the version in lib/generator_tools.dart
5+
version: 22.6.0 # This must match the version in lib/generator_tools.dart
66

77
environment:
88
sdk: ^3.3.0

packages/pigeon/tool/shared/generation.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ Future<int> generateTestPigeons(
9191
? 'FlutterError'
9292
: '${pascalCaseName}Error';
9393

94-
final bool swiftErrorUseDefaultErrorName = input == 'core_tests';
94+
final bool swiftErrorUseDefaultErrorName =
95+
input == 'core_tests' || input == 'background_platform_channels';
9596

9697
final String? swiftErrorClassName =
9798
swiftErrorUseDefaultErrorName ? null : '${pascalCaseName}Error';
@@ -117,6 +118,7 @@ Future<int> generateTestPigeons(
117118
? null
118119
: '$outputBase/ios/Classes/$pascalCaseName.gen.swift',
119120
swiftErrorClassName: swiftErrorClassName,
121+
swiftIncludeErrorClass: input != 'core_tests',
120122
// Linux
121123
gobjectHeaderOut: skipLanguages.contains(GeneratorLanguage.gobject)
122124
? null
@@ -148,6 +150,7 @@ Future<int> generateTestPigeons(
148150
? null
149151
: '$outputBase/macos/Classes/$pascalCaseName.gen.swift',
150152
swiftErrorClassName: swiftErrorClassName,
153+
swiftIncludeErrorClass: input != 'core_tests',
151154
suppressVersion: true,
152155
dartPackageName: 'pigeon_integration_tests',
153156
injectOverflowTypes: includeOverflow && input == 'core_tests',
@@ -212,6 +215,7 @@ Future<int> runPigeon({
212215
String? kotlinPackage,
213216
String? kotlinErrorClassName,
214217
bool kotlinIncludeErrorClass = true,
218+
bool swiftIncludeErrorClass = true,
215219
String? swiftOut,
216220
String? swiftErrorClassName,
217221
String? cppHeaderOut,
@@ -272,6 +276,7 @@ Future<int> runPigeon({
272276
swiftOut: swiftOut,
273277
swiftOptions: SwiftOptions(
274278
errorClassName: swiftErrorClassName,
279+
includeErrorClass: swiftIncludeErrorClass,
275280
),
276281
basePath: basePath,
277282
dartPackageName: dartPackageName,

0 commit comments

Comments
 (0)