|
| 1 | +// Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:io' as io; |
| 7 | + |
| 8 | +import 'package:file/file.dart'; |
| 9 | +import 'package:path/path.dart' as p; |
| 10 | +import 'package:platform/platform.dart'; |
| 11 | + |
| 12 | +import 'common.dart'; |
| 13 | + |
| 14 | +class BuildExamplesCommand extends PluginCommand { |
| 15 | + BuildExamplesCommand( |
| 16 | + Directory packagesDir, |
| 17 | + FileSystem fileSystem, { |
| 18 | + ProcessRunner processRunner = const ProcessRunner(), |
| 19 | + }) : super(packagesDir, fileSystem, processRunner: processRunner) { |
| 20 | + argParser.addFlag(kLinux, defaultsTo: false); |
| 21 | + argParser.addFlag(kMacos, defaultsTo: false); |
| 22 | + argParser.addFlag(kWindows, defaultsTo: false); |
| 23 | + argParser.addFlag(kIpa, defaultsTo: io.Platform.isMacOS); |
| 24 | + argParser.addFlag(kApk); |
| 25 | + argParser.addOption( |
| 26 | + kEnableExperiment, |
| 27 | + defaultsTo: '', |
| 28 | + help: 'Enables the given Dart SDK experiments.', |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + @override |
| 33 | + final String name = 'build-examples'; |
| 34 | + |
| 35 | + @override |
| 36 | + final String description = |
| 37 | + 'Builds all example apps (IPA for iOS and APK for Android).\n\n' |
| 38 | + 'This command requires "flutter" to be in your path.'; |
| 39 | + |
| 40 | + @override |
| 41 | + Future<Null> run() async { |
| 42 | + if (!argResults[kIpa] && |
| 43 | + !argResults[kApk] && |
| 44 | + !argResults[kLinux] && |
| 45 | + !argResults[kMacos] && |
| 46 | + !argResults[kWindows]) { |
| 47 | + print( |
| 48 | + 'None of --linux, --macos, --windows, --apk nor --ipa were specified, ' |
| 49 | + 'so not building anything.'); |
| 50 | + return; |
| 51 | + } |
| 52 | + final String flutterCommand = |
| 53 | + LocalPlatform().isWindows ? 'flutter.bat' : 'flutter'; |
| 54 | + |
| 55 | + final String enableExperiment = argResults[kEnableExperiment]; |
| 56 | + |
| 57 | + checkSharding(); |
| 58 | + final List<String> failingPackages = <String>[]; |
| 59 | + await for (Directory plugin in getPlugins()) { |
| 60 | + for (Directory example in getExamplesForPlugin(plugin)) { |
| 61 | + final String packageName = |
| 62 | + p.relative(example.path, from: packagesDir.path); |
| 63 | + |
| 64 | + if (argResults[kLinux]) { |
| 65 | + print('\nBUILDING Linux for $packageName'); |
| 66 | + if (isLinuxPlugin(plugin, fileSystem)) { |
| 67 | + int buildExitCode = await processRunner.runAndStream( |
| 68 | + flutterCommand, |
| 69 | + <String>[ |
| 70 | + 'build', |
| 71 | + kLinux, |
| 72 | + if (enableExperiment.isNotEmpty) |
| 73 | + '--enable-experiment=$enableExperiment', |
| 74 | + ], |
| 75 | + workingDir: example); |
| 76 | + if (buildExitCode != 0) { |
| 77 | + failingPackages.add('$packageName (linux)'); |
| 78 | + } |
| 79 | + } else { |
| 80 | + print('Linux is not supported by this plugin'); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (argResults[kMacos]) { |
| 85 | + print('\nBUILDING macOS for $packageName'); |
| 86 | + if (isMacOsPlugin(plugin, fileSystem)) { |
| 87 | + // TODO(https://github.com/flutter/flutter/issues/46236): |
| 88 | + // Builing macos without running flutter pub get first results |
| 89 | + // in an error. |
| 90 | + int exitCode = await processRunner.runAndStream( |
| 91 | + flutterCommand, <String>['pub', 'get'], |
| 92 | + workingDir: example); |
| 93 | + if (exitCode != 0) { |
| 94 | + failingPackages.add('$packageName (macos)'); |
| 95 | + } else { |
| 96 | + exitCode = await processRunner.runAndStream( |
| 97 | + flutterCommand, |
| 98 | + <String>[ |
| 99 | + 'build', |
| 100 | + kMacos, |
| 101 | + if (enableExperiment.isNotEmpty) |
| 102 | + '--enable-experiment=$enableExperiment', |
| 103 | + ], |
| 104 | + workingDir: example); |
| 105 | + if (exitCode != 0) { |
| 106 | + failingPackages.add('$packageName (macos)'); |
| 107 | + } |
| 108 | + } |
| 109 | + } else { |
| 110 | + print('macOS is not supported by this plugin'); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (argResults[kWindows]) { |
| 115 | + print('\nBUILDING Windows for $packageName'); |
| 116 | + if (isWindowsPlugin(plugin, fileSystem)) { |
| 117 | + int buildExitCode = await processRunner.runAndStream( |
| 118 | + flutterCommand, |
| 119 | + <String>[ |
| 120 | + 'build', |
| 121 | + kWindows, |
| 122 | + if (enableExperiment.isNotEmpty) |
| 123 | + '--enable-experiment=$enableExperiment', |
| 124 | + ], |
| 125 | + workingDir: example); |
| 126 | + if (buildExitCode != 0) { |
| 127 | + failingPackages.add('$packageName (windows)'); |
| 128 | + } |
| 129 | + } else { |
| 130 | + print('Windows is not supported by this plugin'); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if (argResults[kIpa]) { |
| 135 | + print('\nBUILDING IPA for $packageName'); |
| 136 | + if (isIosPlugin(plugin, fileSystem)) { |
| 137 | + final int exitCode = await processRunner.runAndStream( |
| 138 | + flutterCommand, |
| 139 | + <String>[ |
| 140 | + 'build', |
| 141 | + 'ios', |
| 142 | + '--no-codesign', |
| 143 | + if (enableExperiment.isNotEmpty) |
| 144 | + '--enable-experiment=$enableExperiment', |
| 145 | + ], |
| 146 | + workingDir: example); |
| 147 | + if (exitCode != 0) { |
| 148 | + failingPackages.add('$packageName (ipa)'); |
| 149 | + } |
| 150 | + } else { |
| 151 | + print('iOS is not supported by this plugin'); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if (argResults[kApk]) { |
| 156 | + print('\nBUILDING APK for $packageName'); |
| 157 | + if (isAndroidPlugin(plugin, fileSystem)) { |
| 158 | + final int exitCode = await processRunner.runAndStream( |
| 159 | + flutterCommand, |
| 160 | + <String>[ |
| 161 | + 'build', |
| 162 | + 'apk', |
| 163 | + if (enableExperiment.isNotEmpty) |
| 164 | + '--enable-experiment=$enableExperiment', |
| 165 | + ], |
| 166 | + workingDir: example); |
| 167 | + if (exitCode != 0) { |
| 168 | + failingPackages.add('$packageName (apk)'); |
| 169 | + } |
| 170 | + } else { |
| 171 | + print('Android is not supported by this plugin'); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + print('\n\n'); |
| 177 | + |
| 178 | + if (failingPackages.isNotEmpty) { |
| 179 | + print('The following build are failing (see above for details):'); |
| 180 | + for (String package in failingPackages) { |
| 181 | + print(' * $package'); |
| 182 | + } |
| 183 | + throw ToolExit(1); |
| 184 | + } |
| 185 | + |
| 186 | + print('All builds successful!'); |
| 187 | + } |
| 188 | +} |
0 commit comments