Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pigeon] Allows swift generator to skip error class generation #7849

Merged
merged 11 commits into from
Oct 23, 2024
4 changes: 4 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 22.6.0

* [swift] Adds `includeErrorClass` to `SwiftOptions`.

## 22.5.0

* [swift] Adds implementation for `@ProxyApi`.
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'ast.dart';
/// The current version of pigeon.
///
/// This must match the version in pubspec.yaml.
const String pigeonVersion = '22.5.0';
const String pigeonVersion = '22.6.0';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
1 change: 1 addition & 0 deletions packages/pigeon/lib/pigeon_lib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ class SwiftGeneratorAdapter implements GeneratorAdapter {
path.posix.join(options.basePath ?? '', options.copyrightHeader))
: null,
errorClassName: swiftOptions.errorClassName,
includeErrorClass: swiftOptions.includeErrorClass,
));
const SwiftGenerator generator = SwiftGenerator();
generator.generate(
Expand Down
13 changes: 12 additions & 1 deletion packages/pigeon/lib/swift_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SwiftOptions {
this.copyrightHeader,
this.fileSpecificClassNameComponent,
this.errorClassName,
this.includeErrorClass = true,
});

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

/// Whether to include the error class in generation.
///
/// This should only ever be set to false if you have another generated
/// Swift file in the same directory.
final bool includeErrorClass;

/// Creates a [SwiftOptions] from a Map representation where:
/// `x = SwiftOptions.fromList(x.toMap())`.
static SwiftOptions fromList(Map<String, Object> map) {
Expand All @@ -47,6 +54,7 @@ class SwiftOptions {
fileSpecificClassNameComponent:
map['fileSpecificClassNameComponent'] as String?,
errorClassName: map['errorClassName'] as String?,
includeErrorClass: map['includeErrorClass'] as bool? ?? true,
);
}

Expand All @@ -58,6 +66,7 @@ class SwiftOptions {
if (fileSpecificClassNameComponent != null)
'fileSpecificClassNameComponent': fileSpecificClassNameComponent!,
if (errorClassName != null) 'errorClassName': errorClassName!,
'includeErrorClass': includeErrorClass,
};
return result;
}
Expand Down Expand Up @@ -1303,7 +1312,9 @@ private func nilOrValue<T>(_ value: Any?) -> T? {
.any((Api api) => api.methods.isNotEmpty);
final bool hasProxyApi = root.apis.any((Api api) => api is AstProxyApi);

_writePigeonError(generatorOptions, indent);
if (generatorOptions.includeErrorClass) {
_writePigeonError(generatorOptions, indent);
}

if (hasHostApi || hasProxyApi) {
_writeWrapResult(indent);
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: pigeon
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+pigeon%22
version: 22.5.0 # This must match the version in lib/generator_tools.dart
version: 22.6.0 # This must match the version in lib/generator_tools.dart

environment:
sdk: ^3.3.0
Expand Down
7 changes: 6 additions & 1 deletion packages/pigeon/tool/shared/generation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ Future<int> generateTestPigeons(
? 'FlutterError'
: '${pascalCaseName}Error';

final bool swiftErrorUseDefaultErrorName = input == 'core_tests';
final bool swiftErrorUseDefaultErrorName =
input == 'core_tests' || input == 'background_platform_channels';

final String? swiftErrorClassName =
swiftErrorUseDefaultErrorName ? null : '${pascalCaseName}Error';
Expand All @@ -117,6 +118,7 @@ Future<int> generateTestPigeons(
? null
: '$outputBase/ios/Classes/$pascalCaseName.gen.swift',
swiftErrorClassName: swiftErrorClassName,
swiftIncludeErrorClass: input != 'core_tests',
// Linux
gobjectHeaderOut: skipLanguages.contains(GeneratorLanguage.gobject)
? null
Expand Down Expand Up @@ -148,6 +150,7 @@ Future<int> generateTestPigeons(
? null
: '$outputBase/macos/Classes/$pascalCaseName.gen.swift',
swiftErrorClassName: swiftErrorClassName,
swiftIncludeErrorClass: input != 'core_tests',
suppressVersion: true,
dartPackageName: 'pigeon_integration_tests',
injectOverflowTypes: includeOverflow && input == 'core_tests',
Expand Down Expand Up @@ -212,6 +215,7 @@ Future<int> runPigeon({
String? kotlinPackage,
String? kotlinErrorClassName,
bool kotlinIncludeErrorClass = true,
bool swiftIncludeErrorClass = true,
String? swiftOut,
String? swiftErrorClassName,
String? cppHeaderOut,
Expand Down Expand Up @@ -272,6 +276,7 @@ Future<int> runPigeon({
swiftOut: swiftOut,
swiftOptions: SwiftOptions(
errorClassName: swiftErrorClassName,
includeErrorClass: swiftIncludeErrorClass,
),
basePath: basePath,
dartPackageName: dartPackageName,
Expand Down