[SwiftPM] Move the logic for SwiftPM enablement to the platform project (#158213)

In the future, it will be possible for Swift Package Manager to be enabled on one but not all platforms (see https://github.com/flutter/flutter/issues/151567#issuecomment-2455941279).

This moves the `usesSwiftPackageManager` property from the platform-agnostic `Project` to the platform-specific `IosProject` and `MacOSProject`.

This will allow the `IosProject` and `MacOSProject` to return different values for `usesSwiftPackageManager` in the future. For now, both of these projects will always return the same value.

Part of https://github.com/flutter/flutter/issues/151567
This commit is contained in:
Loïc Sharma
2024-11-13 13:45:07 -08:00
committed by GitHub
parent 5a9c2b4dd1
commit 8a55c941cf
14 changed files with 341 additions and 166 deletions

View File

@@ -1071,12 +1071,22 @@ Future<void> refreshPluginsList(
// Write the legacy plugin files to avoid breaking existing apps.
final bool legacyChanged = useImplicitPubspecResolution && _writeFlutterPluginsListLegacy(project, plugins);
final bool swiftPackageManagerEnabled = !forceCocoaPodsOnly && project.usesSwiftPackageManager;
bool swiftPackageManagerEnabledIos = false;
bool swiftPackageManagerEnabledMacos = false;
if (!forceCocoaPodsOnly) {
if (iosPlatform) {
swiftPackageManagerEnabledIos = project.ios.usesSwiftPackageManager;
}
if (macOSPlatform) {
swiftPackageManagerEnabledMacos = project.macos.usesSwiftPackageManager;
}
}
final bool changed = _writeFlutterPluginsList(
project,
plugins,
swiftPackageManagerEnabledIos: swiftPackageManagerEnabled,
swiftPackageManagerEnabledMacos: swiftPackageManagerEnabled,
swiftPackageManagerEnabledIos: swiftPackageManagerEnabledIos,
swiftPackageManagerEnabledMacos: swiftPackageManagerEnabledMacos,
);
if (changed || legacyChanged || forceCocoaPodsOnly) {
createPluginSymlinks(project, force: true);

View File

@@ -166,7 +166,7 @@ Future<XcodeBuildResult> buildXcodeProject({
RemoveBitcodeMigration(app.project, globals.logger),
XcodeThinBinaryBuildPhaseInputPathsMigration(app.project, globals.logger),
UIApplicationMainDeprecationMigration(app.project, globals.logger),
if (project.usesSwiftPackageManager && app.project.flutterPluginSwiftPackageManifest.existsSync())
if (app.project.usesSwiftPackageManager && app.project.flutterPluginSwiftPackageManifest.existsSync())
SwiftPackageManagerIntegrationMigration(
app.project,
SupportedPlatform.ios,
@@ -267,7 +267,7 @@ Future<XcodeBuildResult> buildXcodeProject({
targetOverride: targetOverride,
buildInfo: buildInfo,
);
if (project.usesSwiftPackageManager) {
if (app.project.usesSwiftPackageManager) {
final String? iosDeploymentTarget = buildSettings['IPHONEOS_DEPLOYMENT_TARGET'];
if (iosDeploymentTarget != null) {
SwiftPackageManager.updateMinimumDeployment(
@@ -888,7 +888,7 @@ Future<bool> _handleIssues(
logger.printError(missingPlatformInstructions(missingPlatform), emphasis: true);
} else if (duplicateModules.isNotEmpty) {
final bool usesCocoapods = xcodeProject.podfile.existsSync();
final bool usesSwiftPackageManager = project.usesSwiftPackageManager;
final bool usesSwiftPackageManager = xcodeProject.usesSwiftPackageManager;
if (usesCocoapods && usesSwiftPackageManager) {
logger.printError(
'Your project uses both CocoaPods and Swift Package Manager, which can '
@@ -906,7 +906,7 @@ Future<bool> _handleIssues(
}
} else if (missingModules.isNotEmpty) {
final bool usesCocoapods = xcodeProject.podfile.existsSync();
final bool usesSwiftPackageManager = project.usesSwiftPackageManager;
final bool usesSwiftPackageManager = xcodeProject.usesSwiftPackageManager;
if (usesCocoapods && !usesSwiftPackageManager) {
final List<String> swiftPackageOnlyPlugins = <String>[];
for (final String module in missingModules) {

View File

@@ -91,7 +91,7 @@ Future<void> buildMacOS({
FlutterApplicationMigration(flutterProject.macos, globals.logger),
NSApplicationMainDeprecationMigration(flutterProject.macos, globals.logger),
SecureRestorableStateMigration(flutterProject.macos, globals.logger),
if (flutterProject.usesSwiftPackageManager && flutterProject.macos.flutterPluginSwiftPackageManifest.existsSync())
if (flutterProject.macos.usesSwiftPackageManager && flutterProject.macos.flutterPluginSwiftPackageManifest.existsSync())
SwiftPackageManagerIntegrationMigration(
flutterProject.macos,
SupportedPlatform.macos,
@@ -145,7 +145,7 @@ Future<void> buildMacOS({
useMacOSConfig: true,
);
if (flutterProject.usesSwiftPackageManager) {
if (flutterProject.macos.usesSwiftPackageManager) {
final String? macOSDeploymentTarget = buildSettings['MACOSX_DEPLOYMENT_TARGET'];
if (macOSDeploymentTarget != null) {
SwiftPackageManager.updateMinimumDeployment(

View File

@@ -22,7 +22,7 @@ Future<void> processPodsIfNeeded(
// When using Swift Package Manager, the Podfile may not exist so if there
// isn't a Podfile, skip processing pods.
if (project.usesSwiftPackageManager &&
if (xcodeProject.usesSwiftPackageManager &&
!xcodeProject.podfile.existsSync() &&
!forceCocoaPodsOnly) {
return;
@@ -50,7 +50,7 @@ Future<void> processPodsIfNeeded(
// If forcing the use of only CocoaPods, but the project is using Swift
// Package Manager, print a warning that CocoaPods will be used.
if (forceCocoaPodsOnly && project.usesSwiftPackageManager) {
if (forceCocoaPodsOnly && xcodeProject.usesSwiftPackageManager) {
globals.logger.printWarning(
'Swift Package Manager does not yet support this command. '
'CocoaPods will be used instead.');

View File

@@ -167,7 +167,7 @@ class CocoaPods {
}) async {
if (!xcodeProject.podfile.existsSync()) {
// Swift Package Manager doesn't need Podfile, so don't error.
if (xcodeProject.parent.usesSwiftPackageManager) {
if (xcodeProject.usesSwiftPackageManager) {
return false;
}
throwToolExit('Podfile missing');

View File

@@ -60,7 +60,7 @@ class DarwinDependencyManagement {
final XcodeBasedProject xcodeProject = platform == SupportedPlatform.ios
? _project.ios
: _project.macos;
if (_project.usesSwiftPackageManager) {
if (xcodeProject.usesSwiftPackageManager) {
await _swiftPackageManager.generatePluginsSwiftPackage(
_plugins,
platform,
@@ -89,7 +89,7 @@ class DarwinDependencyManagement {
);
final bool useCocoapods;
if (_project.usesSwiftPackageManager) {
if (xcodeProject.usesSwiftPackageManager) {
useCocoapods = swiftPackageCount < totalCount;
} else {
// When Swift Package Manager is not enabled, set up Podfile if plugins
@@ -161,7 +161,7 @@ class DarwinDependencyManagement {
// If not using Swift Package Manager and plugin does not have podspec
// but does have a Package.swift, throw an error. Otherwise, it'll error
// when it builds.
if (!_project.usesSwiftPackageManager &&
if (!xcodeProject.usesSwiftPackageManager &&
!cocoaPodsCompatible &&
swiftPackageManagerCompatible) {
throwToolExit(
@@ -175,7 +175,7 @@ class DarwinDependencyManagement {
// Only show warnings to remove CocoaPods if the project is using Swift
// Package Manager, has already been migrated to have SPM integration, and
// all plugins are Swift Packages.
if (_project.usesSwiftPackageManager &&
if (xcodeProject.usesSwiftPackageManager &&
xcodeProject.flutterPluginSwiftPackageInProjectSettings &&
pluginCount == swiftPackageCount &&
swiftPackageCount != 0) {

View File

@@ -21,7 +21,6 @@ import 'features.dart';
import 'flutter_manifest.dart';
import 'flutter_plugins.dart';
import 'globals.dart' as globals;
import 'macos/xcode.dart';
import 'platform_plugins.dart';
import 'project_validator_result.dart';
import 'template.dart';
@@ -262,24 +261,6 @@ class FlutterProject {
/// True if this project has an example application.
bool get hasExampleApp => _exampleDirectory(directory).existsSync();
/// True if this project doesn't have Swift Package Manager disabled in the
/// pubspec, has either an iOS or macOS platform implementation, is not a
/// module project, Xcode is 15 or greater, and the Swift Package Manager
/// feature is enabled.
bool get usesSwiftPackageManager {
if (!manifest.disabledSwiftPackageManager &&
(ios.existsSync() || macos.existsSync()) &&
!isModule) {
final Xcode? xcode = globals.xcode;
final Version? xcodeVersion = xcode?.currentVersion;
if (xcodeVersion == null || xcodeVersion.major < 15) {
return false;
}
return featureFlags.isSwiftPackageManagerEnabled;
}
return false;
}
/// Returns a list of platform names that are supported by the project.
List<SupportedPlatform> getSupportedPlatforms({bool includeRoot = false}) {
return <SupportedPlatform>[

View File

@@ -5,15 +5,18 @@
import 'base/error_handling_io.dart';
import 'base/file_system.dart';
import 'base/utils.dart';
import 'base/version.dart';
import 'build_info.dart';
import 'bundle.dart' as bundle;
import 'convert.dart';
import 'features.dart';
import 'flutter_plugins.dart';
import 'globals.dart' as globals;
import 'ios/code_signing.dart';
import 'ios/plist_parser.dart';
import 'ios/xcode_build_settings.dart' as xcode;
import 'ios/xcodeproj.dart';
import 'macos/xcode.dart';
import 'platform_plugins.dart';
import 'project.dart';
import 'template.dart';
@@ -147,6 +150,40 @@ abstract class XcodeBasedProject extends FlutterProjectPlatform {
.contains('FlutterGeneratedPluginSwiftPackage');
}
/// True if this project doesn't have Swift Package Manager disabled in the
/// pubspec, has either an iOS or macOS platform implementation, is not a
/// module project, Xcode is 15 or greater, and the Swift Package Manager
/// feature is enabled.
bool get usesSwiftPackageManager {
if (!featureFlags.isSwiftPackageManagerEnabled) {
return false;
}
// The project can disable Swift Package Manager in its pubspec.yaml.
if (parent.manifest.disabledSwiftPackageManager) {
return false;
}
// TODO(loic-sharma): Support Swift Package Manager in add-to-app modules.
// https://github.com/flutter/flutter/issues/146957
if (parent.isModule) {
return false;
}
if (!existsSync()) {
return false;
}
// Swift Package Manager requires Xcode 15 or greater.
final Xcode? xcode = globals.xcode;
final Version? xcodeVersion = xcode?.currentVersion;
if (xcodeVersion == null || xcodeVersion.major < 15) {
return false;
}
return true;
}
Future<XcodeProjectInfo?> projectInfo() async {
final XcodeProjectInterpreter? xcodeProjectInterpreter = globals.xcodeProjectInterpreter;
if (!xcodeProject.existsSync() || xcodeProjectInterpreter == null || !xcodeProjectInterpreter.isInstalled) {