Files
packages/script/tool/lib/src/build_examples_command.dart
stuartmorgan ea72f74d0b [flutter_plugin_tools] Migrate build-examples to new base command (#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/flutter#83413
2021-06-30 11:33:09 -07:00

172 lines
5.1 KiB
Dart

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:file/file.dart';
import 'package:path/path.dart' as p;
import 'package:platform/platform.dart';
import 'common/core.dart';
import 'common/package_looping_command.dart';
import 'common/plugin_utils.dart';
import 'common/process_runner.dart';
/// Key for APK.
const String _platformFlagApk = 'apk';
const int _exitNoPlatformFlags = 2;
/// A command to build the example applications for packages.
class BuildExamplesCommand extends PackageLoopingCommand {
/// Creates an instance of the build command.
BuildExamplesCommand(
Directory packagesDir, {
ProcessRunner processRunner = const ProcessRunner(),
}) : super(packagesDir, processRunner: processRunner) {
argParser.addFlag(kPlatformLinux);
argParser.addFlag(kPlatformMacos);
argParser.addFlag(kPlatformWeb);
argParser.addFlag(kPlatformWindows);
argParser.addFlag(kPlatformIos);
argParser.addFlag(_platformFlagApk);
argParser.addOption(
kEnableExperiment,
defaultsTo: '',
help: 'Enables the given Dart SDK experiments.',
);
}
@override
final String name = 'build-examples';
@override
final String description =
'Builds all example apps (IPA for iOS and APK for Android).\n\n'
'This command requires "flutter" to be in your path.';
@override
Future<void> initializeRun() async {
final List<String> platformSwitches = <String>[
_platformFlagApk,
kPlatformIos,
kPlatformLinux,
kPlatformMacos,
kPlatformWeb,
kPlatformWindows,
];
if (!platformSwitches.any((String platform) => getBoolArg(platform))) {
printError(
'None of ${platformSwitches.map((String platform) => '--$platform').join(', ')} '
'were specified. At least one platform must be provided.');
throw ToolExit(_exitNoPlatformFlags);
}
}
@override
Future<List<String>> runForPackage(Directory package) async {
final List<String> errors = <String>[];
for (final Directory example in getExamplesForPlugin(package)) {
final String packageName =
p.relative(example.path, from: packagesDir.path);
if (getBoolArg(kPlatformLinux)) {
print('\nBUILDING $packageName for Linux');
if (isLinuxPlugin(package)) {
if (!await _buildExample(example, kPlatformLinux)) {
errors.add('$packageName (Linux)');
}
} else {
printSkip('Linux is not supported by this plugin');
}
}
if (getBoolArg(kPlatformMacos)) {
print('\nBUILDING $packageName for macOS');
if (isMacOsPlugin(package)) {
if (!await _buildExample(example, kPlatformMacos)) {
errors.add('$packageName (macOS)');
}
} else {
printSkip('macOS is not supported by this plugin');
}
}
if (getBoolArg(kPlatformWeb)) {
print('\nBUILDING $packageName for web');
if (isWebPlugin(package)) {
if (!await _buildExample(example, kPlatformWeb)) {
errors.add('$packageName (web)');
}
} else {
printSkip('Web is not supported by this plugin');
}
}
if (getBoolArg(kPlatformWindows)) {
print('\nBUILDING $packageName for Windows');
if (isWindowsPlugin(package)) {
if (!await _buildExample(example, kPlatformWindows)) {
errors.add('$packageName (Windows)');
}
} else {
printSkip('Windows is not supported by this plugin');
}
}
if (getBoolArg(kPlatformIos)) {
print('\nBUILDING $packageName for iOS');
if (isIosPlugin(package)) {
if (!await _buildExample(
example,
kPlatformIos,
extraBuildFlags: <String>['--no-codesign'],
)) {
errors.add('$packageName (iOS)');
}
} else {
printSkip('iOS is not supported by this plugin');
}
}
if (getBoolArg(_platformFlagApk)) {
print('\nBUILDING APK for $packageName');
if (isAndroidPlugin(package)) {
if (!await _buildExample(example, _platformFlagApk)) {
errors.add('$packageName (apk)');
}
} else {
printSkip('Android is not supported by this plugin');
}
}
}
return errors;
}
Future<bool> _buildExample(
Directory example,
String flutterBuildType, {
List<String> extraBuildFlags = const <String>[],
}) async {
final String flutterCommand =
const LocalPlatform().isWindows ? 'flutter.bat' : 'flutter';
final String enableExperiment = getStringArg(kEnableExperiment);
final int exitCode = await processRunner.runAndStream(
flutterCommand,
<String>[
'build',
flutterBuildType,
...extraBuildFlags,
if (enableExperiment.isNotEmpty)
'--enable-experiment=$enableExperiment',
],
workingDir: example,
);
return exitCode == 0;
}
}