diff --git a/script/tool/lib/src/build_examples_command.dart b/script/tool/lib/src/build_examples_command.dart index d8d1613e06..d961201f71 100644 --- a/script/tool/lib/src/build_examples_command.dart +++ b/script/tool/lib/src/build_examples_command.dart @@ -61,7 +61,7 @@ class BuildExamplesCommand extends PackageLoopingCommand { defaultsTo: '', help: 'Enables the given Dart SDK experiments.', ); - argParser.addFlag(_swiftPackageManagerFlag); + argParser.addFlag(_swiftPackageManagerFlag, defaultsTo: null); } // Maps the switch this command uses to identify a platform to information @@ -115,13 +115,22 @@ class BuildExamplesCommand extends PackageLoopingCommand { 'single key "$_pluginToolsConfigGlobalKey" containing a list of build ' 'arguments.'; - /// Returns true if `--swift-package-manager` flag was passed along with - /// either `--ios` or `--macos`. - bool get usingSwiftPackageManager { + /// Returns whether the Swift Package Manager feature should be enabled, + /// disabled, or left to the release channel's default value. + bool? get _swiftPackageManagerFeatureConfig { final List platformFlags = _platforms.keys.toList(); - return getBoolArg(_swiftPackageManagerFlag) && - (platformFlags.contains(platformIOS) || - platformFlags.contains(platformMacOS)); + if (!platformFlags.contains(platformIOS) && + !platformFlags.contains(platformMacOS)) { + return null; + } + + // TODO(loic-sharma): Allow enabling on stable once Swift Package Manager + // feature is available on stable. + if (platform.environment['CHANNEL'] != 'master') { + return null; + } + + return getNullableBoolArg(_swiftPackageManagerFlag); } @override @@ -135,15 +144,21 @@ class BuildExamplesCommand extends PackageLoopingCommand { throw ToolExit(_exitNoPlatformFlags); } - // TODO(vashworth): Enable on stable once Swift Package Manager feature is - // available on stable. - if (usingSwiftPackageManager && - platform.environment['CHANNEL'] != 'stable') { - await processRunner.runAndStream( - flutterCommand, - ['config', '--enable-swift-package-manager'], - exitOnError: true, - ); + switch (_swiftPackageManagerFeatureConfig) { + case true: + await processRunner.runAndStream( + flutterCommand, + ['config', '--enable-swift-package-manager'], + exitOnError: true, + ); + case false: + await processRunner.runAndStream( + flutterCommand, + ['config', '--no-enable-swift-package-manager'], + exitOnError: true, + ); + case null: + break; } } diff --git a/script/tool/lib/src/common/package_command.dart b/script/tool/lib/src/common/package_command.dart index 94a808afc1..7af85f1d50 100644 --- a/script/tool/lib/src/common/package_command.dart +++ b/script/tool/lib/src/common/package_command.dart @@ -230,6 +230,11 @@ abstract class PackageCommand extends Command { return (argResults![key] as bool?) ?? false; } + /// Convenience accessor for boolean arguments. + bool? getNullableBoolArg(String key) { + return argResults![key] as bool?; + } + /// Convenience accessor for String arguments. String getStringArg(String key) { return (argResults![key] as String?) ?? ''; diff --git a/script/tool/test/build_examples_command_test.dart b/script/tool/test/build_examples_command_test.dart index 0c99bfb9ba..53f1aeb7f1 100644 --- a/script/tool/test/build_examples_command_test.dart +++ b/script/tool/test/build_examples_command_test.dart @@ -164,6 +164,53 @@ void main() { ])); }); + test('building for iOS with CocoaPods on master channel', () async { + mockPlatform.isMacOS = true; + mockPlatform.environment['CHANNEL'] = 'master'; + + final RepositoryPackage plugin = createFakePlugin('plugin', packagesDir, + platformSupport: { + platformIOS: const PlatformDetails(PlatformSupport.inline), + }); + + final Directory pluginExampleDirectory = getExampleDir(plugin); + + final List output = await runCapturingPrint(runner, [ + 'build-examples', + '--ios', + '--enable-experiment=exp1', + '--no-swift-package-manager', + ]); + + expect( + output, + containsAllInOrder([ + '\nBUILDING plugin/example for iOS', + ]), + ); + + expect( + processRunner.recordedCalls, + orderedEquals([ + ProcessCall( + getFlutterCommand(mockPlatform), + const ['config', '--no-enable-swift-package-manager'], + null, + ), + ProcessCall( + getFlutterCommand(mockPlatform), + const [ + 'build', + 'ios', + '--no-codesign', + '--enable-experiment=exp1' + ], + pluginExampleDirectory.path, + ), + ]), + ); + }); + test('building for iOS with Swift Package Manager on master channel', () async { mockPlatform.isMacOS = true; @@ -212,6 +259,50 @@ void main() { ); }); + test( + 'building for iOS with CocoaPods on stable channel does not disable SPM', + () async { + mockPlatform.isMacOS = true; + mockPlatform.environment['CHANNEL'] = 'stable'; + + final RepositoryPackage plugin = createFakePlugin('plugin', packagesDir, + platformSupport: { + platformIOS: const PlatformDetails(PlatformSupport.inline), + }); + + final Directory pluginExampleDirectory = getExampleDir(plugin); + + final List output = await runCapturingPrint(runner, [ + 'build-examples', + '--ios', + '--enable-experiment=exp1', + '--no-swift-package-manager', + ]); + + expect( + output, + containsAllInOrder([ + '\nBUILDING plugin/example for iOS', + ]), + ); + + expect( + processRunner.recordedCalls, + orderedEquals([ + ProcessCall( + getFlutterCommand(mockPlatform), + const [ + 'build', + 'ios', + '--no-codesign', + '--enable-experiment=exp1' + ], + pluginExampleDirectory.path, + ), + ]), + ); + }); + test( 'building for iOS with Swift Package Manager on stable channel does not enable SPM', () async { @@ -353,6 +444,48 @@ void main() { ])); }); + test('building for macOS with CocoaPods on master channel', () async { + mockPlatform.isMacOS = true; + mockPlatform.environment['CHANNEL'] = 'master'; + + final RepositoryPackage plugin = createFakePlugin('plugin', packagesDir, + platformSupport: { + platformMacOS: const PlatformDetails(PlatformSupport.inline), + }); + + final Directory pluginExampleDirectory = getExampleDir(plugin); + + final List output = await runCapturingPrint(runner, + ['build-examples', '--macos', '--no-swift-package-manager']); + + expect( + output, + containsAllInOrder([ + '\nBUILDING plugin/example for macOS', + ]), + ); + + expect( + processRunner.recordedCalls, + orderedEquals([ + ProcessCall( + getFlutterCommand(mockPlatform), + const ['config', '--no-enable-swift-package-manager'], + null, + ), + ProcessCall( + getFlutterCommand(mockPlatform), + const [ + 'build', + 'macos', + ], + pluginExampleDirectory.path, + ), + ]), + ); + }); + + test('building for macOS with Swift Package Manager on master channel', () async { mockPlatform.isMacOS = true; @@ -395,6 +528,44 @@ void main() { ); }); + test( + 'building for macOS with CocoaPods on stable channel does not disable SPM', + () async { + mockPlatform.isMacOS = true; + mockPlatform.environment['CHANNEL'] = 'stable'; + + final RepositoryPackage plugin = createFakePlugin('plugin', packagesDir, + platformSupport: { + platformMacOS: const PlatformDetails(PlatformSupport.inline), + }); + + final Directory pluginExampleDirectory = getExampleDir(plugin); + + final List output = await runCapturingPrint(runner, + ['build-examples', '--macos', '--no-swift-package-manager']); + + expect( + output, + containsAllInOrder([ + '\nBUILDING plugin/example for macOS', + ]), + ); + + expect( + processRunner.recordedCalls, + orderedEquals([ + ProcessCall( + getFlutterCommand(mockPlatform), + const [ + 'build', + 'macos', + ], + pluginExampleDirectory.path, + ), + ]), + ); + }); + test( 'building for macOS with Swift Package Manager on stable channel does not enable SPM', () async {