Skip to content

Commit c96c123

Browse files
Update the plugin template to use Pigeon (flutter#192886)
Flutter's plugin development documentation is being updated to describe Pigeon as the standard method of plugin development, rather than raw method channel use, and as part of that we want the `flutter create` plugin template to be set up using Pigeon, so that the starting point matches the documentation and the recommended development path. This takes the approach of checking in a templatized version of all of the Pigeon-generated files; this has the advantage of not needing to run any additional commands, but does mean that updating Pigeon in the templates will require updating all of the generated files with the diffs (which can be done by snapshotting a fresh `flutter create -t plugin`, updating Pigeon, running generation, then porting those diffs back into the templates). If this turns out to be onerous in practice, this could be replaced later with updating the `create` flow in the `flutter` tool to automatically run `dart run pigeon --input pigeon/messages.dart` after creation (although this will make `create` slightly slower). Also updates the iOS/macOS tests from XCTest to Swift Testing while I was in the files, to reflect current testing practices. Fixes flutter#63284 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 29a0614 commit c96c123

51 files changed

Lines changed: 2582 additions & 355 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎dev/bots/analyze.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,10 @@ Future<void> lintKotlinTemplatedFiles(String workingDirectory) async {
16301630
.listSync(recursive: true)
16311631
.toList()
16321632
.whereType<File>()
1633-
.where((File file) => _kKotlinExtList.contains(path.extension(file.path, 2)));
1633+
.where((File file) => _kKotlinExtList.contains(path.extension(file.path, 2)))
1634+
// Skip Pigeon-generated files, as they are intentionally not autoformatted
1635+
// to minimize diffs when developers re-run Pigeon generation.
1636+
.where((File file) => !path.basename(file.path).contains('.g.kt'));
16341637

16351638
if (files.isEmpty) {
16361639
foundError(<String>['No Kotlin template files found']);

‎packages/flutter_tools/lib/src/commands/create.dart‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,12 @@ class CreateCommand extends FlutterCommand with CreateBase, ExtensionArgParserMi
861861
_printWarningDisabledPlatform(platformsToWarn, logger: logger);
862862
}
863863
final template = generateMethodChannelsPlugin ? 'plugin' : 'plugin_ffi';
864-
_printPluginAddPlatformMessage(relativePluginPath, template, logger: logger);
864+
_printPluginAddPlatformMessage(
865+
relativePluginPath,
866+
template,
867+
logger: logger,
868+
usesPigeon: generateMethodChannelsPlugin,
869+
);
865870
} else {
866871
// Tell the user the next steps.
867872
final FlutterProject project = projectFactory.fromDirectory(fs.directory(projectDirPath));
@@ -968,6 +973,8 @@ Your $application code is in $relativeAppMain.
968973
templateContext[platform] = false;
969974
}
970975
}
976+
templateContext['iosOrMacOS'] =
977+
(templateContext['ios'] as bool? ?? false) || (templateContext['macos'] as bool? ?? false);
971978
final List<String> platformsToAdd = _getSupportedPlatformsFromTemplateContext(templateContext);
972979

973980
final List<String> existingPlatforms = _getSupportedPlatformsInPlugin(
@@ -1346,9 +1353,15 @@ You've created a plugin project that doesn't yet support any platforms.
13461353
''');
13471354
}
13481355

1349-
void _printPluginAddPlatformMessage(String pluginPath, String template, {required Logger logger}) {
1356+
void _printPluginAddPlatformMessage(
1357+
String pluginPath,
1358+
String template, {
1359+
required Logger logger,
1360+
required bool usesPigeon,
1361+
}) {
13501362
logger.printStatus('''
1351-
To add platforms, run `flutter create -t $template --platforms <platforms> .` under $pluginPath.
1363+
To add platforms, run `flutter create -t $template --platforms <platforms> .` under $pluginPath,
1364+
then update ${usesPigeon ? 'pigeon/messages.dart and ' : ''}pubspec.yaml to include the new platforms.
13521365
For more information, see https://flutter.dev/to/pubspec-plugin-platforms.
13531366
13541367
''');

‎packages/flutter_tools/lib/src/commands/create_base.dart‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,10 @@ mixin CreateBase on FlutterCommand {
370370
bool implementationTests = false,
371371
}) {
372372
final FlutterVersion flutterVersion = _context.flutterVersion;
373-
final String pluginDartClass = _createPluginClassName(projectName);
374-
final pluginClass = pluginDartClass.endsWith('Plugin')
375-
? pluginDartClass
376-
: '${pluginDartClass}Plugin';
373+
final String projectNameUpperCamelCase = _createPluginClassName(projectName);
374+
final pluginClass = projectNameUpperCamelCase.endsWith('Plugin')
375+
? projectNameUpperCamelCase
376+
: '${projectNameUpperCamelCase}Plugin';
377377
final String pluginClassSnakeCase = snakeCase(pluginClass);
378378
final String pluginClassCapitalSnakeCase = pluginClassSnakeCase.toUpperCase();
379379
final String pluginClassLowerCamelCase =
@@ -388,8 +388,11 @@ mixin CreateBase on FlutterCommand {
388388
return <String, Object?>{
389389
'organization': organization,
390390
'projectName': projectName,
391+
'projectNameUpperCamelCase': projectNameUpperCamelCase,
392+
'projectNameCapitalSnakeCase': projectName.toUpperCase(),
391393
'titleCaseProjectName': titleCaseProjectName,
392394
'androidIdentifier': androidIdentifier,
395+
'androidIdentifierPosixPath': androidIdentifier.replaceAll('.', '/'),
393396
'iosIdentifier': appleIdentifier,
394397
'macosIdentifier': appleIdentifier,
395398
'darwinIdentifier': appleIdentifier,
@@ -403,7 +406,7 @@ mixin CreateBase on FlutterCommand {
403406
'pluginClassSnakeCase': pluginClassSnakeCase,
404407
'pluginClassLowerCamelCase': pluginClassLowerCamelCase,
405408
'pluginClassCapitalSnakeCase': pluginClassCapitalSnakeCase,
406-
'pluginDartClass': pluginDartClass,
409+
'pluginDartClass': projectNameUpperCamelCase,
407410
'pluginProjectUUID': const Uuid().v4().toUpperCase(),
408411
'withFfi': withFfiPluginHook || withFfiPackage,
409412
'withFfiPackage': withFfiPackage,
@@ -412,6 +415,7 @@ mixin CreateBase on FlutterCommand {
412415
'withSwiftPackageManager': withSwiftPackageManager,
413416
'withPluginHook': withFfiPluginHook || withFfiPackage || withPlatformChannelPluginHook,
414417
'withEmptyMain': withEmptyMain,
418+
'withJava': androidLanguage == 'java',
415419
'androidLanguage': androidLanguage,
416420
'hasIosDevelopmentTeam': iosDevelopmentTeam != null && iosDevelopmentTeam.isNotEmpty,
417421
'iosDevelopmentTeam': iosDevelopmentTeam ?? '',

‎packages/flutter_tools/templates/app/ios.tmpl/RunnerTests/RunnerTests.swift.tmpl‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Flutter
2+
import Testing
23
import UIKit
3-
import XCTest
44
{{#withPlatformChannelPluginHook}}
55

66
{{#withSwiftPackageManager}}
@@ -12,29 +12,25 @@ import XCTest
1212

1313
// This demonstrates a simple unit test of the Swift portion of this plugin's implementation.
1414
//
15-
// See https://developer.apple.com/documentation/xctest for more information about using XCTest.
15+
// See https://developer.apple.com/xcode/swift-testing/ for more information about using
16+
// Swift Testing.
1617
{{/withPlatformChannelPluginHook}}
1718

18-
class RunnerTests: XCTestCase {
19+
struct RunnerTests {
1920

2021
{{#withPlatformChannelPluginHook}}
21-
func testGetPlatformVersion() {
22+
@Test func getPlatformVersionSucceeds() throws {
2223
let plugin = {{pluginClass}}()
2324

24-
let call = FlutterMethodCall(methodName: "getPlatformVersion", arguments: [])
25-
26-
let resultExpectation = expectation(description: "result block must be called.")
27-
plugin.handle(call) { result in
28-
XCTAssertEqual(result as! String, "iOS " + UIDevice.current.systemVersion)
29-
resultExpectation.fulfill()
30-
}
31-
waitForExpectations(timeout: 1)
25+
let result = try plugin.getPlatformVersion()
26+
#expect(result == "iOS " + UIDevice.current.systemVersion)
3227
}
3328
{{/withPlatformChannelPluginHook}}
3429
{{^withPlatformChannelPluginHook}}
3530
func testExample() {
3631
// If you add code to the Runner application, consider adding tests here.
37-
// See https://developer.apple.com/documentation/xctest for more information about using XCTest.
32+
// See https://developer.apple.com/xcode/swift-testing/ for more information about using
33+
// Swift Testing.
3834
}
3935
{{/withPlatformChannelPluginHook}}
4036

‎packages/flutter_tools/templates/app/macos.tmpl/RunnerTests/RunnerTests.swift.tmpl‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Cocoa
22
import FlutterMacOS
3-
import XCTest
3+
import Testing
44
{{#withPlatformChannelPluginHook}}
55

66
{{#withSwiftPackageManager}}
@@ -12,30 +12,25 @@ import XCTest
1212

1313
// This demonstrates a simple unit test of the Swift portion of this plugin's implementation.
1414
//
15-
// See https://developer.apple.com/documentation/xctest for more information about using XCTest.
15+
// See https://developer.apple.com/xcode/swift-testing/ for more information about using
16+
// Swift Testing.
1617
{{/withPlatformChannelPluginHook}}
1718

18-
class RunnerTests: XCTestCase {
19+
struct RunnerTests {
1920

2021
{{#withPlatformChannelPluginHook}}
21-
func testGetPlatformVersion() {
22+
@Test func getPlatformVersionSucceeds() throws {
2223
let plugin = {{pluginClass}}()
2324

24-
let call = FlutterMethodCall(methodName: "getPlatformVersion", arguments: [])
25-
26-
let resultExpectation = expectation(description: "result block must be called.")
27-
plugin.handle(call) { result in
28-
XCTAssertEqual(result as! String,
29-
"macOS " + ProcessInfo.processInfo.operatingSystemVersionString)
30-
resultExpectation.fulfill()
31-
}
32-
waitForExpectations(timeout: 1)
25+
let result = try plugin.getPlatformVersion()
26+
#expect(result == "macOS " + ProcessInfo.processInfo.operatingSystemVersionString)
3327
}
3428
{{/withPlatformChannelPluginHook}}
3529
{{^withPlatformChannelPluginHook}}
3630
func testExample() {
3731
// If you add code to the Runner application, consider adding tests here.
38-
// See https://developer.apple.com/documentation/xctest for more information about using XCTest.
32+
// See https://developer.apple.com/xcode/swift-testing/ for more information about using
33+
// Swift Testing.
3934
}
4035
{{/withPlatformChannelPluginHook}}
4136

‎packages/flutter_tools/templates/plugin/README.md.tmpl‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ samples, guidance on mobile development, and a full API reference.
1515

1616
{{#no_platforms}}
1717
The plugin project was generated without specifying the `--platforms` flag, no platforms are currently supported.
18-
To add platforms, run `flutter create -t plugin --platforms <platforms> .` in this directory.
18+
To add platforms, run `flutter create -t plugin --platforms <platforms> .` in this directory,
19+
then update pigeon/messages.dart and pubspec.yaml to include the new platforms.
1920
You can also find a detailed instruction on how to add platforms in the `pubspec.yaml` at https://flutter.dev/to/pubspec-plugin-platforms.
2021
{{/no_platforms}}

‎packages/flutter_tools/templates/plugin/android-java.tmpl/build.gradle.kts.tmpl‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,4 @@ android {
5151

5252
dependencies {
5353
testImplementation("junit:junit:4.13.2")
54-
testImplementation("org.mockito:mockito-core:5.0.0")
5554
}

0 commit comments

Comments
 (0)