Skip to content

Commit

Permalink
[pigeon] added macos swift support (#2587)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaaclarke authored Sep 12, 2022
1 parent 7d222a0 commit 7d21d59
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.0.3

* [swift] Makes swift output work on macOS.

## 4.0.2

* Fixes lint warnings.
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 @@ -9,7 +9,7 @@ import 'dart:mirrors';
import 'ast.dart';

/// The current version of pigeon. This must match the version in pubspec.yaml.
const String pigeonVersion = '4.0.2';
const String pigeonVersion = '4.0.3';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
10 changes: 9 additions & 1 deletion packages/pigeon/lib/swift_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,15 @@ void generateSwift(SwiftOptions options, Root root, StringSink sink) {

void writeImports() {
indent.writeln('import Foundation');
indent.writeln('import Flutter');
indent.format('''
#if os(iOS)
import Flutter
#elseif os(macOS)
import FlutterMacOS
#else
#error("Unsupported platform.")
#endif
''');
}

void writeEnum(Enum anEnum) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import Cocoa
import FlutterMacOS

public class MyApi: Api {
public init() {}
func getPlatform() -> String {
return "macOS " + ProcessInfo.processInfo.operatingSystemVersionString
}
}

public class MacosSwiftUnitTestsPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
ApiSetup.setUp(binaryMessenger: registrar.messenger, api: MyApi())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import XCTest
import macos_swift_unit_tests

class MessagesTest: XCTestCase {
func testMakeApi() {
let api = MyApi()
XCTAssertNotNil(api)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
# Run `pod lib lint macos_swift_unit_tests.podspec` to validate before publishing.
#
Pod::Spec.new do |s|
s.name = 'macos_swift_unit_tests'
s.version = '0.0.1'
s.summary = 'Fake library that uses pigeon for macos for testing.'
s.description = <<-DESC
Fake library that uses pigeon for macos for testing.
Use `pod lib lint` to run the tests.
DESC
s.homepage = 'http://example.com'
s.license = { :type => 'Flutter', :file => '../../../LICENSE' }
s.author = { 'Your Company' => 'email@example.com' }

s.source = { :http => 'https://github.com/flutter/packages' }
s.source_files = 'Classes/**/*'
s.dependency 'FlutterMacOS'

s.platform = :osx, '10.11'
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
s.swift_version = '5.0'

s.test_spec 'Tests' do |test_spec|
test_spec.source_files = 'Tests/*.{swift}'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:pigeon/pigeon.dart';

@HostApi()
abstract class Api {
String getPlatform();
}
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%3Apigeon
version: 4.0.2 # This must match the version in lib/generator_tools.dart
version: 4.0.3 # This must match the version in lib/generator_tools.dart

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
11 changes: 11 additions & 0 deletions packages/pigeon/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ run_mock_handler_tests() {
dart run tool/run_tests.dart -t mock_handler_tests
}

run_macos_swift_unittests() {
dart run tool/run_tests.dart -t mac_swift_unittests
}

run_dart_compilation_tests() {
local temp_dir=$(mktmpdir)
local flutter_project_dir=$temp_dir/project
Expand Down Expand Up @@ -311,6 +315,7 @@ should_run_flutter_unittests=true
should_run_ios_e2e_tests=true
should_run_ios_unittests=true
should_run_mock_handler_tests=true
should_run_macos_swift_unittests=true
while getopts "t:l?h" opt; do
case $opt in
t)
Expand All @@ -321,6 +326,7 @@ while getopts "t:l?h" opt; do
should_run_ios_e2e_tests=false
should_run_ios_unittests=false
should_run_mock_handler_tests=false
should_run_macos_swift_unittests=false
case $OPTARG in
android_unittests) should_run_android_unittests=true ;;
dart_compilation_tests) should_run_dart_compilation_tests=true ;;
Expand All @@ -329,6 +335,7 @@ while getopts "t:l?h" opt; do
ios_e2e_tests) should_run_ios_e2e_tests=true ;;
ios_unittests) should_run_ios_unittests=true ;;
mock_handler_tests) should_run_mock_handler_tests=true ;;
macos_swift_unittests) should_run_macos_swift_unittests=true ;;
*)
echo "unrecognized test: $OPTARG"
exit 1
Expand All @@ -344,6 +351,7 @@ while getopts "t:l?h" opt; do
ios_e2e_tests - End-to-end objc tests run on iOS Simulator
ios_unittests - Unit tests on generated Objc code.
mock_handler_tests - Unit tests on generated Dart mock handler code.
macos_swift_unittests - Unit tests on generated Swift code on macOS.
"
exit 1
;;
Expand Down Expand Up @@ -390,3 +398,6 @@ fi
if [ "$should_run_android_unittests" = true ]; then
run_android_unittests
fi
if [ "$should_run_macos_swift_unittests" = true ]; then
run_macos_swift_unittests
fi
39 changes: 38 additions & 1 deletion packages/pigeon/tool/run_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@
///
/// usage: dart run tool/run_tests.dart
////////////////////////////////////////////////////////////////////////////////
import 'dart:io' show File, Platform, Process, exit, stderr, stdout;
import 'dart:io'
show
Directory,
File,
Platform,
Process,
ProcessResult,
exit,
stderr,
stdout;
import 'package:args/args.dart';
import 'package:meta/meta.dart';
import 'package:pigeon/functional.dart';
Expand Down Expand Up @@ -49,6 +58,9 @@ const Map<String, _TestInfo> _tests = <String, _TestInfo>{
'ios_swift_unittests': _TestInfo(
function: _runIosSwiftUnitTests,
description: 'Unit tests on generated Swift code.'),
'mac_swift_unittests': _TestInfo(
function: _runMacOSSwiftUnitTests,
description: 'Unit tests on generated Swift code on macOS.'),
'mock_handler_tests': _TestInfo(
function: _runMockHandlerTests,
description: 'Unit tests on generated Dart mock handler code.'),
Expand Down Expand Up @@ -190,6 +202,31 @@ Future<int> _runIosUnitTests() async {
throw UnimplementedError('See run_tests.sh.');
}

Future<int> _runMacOSSwiftUnitTests() async {
const String macosSwiftUnitTestsPath =
'./platform_tests/macos_swift_unit_tests';
final int generateCode = await _runPigeon(
input: '$macosSwiftUnitTestsPath/pigeons/messages.dart',
iosSwiftOut: '$macosSwiftUnitTestsPath/macos/Classes/messages.g.swift',
);
if (generateCode != 0) {
return generateCode;
}
final Directory oldCwd = Directory.current;
try {
Directory.current = Directory('$macosSwiftUnitTestsPath/macos');
final ProcessResult lintResult =
Process.runSync('pod', <String>['lib', 'lint']);
if (lintResult.exitCode != 0) {
return lintResult.exitCode;
}
} finally {
Directory.current = oldCwd;
}

return 0;
}

Future<int> _runIosSwiftUnitTests() async {
const String iosSwiftUnitTestsPath = './platform_tests/ios_swift_unit_tests';
const List<String> tests = <String>[
Expand Down

0 comments on commit 7d21d59

Please sign in to comment.