Skip to content

Commit c7f0526

Browse files
authored
[flutter_plugin_tools] Allow disabling Swift Package Manager when building examples (flutter#7145)
You can use the Flutter plugin tool to [build the plugins' examples apps](https://github.com/flutter/packages/tree/main/script/tool#run-dart-integration-tests). Currently, the tool can enable the Swift Package Manager feature. This change allows the tool to disable the Swift Package Manager feature. This will be useful for when SPM is enabled by default on a release channel. Preparation for: flutter#151567
1 parent df420f6 commit c7f0526

File tree

3 files changed

+207
-16
lines changed

3 files changed

+207
-16
lines changed

script/tool/lib/src/build_examples_command.dart

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class BuildExamplesCommand extends PackageLoopingCommand {
6161
defaultsTo: '',
6262
help: 'Enables the given Dart SDK experiments.',
6363
);
64-
argParser.addFlag(_swiftPackageManagerFlag);
64+
argParser.addFlag(_swiftPackageManagerFlag, defaultsTo: null);
6565
}
6666

6767
// Maps the switch this command uses to identify a platform to information
@@ -115,13 +115,22 @@ class BuildExamplesCommand extends PackageLoopingCommand {
115115
'single key "$_pluginToolsConfigGlobalKey" containing a list of build '
116116
'arguments.';
117117

118-
/// Returns true if `--swift-package-manager` flag was passed along with
119-
/// either `--ios` or `--macos`.
120-
bool get usingSwiftPackageManager {
118+
/// Returns whether the Swift Package Manager feature should be enabled,
119+
/// disabled, or left to the release channel's default value.
120+
bool? get _swiftPackageManagerFeatureConfig {
121121
final List<String> platformFlags = _platforms.keys.toList();
122-
return getBoolArg(_swiftPackageManagerFlag) &&
123-
(platformFlags.contains(platformIOS) ||
124-
platformFlags.contains(platformMacOS));
122+
if (!platformFlags.contains(platformIOS) &&
123+
!platformFlags.contains(platformMacOS)) {
124+
return null;
125+
}
126+
127+
// TODO(loic-sharma): Allow enabling on stable once Swift Package Manager
128+
// feature is available on stable.
129+
if (platform.environment['CHANNEL'] != 'master') {
130+
return null;
131+
}
132+
133+
return getNullableBoolArg(_swiftPackageManagerFlag);
125134
}
126135

127136
@override
@@ -135,15 +144,21 @@ class BuildExamplesCommand extends PackageLoopingCommand {
135144
throw ToolExit(_exitNoPlatformFlags);
136145
}
137146

138-
// TODO(vashworth): Enable on stable once Swift Package Manager feature is
139-
// available on stable.
140-
if (usingSwiftPackageManager &&
141-
platform.environment['CHANNEL'] != 'stable') {
142-
await processRunner.runAndStream(
143-
flutterCommand,
144-
<String>['config', '--enable-swift-package-manager'],
145-
exitOnError: true,
146-
);
147+
switch (_swiftPackageManagerFeatureConfig) {
148+
case true:
149+
await processRunner.runAndStream(
150+
flutterCommand,
151+
<String>['config', '--enable-swift-package-manager'],
152+
exitOnError: true,
153+
);
154+
case false:
155+
await processRunner.runAndStream(
156+
flutterCommand,
157+
<String>['config', '--no-enable-swift-package-manager'],
158+
exitOnError: true,
159+
);
160+
case null:
161+
break;
147162
}
148163
}
149164

script/tool/lib/src/common/package_command.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ abstract class PackageCommand extends Command<void> {
230230
return (argResults![key] as bool?) ?? false;
231231
}
232232

233+
/// Convenience accessor for boolean arguments.
234+
bool? getNullableBoolArg(String key) {
235+
return argResults![key] as bool?;
236+
}
237+
233238
/// Convenience accessor for String arguments.
234239
String getStringArg(String key) {
235240
return (argResults![key] as String?) ?? '';

script/tool/test/build_examples_command_test.dart

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,53 @@ void main() {
164164
]));
165165
});
166166

167+
test('building for iOS with CocoaPods on master channel', () async {
168+
mockPlatform.isMacOS = true;
169+
mockPlatform.environment['CHANNEL'] = 'master';
170+
171+
final RepositoryPackage plugin = createFakePlugin('plugin', packagesDir,
172+
platformSupport: <String, PlatformDetails>{
173+
platformIOS: const PlatformDetails(PlatformSupport.inline),
174+
});
175+
176+
final Directory pluginExampleDirectory = getExampleDir(plugin);
177+
178+
final List<String> output = await runCapturingPrint(runner, <String>[
179+
'build-examples',
180+
'--ios',
181+
'--enable-experiment=exp1',
182+
'--no-swift-package-manager',
183+
]);
184+
185+
expect(
186+
output,
187+
containsAllInOrder(<String>[
188+
'\nBUILDING plugin/example for iOS',
189+
]),
190+
);
191+
192+
expect(
193+
processRunner.recordedCalls,
194+
orderedEquals(<ProcessCall>[
195+
ProcessCall(
196+
getFlutterCommand(mockPlatform),
197+
const <String>['config', '--no-enable-swift-package-manager'],
198+
null,
199+
),
200+
ProcessCall(
201+
getFlutterCommand(mockPlatform),
202+
const <String>[
203+
'build',
204+
'ios',
205+
'--no-codesign',
206+
'--enable-experiment=exp1'
207+
],
208+
pluginExampleDirectory.path,
209+
),
210+
]),
211+
);
212+
});
213+
167214
test('building for iOS with Swift Package Manager on master channel',
168215
() async {
169216
mockPlatform.isMacOS = true;
@@ -212,6 +259,50 @@ void main() {
212259
);
213260
});
214261

262+
test(
263+
'building for iOS with CocoaPods on stable channel does not disable SPM',
264+
() async {
265+
mockPlatform.isMacOS = true;
266+
mockPlatform.environment['CHANNEL'] = 'stable';
267+
268+
final RepositoryPackage plugin = createFakePlugin('plugin', packagesDir,
269+
platformSupport: <String, PlatformDetails>{
270+
platformIOS: const PlatformDetails(PlatformSupport.inline),
271+
});
272+
273+
final Directory pluginExampleDirectory = getExampleDir(plugin);
274+
275+
final List<String> output = await runCapturingPrint(runner, <String>[
276+
'build-examples',
277+
'--ios',
278+
'--enable-experiment=exp1',
279+
'--no-swift-package-manager',
280+
]);
281+
282+
expect(
283+
output,
284+
containsAllInOrder(<String>[
285+
'\nBUILDING plugin/example for iOS',
286+
]),
287+
);
288+
289+
expect(
290+
processRunner.recordedCalls,
291+
orderedEquals(<ProcessCall>[
292+
ProcessCall(
293+
getFlutterCommand(mockPlatform),
294+
const <String>[
295+
'build',
296+
'ios',
297+
'--no-codesign',
298+
'--enable-experiment=exp1'
299+
],
300+
pluginExampleDirectory.path,
301+
),
302+
]),
303+
);
304+
});
305+
215306
test(
216307
'building for iOS with Swift Package Manager on stable channel does not enable SPM',
217308
() async {
@@ -353,6 +444,48 @@ void main() {
353444
]));
354445
});
355446

447+
test('building for macOS with CocoaPods on master channel', () async {
448+
mockPlatform.isMacOS = true;
449+
mockPlatform.environment['CHANNEL'] = 'master';
450+
451+
final RepositoryPackage plugin = createFakePlugin('plugin', packagesDir,
452+
platformSupport: <String, PlatformDetails>{
453+
platformMacOS: const PlatformDetails(PlatformSupport.inline),
454+
});
455+
456+
final Directory pluginExampleDirectory = getExampleDir(plugin);
457+
458+
final List<String> output = await runCapturingPrint(runner,
459+
<String>['build-examples', '--macos', '--no-swift-package-manager']);
460+
461+
expect(
462+
output,
463+
containsAllInOrder(<String>[
464+
'\nBUILDING plugin/example for macOS',
465+
]),
466+
);
467+
468+
expect(
469+
processRunner.recordedCalls,
470+
orderedEquals(<ProcessCall>[
471+
ProcessCall(
472+
getFlutterCommand(mockPlatform),
473+
const <String>['config', '--no-enable-swift-package-manager'],
474+
null,
475+
),
476+
ProcessCall(
477+
getFlutterCommand(mockPlatform),
478+
const <String>[
479+
'build',
480+
'macos',
481+
],
482+
pluginExampleDirectory.path,
483+
),
484+
]),
485+
);
486+
});
487+
488+
356489
test('building for macOS with Swift Package Manager on master channel',
357490
() async {
358491
mockPlatform.isMacOS = true;
@@ -395,6 +528,44 @@ void main() {
395528
);
396529
});
397530

531+
test(
532+
'building for macOS with CocoaPods on stable channel does not disable SPM',
533+
() async {
534+
mockPlatform.isMacOS = true;
535+
mockPlatform.environment['CHANNEL'] = 'stable';
536+
537+
final RepositoryPackage plugin = createFakePlugin('plugin', packagesDir,
538+
platformSupport: <String, PlatformDetails>{
539+
platformMacOS: const PlatformDetails(PlatformSupport.inline),
540+
});
541+
542+
final Directory pluginExampleDirectory = getExampleDir(plugin);
543+
544+
final List<String> output = await runCapturingPrint(runner,
545+
<String>['build-examples', '--macos', '--no-swift-package-manager']);
546+
547+
expect(
548+
output,
549+
containsAllInOrder(<String>[
550+
'\nBUILDING plugin/example for macOS',
551+
]),
552+
);
553+
554+
expect(
555+
processRunner.recordedCalls,
556+
orderedEquals(<ProcessCall>[
557+
ProcessCall(
558+
getFlutterCommand(mockPlatform),
559+
const <String>[
560+
'build',
561+
'macos',
562+
],
563+
pluginExampleDirectory.path,
564+
),
565+
]),
566+
);
567+
});
568+
398569
test(
399570
'building for macOS with Swift Package Manager on stable channel does not enable SPM',
400571
() async {

0 commit comments

Comments
 (0)