Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[flutter_plugin_tools] Migrate build-examples to new base command #4087

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ task:
- xcrun simctl list
- xcrun simctl create Flutter-iPhone com.apple.CoreSimulator.SimDeviceType.iPhone-11 com.apple.CoreSimulator.SimRuntime.iOS-14-5 | xargs xcrun simctl boot
build_script:
- ./script/tool_runner.sh build-examples --ipa
- ./script/tool_runner.sh build-examples --ios
xctest_script:
- ./script/tool_runner.sh xctest --ios --ios-destination "platform=iOS Simulator,name=iPhone 11,OS=latest"
drive_script:
Expand Down Expand Up @@ -246,7 +246,7 @@ task:
PATH: $PATH:/usr/local/bin
build_script:
- flutter config --enable-macos-desktop
- ./script/tool_runner.sh build-examples --macos --no-ipa
- ./script/tool_runner.sh build-examples --macos
xctest_script:
- ./script/tool_runner.sh xctest --macos --exclude $PLUGINS_TO_EXCLUDE_MACOS_XCTESTS
drive_script:
Expand Down
2 changes: 2 additions & 0 deletions script/tool/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
compatibility.
- `xctest` now supports running macOS tests in addition to iOS
- **Breaking change**: it now requires an `--ios` and/or `--macos` flag.
- **Breaking change**: `build-examples` for iOS now uses `--ios` rather than
`--ipa`.
- The tooling now runs in strong null-safe mode.
- Modified the output format of `pubspec-check` and `xctest`

Expand Down
241 changes: 100 additions & 141 deletions script/tool/lib/src/build_examples_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,34 @@
// found in the LICENSE file.

import 'dart:async';
import 'dart:io' as io;

import 'package:file/file.dart';
import 'package:path/path.dart' as p;
import 'package:platform/platform.dart';

import 'common/core.dart';
import 'common/plugin_command.dart';
import 'common/package_looping_command.dart';
import 'common/plugin_utils.dart';
import 'common/process_runner.dart';

/// Key for IPA.
const String kIpa = 'ipa';

/// Key for APK.
const String kApk = 'apk';
const String _platformFlagApk = 'apk';

const int _exitNoPlatformFlags = 2;

/// A command to build the example applications for packages.
class BuildExamplesCommand extends PluginCommand {
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, defaultsTo: false);
argParser.addFlag(kPlatformMacos, defaultsTo: false);
argParser.addFlag(kPlatformWeb, defaultsTo: false);
argParser.addFlag(kPlatformWindows, defaultsTo: false);
argParser.addFlag(kIpa, defaultsTo: io.Platform.isMacOS);
argParser.addFlag(kApk);
argParser.addFlag(kPlatformLinux);
argParser.addFlag(kPlatformMacos);
argParser.addFlag(kPlatformWeb);
argParser.addFlag(kPlatformWindows);
argParser.addFlag(kPlatformIos);
argParser.addFlag(_platformFlagApk);
argParser.addOption(
kEnableExperiment,
defaultsTo: '',
Expand All @@ -49,164 +47,125 @@ class BuildExamplesCommand extends PluginCommand {
'This command requires "flutter" to be in your path.';

@override
Future<void> run() async {
Future<void> initializeRun() async {
final List<String> platformSwitches = <String>[
kApk,
kIpa,
_platformFlagApk,
kPlatformIos,
kPlatformLinux,
kPlatformMacos,
kPlatformWeb,
kPlatformWindows,
];
if (!platformSwitches.any((String platform) => getBoolArg(platform))) {
print(
printError(
'None of ${platformSwitches.map((String platform) => '--$platform').join(', ')} '
'were specified, so not building anything.');
return;
'were specified. At least one platform must be provided.');
throw ToolExit(_exitNoPlatformFlags);
}
final String flutterCommand =
const LocalPlatform().isWindows ? 'flutter.bat' : 'flutter';

final String enableExperiment = getStringArg(kEnableExperiment);
}

final List<String> failingPackages = <String>[];
await for (final Directory plugin in getPlugins()) {
for (final Directory example in getExamplesForPlugin(plugin)) {
final String packageName =
p.relative(example.path, from: packagesDir.path);

if (getBoolArg(kPlatformLinux)) {
print('\nBUILDING Linux for $packageName');
if (isLinuxPlugin(plugin)) {
final int buildExitCode = await processRunner.runAndStream(
flutterCommand,
<String>[
'build',
kPlatformLinux,
if (enableExperiment.isNotEmpty)
'--enable-experiment=$enableExperiment',
],
workingDir: example);
if (buildExitCode != 0) {
failingPackages.add('$packageName (linux)');
}
} else {
print('Linux is not supported by this plugin');
@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 macOS for $packageName');
if (isMacOsPlugin(plugin)) {
final int exitCode = await processRunner.runAndStream(
flutterCommand,
<String>[
'build',
kPlatformMacos,
if (enableExperiment.isNotEmpty)
'--enable-experiment=$enableExperiment',
],
workingDir: example);
if (exitCode != 0) {
failingPackages.add('$packageName (macos)');
}
} else {
print('macOS is not supported by this plugin');
if (getBoolArg(kPlatformMacos)) {
print('\nBUILDING $packageName for macOS');
if (isMacOsPlugin(package)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be an else branch for isMacOsPlugin? Printing out a warning?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an else; it's the printSkip. It's not a warning because it's normal for many plugins not to support a given platform, particularly with federation.

if (!await _buildExample(example, kPlatformMacos)) {
errors.add('$packageName (macOS)');
}
} else {
printSkip('macOS is not supported by this plugin');
}
}

if (getBoolArg(kPlatformWeb)) {
print('\nBUILDING web for $packageName');
if (isWebPlugin(plugin)) {
final int buildExitCode = await processRunner.runAndStream(
flutterCommand,
<String>[
'build',
kPlatformWeb,
if (enableExperiment.isNotEmpty)
'--enable-experiment=$enableExperiment',
],
workingDir: example);
if (buildExitCode != 0) {
failingPackages.add('$packageName (web)');
}
} else {
print('Web 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 Windows for $packageName');
if (isWindowsPlugin(plugin)) {
final int buildExitCode = await processRunner.runAndStream(
flutterCommand,
<String>[
'build',
kPlatformWindows,
if (enableExperiment.isNotEmpty)
'--enable-experiment=$enableExperiment',
],
workingDir: example);
if (buildExitCode != 0) {
failingPackages.add('$packageName (windows)');
}
} else {
print('Windows 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(kIpa)) {
print('\nBUILDING IPA for $packageName');
if (isIosPlugin(plugin)) {
final int exitCode = await processRunner.runAndStream(
flutterCommand,
<String>[
'build',
'ios',
'--no-codesign',
if (enableExperiment.isNotEmpty)
'--enable-experiment=$enableExperiment',
],
workingDir: example);
if (exitCode != 0) {
failingPackages.add('$packageName (ipa)');
}
} else {
print('iOS 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(kApk)) {
print('\nBUILDING APK for $packageName');
if (isAndroidPlugin(plugin)) {
final int exitCode = await processRunner.runAndStream(
flutterCommand,
<String>[
'build',
'apk',
if (enableExperiment.isNotEmpty)
'--enable-experiment=$enableExperiment',
],
workingDir: example);
if (exitCode != 0) {
failingPackages.add('$packageName (apk)');
}
} else {
print('Android 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');
}
}
}
print('\n\n');

if (failingPackages.isNotEmpty) {
print('The following build are failing (see above for details):');
for (final String package in failingPackages) {
print(' * $package');
}
throw ToolExit(1);
}
return errors;
}

print('All builds successful!');
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;
}
}
Loading