Prompt plugin authors to add Swift Package Manager compatibility to their plugin#182246
Prompt plugin authors to add Swift Package Manager compatibility to their plugin#182246okorohelijah wants to merge 10 commits intoflutter:masterfrom
Conversation
|
The doc for migrating to spm doesnt seem to include adding the FlutterFramework dependency. I plan on updating it |
There was a problem hiding this comment.
Code Review
This pull request introduces a validation check to prompt plugin authors to add Swift Package Manager (SPM) compatibility for their iOS and macOS plugins. The check runs when refreshPluginsList is called for a plugin's example app. It verifies if a plugin with a podspec also has a Package.swift file, and if so, whether it correctly depends on FlutterFramework. Warnings are displayed to guide plugin authors on how to add or fix their SPM support. The changes include new logic in flutter_plugins.dart and plugins.dart, along with corresponding unit tests.
vashworth
left a comment
There was a problem hiding this comment.
Can you move all logic into DarwinDependencyManagement?
There was a problem hiding this comment.
I think this could be simplified by adding the warning to _evaluatePluginsAndPrintWarnings, which already checks if the plugin is SwiftPM/CocoaPods compatible.
I'd do something like
Future<({int totalCount, int swiftPackageCount, int podCount})> _evaluatePluginsAndPrintWarnings({
...
}) async {
String? pluginFromExampleApp = _loadPluginFromExampleProject(...);
for (final Plugin plugin in _plugins) {
...
if (!swiftPackageManagerCompatible && !cocoaPodsCompatible) {
continue;
}
if (plugin.name == pluginFromExampleApp) {
if (swiftPackageManagerCompatible && !_hasFlutterFrameworkDependency()) {
_logger.printWarning(...)
} else if (!swiftPackageManagerCompatible) {
_logger.printWarning(...)
}
}
}
}There was a problem hiding this comment.
_evaluatePluginsAndPrintWarnings operates on the app's dependency plugins during every build and targets app developers while the validation only fires for plugin example apps, targets the parent plugin being developed, and does a deeper content analysis of the Package.swift (checking for FlutterFramework dependency declarations while filtering out comments). I thought merging them could be be a mix of 2 different things so I kept them separate for clarity wdyt?
There was a problem hiding this comment.
If you prefer to separate the call path, maybe worth creating a common helper function to be called by both path?
There was a problem hiding this comment.
I agree with @hellohuanlin. If we're going to keep them separate, let's move the common logic into a helper function
| ' .package(name: "FlutterFramework", path: "../FlutterFramework")\n' | ||
| 'And add FlutterFramework as a target dependency:\n' | ||
| ' .product(name: "FlutterFramework", package: "FlutterFramework")\n' | ||
| 'See $kSwiftPackageManagerDocsUrl for more information.', |
There was a problem hiding this comment.
Let's wait for flutter/website#12979 to land, so we can point to the docs
There was a problem hiding this comment.
If you prefer to separate the call path, maybe worth creating a common helper function to be called by both path?
| @@ -129,6 +129,9 @@ class DarwinDependencyManagement { | |||
| ); | |||
|
|
|||
| _analytics.send(event); | |||
|
|
|||
| // Validate Swift Package Manager support for plugin example apps and print warning if it doesn't support Swift Package Manager. | |||
| _validatePluginSwiftPackageManagerSupport(platform: platform); | |||
There was a problem hiding this comment.
nit: it's unclear from the function name that this is for plugin's example app, rather than a regular app. How about something like _validatePluginSwiftPMSupportViaPluginExampleApp
There was a problem hiding this comment.
Or _validateExampleAppPluginSupportsSwiftPackageManager
|
|
||
| /// Tracks which plugin/platform combinations have already been warned about | ||
| /// during this session to avoid duplicate warnings. | ||
| static final Set<String> _spmValidationWarningsShown = <String>{}; |
There was a problem hiding this comment.
- Worth commenting how duplicate can happen. (i'm also curious)
- why static?
- let's be consistent and pick one among SPM vs SwiftPM vs SwiftPackageManager naming
There was a problem hiding this comment.
I don't think this is needed
| return; | ||
| } | ||
|
|
||
| final cacheKey = '${parentPlugin.name}:${platform.name}'; |
There was a problem hiding this comment.
uber nit: cacheKey sounds like you are fetching something from the cache. How about something like dedupKey
| /// | ||
| /// This looks for common patterns used to declare a FlutterFramework dependency: | ||
| /// - `.package(name: "FlutterFramework", path: "../FlutterFramework")` - package dependency | ||
| /// - `.product(name: "FlutterFramework", package: "FlutterFramework")` - target dependency |
There was a problem hiding this comment.
I'm not familiar with SwiftPM, but can target dependency be just a string (if no naming conflict)? Something like:
dependencies: [
"FlutterFramework",
]
| class SwiftPackageManagerPluginValidationResult { | ||
| SwiftPackageManagerPluginValidationResult({ | ||
| required this.hasPodspec, | ||
| required this.hasPackageSwift, |
There was a problem hiding this comment.
Nit: I assume this refers to "Package.swift" file? How about hasSwiftPMManifest?
| final bool hasFlutterFrameworkDependency; | ||
| final List<String> validationMessages; | ||
|
|
||
| bool get isFullyCompatible => hasPackageSwift && hasFlutterFrameworkDependency; |
There was a problem hiding this comment.
what does "fully compatible" mean? (and what is "partially" compatible? the ones without
"FlutterFramework" dependency?
There was a problem hiding this comment.
I agree with @hellohuanlin. If we're going to keep them separate, let's move the common logic into a helper function
| @@ -129,6 +129,9 @@ class DarwinDependencyManagement { | |||
| ); | |||
|
|
|||
| _analytics.send(event); | |||
|
|
|||
| // Validate Swift Package Manager support for plugin example apps and print warning if it doesn't support Swift Package Manager. | |||
| _validatePluginSwiftPackageManagerSupport(platform: platform); | |||
There was a problem hiding this comment.
Or _validateExampleAppPluginSupportsSwiftPackageManager
|
|
||
| /// Tracks which plugin/platform combinations have already been warned about | ||
| /// during this session to avoid duplicate warnings. | ||
| static final Set<String> _spmValidationWarningsShown = <String>{}; |
There was a problem hiding this comment.
I don't think this is needed
| /// Checks if the current project is a plugin example app and validates | ||
| /// the parent plugin's Swift Package Manager compatibility: | ||
| /// 1. If the plugin has a podspec but no Package.swift, prompts the user to | ||
| /// add SPM support. |
There was a problem hiding this comment.
| /// add SPM support. | |
| /// add SwiftPM support. |
| /// during this session to avoid duplicate warnings. | ||
| static final Set<String> _spmValidationWarningsShown = <String>{}; | ||
|
|
||
| /// Validates Swift Package Manager support for plugins in the current project. |
There was a problem hiding this comment.
| /// Validates Swift Package Manager support for plugins in the current project. | |
| /// Validates Swift Package Manager support for the plugin when building it's example app. |
| /// 2. If the plugin has a Package.swift, validates that it has a dependency | ||
| /// on FlutterFramework. | ||
| /// | ||
| /// Warnings are printed to inform plugin authors about SPM compatibility issues. |
There was a problem hiding this comment.
| /// Warnings are printed to inform plugin authors about SPM compatibility issues. | |
| /// Warnings are printed to inform plugin authors about SwiftPM compatibility issues. |
| final Directory projectDir = _project.directory; | ||
| if (!projectDir.path.endsWith('example')) { | ||
| return; | ||
| } | ||
|
|
||
| final Directory parentDir = projectDir.parent; | ||
| final File parentPubspec = parentDir.childFile('pubspec.yaml'); | ||
| if (!parentPubspec.existsSync()) { | ||
| return; | ||
| } | ||
|
|
||
| final FlutterProject parentProject; | ||
| try { | ||
| parentProject = FlutterProject.fromDirectory(parentDir); | ||
| } on Exception catch (e) { | ||
| _logger.printTrace('Failed to parse parent project for SPM validation: $e'); | ||
| return; | ||
| } | ||
|
|
||
| if (!parentProject.isPlugin) { | ||
| return; | ||
| } | ||
|
|
||
| final Plugin? parentPlugin = _plugins | ||
| .where((Plugin p) => p.name == parentProject.manifest.appName) | ||
| .firstOrNull; | ||
|
|
||
| if (parentPlugin == null) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
I'd move this logic into it's own method so it's clear what it's doing. For example, something like
Plugin? pluginFromExampleApp = _loadPluginFromExampleProject(...);| if (result.validationMessages.isNotEmpty) { | ||
| _spmValidationWarningsShown.add(cacheKey); | ||
| result.validationMessages.forEach(_logger.printWarning); | ||
| } |
There was a problem hiding this comment.
validationMessages does not need to be a List, it can just be a string since there is only ever one
| return; | ||
| } | ||
|
|
||
| final SwiftPackageManagerPluginValidationResult result = |
There was a problem hiding this comment.
Nothing out of this result is used except the validationMessages. Instead of returning a class, just return a String
| // Remove both single-line (//) and block (/* ... */) comments. | ||
| final String uncommentedContents = contents.replaceAll(RegExp(r'//.*|/\*[\s\S]*?\*/'), ''); | ||
|
|
||
| final bool hasPackageDependency = uncommentedContents.contains( | ||
| RegExp(r'\.package\s*\(\s*name\s*:\s*"FlutterFramework"'), | ||
| ); | ||
| final bool hasTargetDependency = uncommentedContents.contains( | ||
| RegExp(r'\.product\s*\(\s*name\s*:\s*"FlutterFramework"'), | ||
| ); |
There was a problem hiding this comment.
I'd rather make this check overly generous rather than have potential false positives. Let's just check if the Package.swift contains the string FlutterFramework.
Prompt plugin authors to add Swift Package Manager compatibility to their plugin
Fixes #148222
If you had to change anything in the flutter/tests repo, include a link to the migration guide as per the breaking change policy.
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot 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.