Skip to content

Commit

Permalink
add import fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bparrishMines committed Dec 22, 2024
1 parent 3515aba commit ca772e1
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 8 deletions.
46 changes: 38 additions & 8 deletions packages/pigeon/lib/swift_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,7 @@ class SwiftGenerator extends StructuredGenerator<SwiftOptions> {
}) {
indent.writeln('import Foundation');

final Iterable<String> proxyApiImports = root.apis
.whereType<AstProxyApi>()
.map((AstProxyApi proxyApi) => proxyApi.swiftOptions?.import)
.nonNulls
.toSet();
for (final String import in proxyApiImports) {
indent.writeln('import $import');
}
_writeProxyApiImports(indent, root.apis.whereType<AstProxyApi>());
indent.newln();

indent.format('''
Expand Down Expand Up @@ -1155,6 +1148,9 @@ if (wrapped == nil) {
final String swiftApiProtocolName =
'${hostProxyApiPrefix}Protocol${api.name}';
indent.writeScoped('protocol $swiftApiProtocolName {', '}', () {
indent.writeln(
'var pigeonRegistrar: ${proxyApiRegistrarName(generatorOptions)} { get }',
);
_writeProxyApiFlutterMethods(
indent,
api,
Expand Down Expand Up @@ -2487,6 +2483,40 @@ private func nilOrValue<T>(_ value: Any?) -> T? {
});
});
}

void _writeProxyApiImports(Indent indent, Iterable<AstProxyApi> apis) {
final Map<String, List<AstProxyApi>> apisOfImports =
<String, List<AstProxyApi>>{};
for (final AstProxyApi proxyApi in apis) {
final String? import = proxyApi.swiftOptions?.import;
if (import != null) {
if (apisOfImports.containsKey(import)) {
apisOfImports[import]!.add(proxyApi);
} else {
apisOfImports[import] = <AstProxyApi>[proxyApi];
}
}
}

for (final String import in apisOfImports.keys) {
final List<String> unsupportedPlatforms = <String>[
if (!apisOfImports[import]!
.every((AstProxyApi api) => api.swiftOptions?.supportsIos ?? true))
'!os(iOS)',
if (!apisOfImports[import]!.every(
(AstProxyApi api) => api.swiftOptions?.supportsMacos ?? true))
'!os(macOS)',
];

if (unsupportedPlatforms.isNotEmpty) {
indent.writeln('#if ${unsupportedPlatforms.join(' || ')}');
}
indent.writeln('import $import');
if (unsupportedPlatforms.isNotEmpty) {
indent.writeln('#endif');
}
}
}
}

typedef _VersionRequirement = ({TypeDeclaration type, Version version});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,7 @@ protocol PigeonApiDelegateProxyApiTestClass {
}

protocol PigeonApiProtocolProxyApiTestClass {
var pigeonRegistrar: ProxyApiTestsPigeonProxyApiRegistrar { get }
/// A no-op function taking no arguments and returning no value, to sanity
/// test basic calling.
func flutterNoop(
Expand Down Expand Up @@ -3895,6 +3896,7 @@ protocol PigeonApiDelegateProxyApiSuperClass {
}

protocol PigeonApiProtocolProxyApiSuperClass {
var pigeonRegistrar: ProxyApiTestsPigeonProxyApiRegistrar { get }
}

final class PigeonApiProxyApiSuperClass: PigeonApiProtocolProxyApiSuperClass {
Expand Down Expand Up @@ -4000,6 +4002,7 @@ open class PigeonApiDelegateProxyApiInterface {
}

protocol PigeonApiProtocolProxyApiInterface {
var pigeonRegistrar: ProxyApiTestsPigeonProxyApiRegistrar { get }
func anInterfaceMethod(
pigeonInstance pigeonInstanceArg: ProxyApiInterface,
completion: @escaping (Result<Void, ProxyApiTestsError>) -> Void)
Expand Down Expand Up @@ -4100,6 +4103,7 @@ protocol PigeonApiDelegateClassWithApiRequirement {
}

protocol PigeonApiProtocolClassWithApiRequirement {
var pigeonRegistrar: ProxyApiTestsPigeonProxyApiRegistrar { get }
}

final class PigeonApiClassWithApiRequirement: PigeonApiProtocolClassWithApiRequirement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,7 @@ protocol PigeonApiDelegateProxyApiTestClass {
}

protocol PigeonApiProtocolProxyApiTestClass {
var pigeonRegistrar: ProxyApiTestsPigeonProxyApiRegistrar { get }
/// A no-op function taking no arguments and returning no value, to sanity
/// test basic calling.
func flutterNoop(
Expand Down Expand Up @@ -3895,6 +3896,7 @@ protocol PigeonApiDelegateProxyApiSuperClass {
}

protocol PigeonApiProtocolProxyApiSuperClass {
var pigeonRegistrar: ProxyApiTestsPigeonProxyApiRegistrar { get }
}

final class PigeonApiProxyApiSuperClass: PigeonApiProtocolProxyApiSuperClass {
Expand Down Expand Up @@ -4000,6 +4002,7 @@ open class PigeonApiDelegateProxyApiInterface {
}

protocol PigeonApiProtocolProxyApiInterface {
var pigeonRegistrar: ProxyApiTestsPigeonProxyApiRegistrar { get }
func anInterfaceMethod(
pigeonInstance pigeonInstanceArg: ProxyApiInterface,
completion: @escaping (Result<Void, ProxyApiTestsError>) -> Void)
Expand Down Expand Up @@ -4100,6 +4103,7 @@ protocol PigeonApiDelegateClassWithApiRequirement {
}

protocol PigeonApiProtocolClassWithApiRequirement {
var pigeonRegistrar: ProxyApiTestsPigeonProxyApiRegistrar { get }
}

final class PigeonApiClassWithApiRequirement: PigeonApiProtocolClassWithApiRequirement {
Expand Down
102 changes: 102 additions & 0 deletions packages/pigeon/test/swift/proxy_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ void main() {
// Delegate and class
expect(code, contains('protocol PigeonApiDelegateApi'));
expect(code, contains('protocol PigeonApiProtocolApi'));
expect(
code,
contains(
'var pigeonRegistrar: MyFilePigeonProxyApiRegistrar { get }',
),
);
expect(
code,
contains(
Expand Down Expand Up @@ -187,6 +193,102 @@ void main() {
);
});

group('imports', () {
test('add check if every class does not support iOS', () {
final Root root = Root(
apis: <Api>[
AstProxyApi(
name: 'Api',
swiftOptions: const SwiftProxyApiOptions(
import: 'MyImport',
supportsIos: false,
),
constructors: <Constructor>[],
fields: <ApiField>[],
methods: <Method>[],
),
],
classes: <Class>[],
enums: <Enum>[],
);
final StringBuffer sink = StringBuffer();
const SwiftGenerator generator = SwiftGenerator();
generator.generate(
const SwiftOptions(),
root,
sink,
dartPackageName: DEFAULT_PACKAGE_NAME,
);
final String code = sink.toString();

expect(code, contains('#if !os(iOS)\nimport MyImport\n#endif'));
});

test('add check if every class does not support macOS', () {
final Root root = Root(
apis: <Api>[
AstProxyApi(
name: 'Api',
swiftOptions: const SwiftProxyApiOptions(
import: 'MyImport',
supportsMacos: false,
),
constructors: <Constructor>[],
fields: <ApiField>[],
methods: <Method>[],
),
],
classes: <Class>[],
enums: <Enum>[],
);
final StringBuffer sink = StringBuffer();
const SwiftGenerator generator = SwiftGenerator();
generator.generate(
const SwiftOptions(),
root,
sink,
dartPackageName: DEFAULT_PACKAGE_NAME,
);
final String code = sink.toString();

expect(code, contains('#if !os(macOS)\nimport MyImport\n#endif'));
});

test('add check if for multiple unsupported platforms', () {
final Root root = Root(
apis: <Api>[
AstProxyApi(
name: 'Api',
swiftOptions: const SwiftProxyApiOptions(
import: 'MyImport',
supportsIos: false,
supportsMacos: false,
),
constructors: <Constructor>[],
fields: <ApiField>[],
methods: <Method>[],
),
],
classes: <Class>[],
enums: <Enum>[],
);
final StringBuffer sink = StringBuffer();
const SwiftGenerator generator = SwiftGenerator();
generator.generate(
const SwiftOptions(),
root,
sink,
dartPackageName: DEFAULT_PACKAGE_NAME,
);
final String code = sink.toString();

expect(
code,
contains('#if !os(iOS) || !os(macOS)\nimport MyImport\n#endif'),
);
});
});

group('inheritance', () {
test('extends', () {
final AstProxyApi api2 = AstProxyApi(
Expand Down

0 comments on commit ca772e1

Please sign in to comment.