Skip to content

Commit 9c8b1f2

Browse files
okorohelijahmboetger
authored andcommitted
Prompt plugin authors to add Swift Package Manager compatibility to their plugin (flutter#182246)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> Prompt plugin authors to add Swift Package Manager compatibility to their plugin Fixes flutter#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 - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [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 documentation (doc comments with `///`). - [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]. **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 [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 [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 b256de7 commit 9c8b1f2

2 files changed

Lines changed: 576 additions & 0 deletions

File tree

‎packages/flutter_tools/lib/src/macos/darwin_dependency_management.dart‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import '../base/logger.dart';
1010
import '../base/platform.dart';
1111
import '../darwin/darwin.dart';
1212
import '../features.dart';
13+
import '../flutter_manifest.dart';
1314
import '../plugins.dart';
1415
import '../project.dart';
1516
import 'cocoapods.dart';
@@ -129,6 +130,9 @@ class DarwinDependencyManagement {
129130
);
130131

131132
_analytics.send(event);
133+
134+
// Validate SwiftPM support when building a plugin's example app.
135+
_validateExampleAppPluginSupportsSwiftPackageManager(platform: platform);
132136
}
133137

134138
/// Returns count of total number of plugins, number of Swift Package Manager compatible plugins,
@@ -283,4 +287,114 @@ class DarwinDependencyManagement {
283287

284288
return '';
285289
}
290+
291+
/// Validates that a plugin supports Swift Package Manager when building
292+
/// its example app.
293+
///
294+
/// If the current project is a plugin's example app, this checks whether
295+
/// the parent plugin has SwiftPM support:
296+
/// 1. If the plugin has a podspec but no Package.swift, prompts the author
297+
/// to add SwiftPM support.
298+
/// 2. If the plugin has a Package.swift, validates that it contains a
299+
/// dependency on FlutterFramework.
300+
void _validateExampleAppPluginSupportsSwiftPackageManager({
301+
required FlutterDarwinPlatform platform,
302+
}) {
303+
final Plugin? parentPlugin = _loadPluginFromExampleProject();
304+
if (parentPlugin == null) {
305+
return;
306+
}
307+
308+
final String? warning = validatePluginSupportsSwiftPackageManager(
309+
parentPlugin,
310+
fileSystem: _fileSystem,
311+
platform: platform.name,
312+
);
313+
314+
if (warning != null) {
315+
_logger.printWarning(warning);
316+
}
317+
}
318+
319+
/// Returns the parent plugin if the current project is a plugin's example app,
320+
/// or `null` otherwise.
321+
Plugin? _loadPluginFromExampleProject() {
322+
final Directory projectDir = _project.directory;
323+
if (!projectDir.path.endsWith('example')) {
324+
return null;
325+
}
326+
327+
final Directory parentDir = projectDir.parent;
328+
final File parentPubspec = parentDir.childFile('pubspec.yaml');
329+
if (!parentPubspec.existsSync()) {
330+
return null;
331+
}
332+
333+
final FlutterManifest? parentManifest;
334+
try {
335+
parentManifest = FlutterManifest.createFromPath(
336+
parentPubspec.path,
337+
fileSystem: _fileSystem,
338+
logger: _logger,
339+
);
340+
} on Exception catch (e) {
341+
_logger.printTrace('Failed to parse parent pubspec for SwiftPM validation: $e');
342+
return null;
343+
}
344+
345+
if (parentManifest == null || !parentManifest.isPlugin) {
346+
return null;
347+
}
348+
349+
final String pluginName = parentManifest.appName;
350+
return _plugins.where((Plugin p) => p.name == pluginName).firstOrNull;
351+
}
352+
353+
/// Validates a plugin's Swift Package Manager compatibility for a given [platform].
354+
///
355+
/// Returns a warning message if the plugin has SwiftPM compatibility issues,
356+
/// or `null` if the plugin is compatible or does not apply.
357+
///
358+
/// This checks:
359+
/// 1. If the plugin has a podspec but no Package.swift, it returns a message
360+
/// prompting the author to add SwiftPM support.
361+
/// 2. If the plugin has a Package.swift, it validates that it contains the
362+
/// string "FlutterFramework".
363+
static String? validatePluginSupportsSwiftPackageManager(
364+
Plugin plugin, {
365+
required FileSystem fileSystem,
366+
required String platform,
367+
}) {
368+
final String? podspecPath = plugin.pluginPodspecPath(fileSystem, platform);
369+
final String? packageSwiftPath = plugin.pluginSwiftPackageManifestPath(fileSystem, platform);
370+
371+
final bool hasPodspec = podspecPath != null && fileSystem.file(podspecPath).existsSync();
372+
373+
final bool hasPackageSwift =
374+
packageSwiftPath != null && fileSystem.file(packageSwiftPath).existsSync();
375+
376+
if (hasPodspec && !hasPackageSwift) {
377+
return 'Plugin ${plugin.name} does not have Swift Package Manager support for $platform. '
378+
'Consider adding Swift Package Manager compatibility to your plugin. '
379+
'See $kSwiftPackageManagerDocsUrl for more information.';
380+
}
381+
382+
if (hasPackageSwift) {
383+
final String contents = fileSystem.file(packageSwiftPath).readAsStringSync();
384+
if (!contents.contains('FlutterFramework')) {
385+
return 'Plugin ${plugin.name} has a Package.swift for $platform but is missing a dependency '
386+
'on FlutterFramework. Add the following to your Package.swift dependencies:\n'
387+
' .package(name: "FlutterFramework", path: "../FlutterFramework")\n'
388+
'And add FlutterFramework as a target dependency:\n'
389+
' .product(name: "FlutterFramework", package: "FlutterFramework")\n'
390+
'See $kSwiftPackageManagerDocsUrl for more information.';
391+
}
392+
}
393+
394+
return null;
395+
}
286396
}
397+
398+
/// The URL for documentation on adding Swift Package Manager support to a plugin.
399+
const String kSwiftPackageManagerDocsUrl =
400+
'https://docs.flutter.dev/packages-and-plugins/swift-package-manager/for-plugin-authors';

0 commit comments

Comments
 (0)