Skip to content

Commit 0139586

Browse files
[flutter_plugin_tools] Migrate build-examples to new base command (flutter#4087)
Switches build-examples to the new base command that handles the boilerplate of looping over target packages. While modifying the command, also does some minor cleanup: - Extracts a helper to reduce duplicated details of calling `flutter build` - Switches the flag for iOS to `--ios` rather than `--ipa` since `ios` is what is actually passed to the build command - iOS no longer defaults to on, so that it behaves like all the other platform flags - Passing no platform flags is now an error rather than a silent pass, to ensure that we never accidentally have CI doing a no-op run without noticing. - Rewords the logging slightly for the versions where the label for what is being built is a platform, not an artifact (which is now everything but Android). Part of flutter#83413
1 parent 6e1d724 commit 0139586

File tree

4 files changed

+161
-232
lines changed

4 files changed

+161
-232
lines changed

.cirrus.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ task:
220220
- xcrun simctl list
221221
- xcrun simctl create Flutter-iPhone com.apple.CoreSimulator.SimDeviceType.iPhone-11 com.apple.CoreSimulator.SimRuntime.iOS-14-5 | xargs xcrun simctl boot
222222
build_script:
223-
- ./script/tool_runner.sh build-examples --ipa
223+
- ./script/tool_runner.sh build-examples --ios
224224
xctest_script:
225225
- ./script/tool_runner.sh xctest --ios --ios-destination "platform=iOS Simulator,name=iPhone 11,OS=latest"
226226
drive_script:
@@ -248,7 +248,7 @@ task:
248248
PATH: $PATH:/usr/local/bin
249249
build_script:
250250
- flutter config --enable-macos-desktop
251-
- ./script/tool_runner.sh build-examples --macos --no-ipa
251+
- ./script/tool_runner.sh build-examples --macos
252252
xctest_script:
253253
- ./script/tool_runner.sh xctest --macos --exclude $PLUGINS_TO_EXCLUDE_MACOS_XCTESTS
254254
drive_script:

script/tool/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
compatibility.
66
- `xctest` now supports running macOS tests in addition to iOS
77
- **Breaking change**: it now requires an `--ios` and/or `--macos` flag.
8+
- **Breaking change**: `build-examples` for iOS now uses `--ios` rather than
9+
`--ipa`.
810
- The tooling now runs in strong null-safe mode.
911
- `publish plugins` check against pub.dev to determine if a release should happen.
1012
- Modified the output format of many commands

script/tool/lib/src/build_examples_command.dart

Lines changed: 100 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,34 @@
33
// found in the LICENSE file.
44

55
import 'dart:async';
6-
import 'dart:io' as io;
76

87
import 'package:file/file.dart';
98
import 'package:path/path.dart' as p;
109
import 'package:platform/platform.dart';
1110

1211
import 'common/core.dart';
13-
import 'common/plugin_command.dart';
12+
import 'common/package_looping_command.dart';
1413
import 'common/plugin_utils.dart';
1514
import 'common/process_runner.dart';
1615

17-
/// Key for IPA.
18-
const String kIpa = 'ipa';
19-
2016
/// Key for APK.
21-
const String kApk = 'apk';
17+
const String _platformFlagApk = 'apk';
18+
19+
const int _exitNoPlatformFlags = 2;
2220

2321
/// A command to build the example applications for packages.
24-
class BuildExamplesCommand extends PluginCommand {
22+
class BuildExamplesCommand extends PackageLoopingCommand {
2523
/// Creates an instance of the build command.
2624
BuildExamplesCommand(
2725
Directory packagesDir, {
2826
ProcessRunner processRunner = const ProcessRunner(),
2927
}) : super(packagesDir, processRunner: processRunner) {
30-
argParser.addFlag(kPlatformLinux, defaultsTo: false);
31-
argParser.addFlag(kPlatformMacos, defaultsTo: false);
32-
argParser.addFlag(kPlatformWeb, defaultsTo: false);
33-
argParser.addFlag(kPlatformWindows, defaultsTo: false);
34-
argParser.addFlag(kIpa, defaultsTo: io.Platform.isMacOS);
35-
argParser.addFlag(kApk);
28+
argParser.addFlag(kPlatformLinux);
29+
argParser.addFlag(kPlatformMacos);
30+
argParser.addFlag(kPlatformWeb);
31+
argParser.addFlag(kPlatformWindows);
32+
argParser.addFlag(kPlatformIos);
33+
argParser.addFlag(_platformFlagApk);
3634
argParser.addOption(
3735
kEnableExperiment,
3836
defaultsTo: '',
@@ -49,164 +47,125 @@ class BuildExamplesCommand extends PluginCommand {
4947
'This command requires "flutter" to be in your path.';
5048

5149
@override
52-
Future<void> run() async {
50+
Future<void> initializeRun() async {
5351
final List<String> platformSwitches = <String>[
54-
kApk,
55-
kIpa,
52+
_platformFlagApk,
53+
kPlatformIos,
5654
kPlatformLinux,
5755
kPlatformMacos,
5856
kPlatformWeb,
5957
kPlatformWindows,
6058
];
6159
if (!platformSwitches.any((String platform) => getBoolArg(platform))) {
62-
print(
60+
printError(
6361
'None of ${platformSwitches.map((String platform) => '--$platform').join(', ')} '
64-
'were specified, so not building anything.');
65-
return;
62+
'were specified. At least one platform must be provided.');
63+
throw ToolExit(_exitNoPlatformFlags);
6664
}
67-
final String flutterCommand =
68-
const LocalPlatform().isWindows ? 'flutter.bat' : 'flutter';
69-
70-
final String enableExperiment = getStringArg(kEnableExperiment);
65+
}
7166

72-
final List<String> failingPackages = <String>[];
73-
await for (final Directory plugin in getPlugins()) {
74-
for (final Directory example in getExamplesForPlugin(plugin)) {
75-
final String packageName =
76-
p.relative(example.path, from: packagesDir.path);
77-
78-
if (getBoolArg(kPlatformLinux)) {
79-
print('\nBUILDING Linux for $packageName');
80-
if (isLinuxPlugin(plugin)) {
81-
final int buildExitCode = await processRunner.runAndStream(
82-
flutterCommand,
83-
<String>[
84-
'build',
85-
kPlatformLinux,
86-
if (enableExperiment.isNotEmpty)
87-
'--enable-experiment=$enableExperiment',
88-
],
89-
workingDir: example);
90-
if (buildExitCode != 0) {
91-
failingPackages.add('$packageName (linux)');
92-
}
93-
} else {
94-
print('Linux is not supported by this plugin');
67+
@override
68+
Future<List<String>> runForPackage(Directory package) async {
69+
final List<String> errors = <String>[];
70+
71+
for (final Directory example in getExamplesForPlugin(package)) {
72+
final String packageName =
73+
p.relative(example.path, from: packagesDir.path);
74+
75+
if (getBoolArg(kPlatformLinux)) {
76+
print('\nBUILDING $packageName for Linux');
77+
if (isLinuxPlugin(package)) {
78+
if (!await _buildExample(example, kPlatformLinux)) {
79+
errors.add('$packageName (Linux)');
9580
}
81+
} else {
82+
printSkip('Linux is not supported by this plugin');
9683
}
84+
}
9785

98-
if (getBoolArg(kPlatformMacos)) {
99-
print('\nBUILDING macOS for $packageName');
100-
if (isMacOsPlugin(plugin)) {
101-
final int exitCode = await processRunner.runAndStream(
102-
flutterCommand,
103-
<String>[
104-
'build',
105-
kPlatformMacos,
106-
if (enableExperiment.isNotEmpty)
107-
'--enable-experiment=$enableExperiment',
108-
],
109-
workingDir: example);
110-
if (exitCode != 0) {
111-
failingPackages.add('$packageName (macos)');
112-
}
113-
} else {
114-
print('macOS is not supported by this plugin');
86+
if (getBoolArg(kPlatformMacos)) {
87+
print('\nBUILDING $packageName for macOS');
88+
if (isMacOsPlugin(package)) {
89+
if (!await _buildExample(example, kPlatformMacos)) {
90+
errors.add('$packageName (macOS)');
11591
}
92+
} else {
93+
printSkip('macOS is not supported by this plugin');
11694
}
95+
}
11796

118-
if (getBoolArg(kPlatformWeb)) {
119-
print('\nBUILDING web for $packageName');
120-
if (isWebPlugin(plugin)) {
121-
final int buildExitCode = await processRunner.runAndStream(
122-
flutterCommand,
123-
<String>[
124-
'build',
125-
kPlatformWeb,
126-
if (enableExperiment.isNotEmpty)
127-
'--enable-experiment=$enableExperiment',
128-
],
129-
workingDir: example);
130-
if (buildExitCode != 0) {
131-
failingPackages.add('$packageName (web)');
132-
}
133-
} else {
134-
print('Web is not supported by this plugin');
97+
if (getBoolArg(kPlatformWeb)) {
98+
print('\nBUILDING $packageName for web');
99+
if (isWebPlugin(package)) {
100+
if (!await _buildExample(example, kPlatformWeb)) {
101+
errors.add('$packageName (web)');
135102
}
103+
} else {
104+
printSkip('Web is not supported by this plugin');
136105
}
106+
}
137107

138-
if (getBoolArg(kPlatformWindows)) {
139-
print('\nBUILDING Windows for $packageName');
140-
if (isWindowsPlugin(plugin)) {
141-
final int buildExitCode = await processRunner.runAndStream(
142-
flutterCommand,
143-
<String>[
144-
'build',
145-
kPlatformWindows,
146-
if (enableExperiment.isNotEmpty)
147-
'--enable-experiment=$enableExperiment',
148-
],
149-
workingDir: example);
150-
if (buildExitCode != 0) {
151-
failingPackages.add('$packageName (windows)');
152-
}
153-
} else {
154-
print('Windows is not supported by this plugin');
108+
if (getBoolArg(kPlatformWindows)) {
109+
print('\nBUILDING $packageName for Windows');
110+
if (isWindowsPlugin(package)) {
111+
if (!await _buildExample(example, kPlatformWindows)) {
112+
errors.add('$packageName (Windows)');
155113
}
114+
} else {
115+
printSkip('Windows is not supported by this plugin');
156116
}
117+
}
157118

158-
if (getBoolArg(kIpa)) {
159-
print('\nBUILDING IPA for $packageName');
160-
if (isIosPlugin(plugin)) {
161-
final int exitCode = await processRunner.runAndStream(
162-
flutterCommand,
163-
<String>[
164-
'build',
165-
'ios',
166-
'--no-codesign',
167-
if (enableExperiment.isNotEmpty)
168-
'--enable-experiment=$enableExperiment',
169-
],
170-
workingDir: example);
171-
if (exitCode != 0) {
172-
failingPackages.add('$packageName (ipa)');
173-
}
174-
} else {
175-
print('iOS is not supported by this plugin');
119+
if (getBoolArg(kPlatformIos)) {
120+
print('\nBUILDING $packageName for iOS');
121+
if (isIosPlugin(package)) {
122+
if (!await _buildExample(
123+
example,
124+
kPlatformIos,
125+
extraBuildFlags: <String>['--no-codesign'],
126+
)) {
127+
errors.add('$packageName (iOS)');
176128
}
129+
} else {
130+
printSkip('iOS is not supported by this plugin');
177131
}
132+
}
178133

179-
if (getBoolArg(kApk)) {
180-
print('\nBUILDING APK for $packageName');
181-
if (isAndroidPlugin(plugin)) {
182-
final int exitCode = await processRunner.runAndStream(
183-
flutterCommand,
184-
<String>[
185-
'build',
186-
'apk',
187-
if (enableExperiment.isNotEmpty)
188-
'--enable-experiment=$enableExperiment',
189-
],
190-
workingDir: example);
191-
if (exitCode != 0) {
192-
failingPackages.add('$packageName (apk)');
193-
}
194-
} else {
195-
print('Android is not supported by this plugin');
134+
if (getBoolArg(_platformFlagApk)) {
135+
print('\nBUILDING APK for $packageName');
136+
if (isAndroidPlugin(package)) {
137+
if (!await _buildExample(example, _platformFlagApk)) {
138+
errors.add('$packageName (apk)');
196139
}
140+
} else {
141+
printSkip('Android is not supported by this plugin');
197142
}
198143
}
199144
}
200-
print('\n\n');
201145

202-
if (failingPackages.isNotEmpty) {
203-
print('The following build are failing (see above for details):');
204-
for (final String package in failingPackages) {
205-
print(' * $package');
206-
}
207-
throw ToolExit(1);
208-
}
146+
return errors;
147+
}
209148

210-
print('All builds successful!');
149+
Future<bool> _buildExample(
150+
Directory example,
151+
String flutterBuildType, {
152+
List<String> extraBuildFlags = const <String>[],
153+
}) async {
154+
final String flutterCommand =
155+
const LocalPlatform().isWindows ? 'flutter.bat' : 'flutter';
156+
final String enableExperiment = getStringArg(kEnableExperiment);
157+
158+
final int exitCode = await processRunner.runAndStream(
159+
flutterCommand,
160+
<String>[
161+
'build',
162+
flutterBuildType,
163+
...extraBuildFlags,
164+
if (enableExperiment.isNotEmpty)
165+
'--enable-experiment=$enableExperiment',
166+
],
167+
workingDir: example,
168+
);
169+
return exitCode == 0;
211170
}
212171
}

0 commit comments

Comments
 (0)