Adam Barth | bdd2066 | 2015-10-11 19:42:50 -0700 | [diff] [blame] | 1 | // Copyright 2015 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'; |
Devon Carew | 494d1e0 | 2015-10-28 20:57:51 -0700 | [diff] [blame] | 6 | import 'dart:io'; |
Adam Barth | bdd2066 | 2015-10-11 19:42:50 -0700 | [diff] [blame] | 7 | |
| 8 | import 'package:args/command_runner.dart'; |
| 9 | |
| 10 | import '../application_package.dart'; |
Devon Carew | 2dbceaf | 2016-02-06 19:19:50 -0800 | [diff] [blame] | 11 | import '../build_configuration.dart'; |
Adam Barth | 2e062df | 2016-03-28 13:07:42 -0700 | [diff] [blame] | 12 | import '../dart/pub.dart'; |
Adam Barth | bdd2066 | 2015-10-11 19:42:50 -0700 | [diff] [blame] | 13 | import '../device.dart'; |
Devon Carew | 5ad6a57 | 2016-03-11 08:49:55 -0800 | [diff] [blame] | 14 | import '../flx.dart' as flx; |
Devon Carew | d5a6fce | 2016-02-17 00:59:56 -0800 | [diff] [blame] | 15 | import '../globals.dart'; |
Adam Barth | 2e062df | 2016-03-28 13:07:42 -0700 | [diff] [blame] | 16 | import '../package_map.dart'; |
Adam Barth | 12f7581 | 2015-10-13 10:00:06 -0700 | [diff] [blame] | 17 | import '../toolchain.dart'; |
Adam Barth | bdd2066 | 2015-10-11 19:42:50 -0700 | [diff] [blame] | 18 | import 'flutter_command_runner.dart'; |
| 19 | |
Collin Jackson | beaffec | 2016-01-29 12:54:15 -0800 | [diff] [blame] | 20 | typedef bool Validator(); |
| 21 | |
Adam Barth | bdd2066 | 2015-10-11 19:42:50 -0700 | [diff] [blame] | 22 | abstract class FlutterCommand extends Command { |
Devon Carew | f8374cd | 2016-03-16 14:37:46 -0700 | [diff] [blame] | 23 | FlutterCommand() { |
| 24 | commandValidator = _commandValidator; |
| 25 | } |
| 26 | |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 27 | @override |
Adam Barth | bdd2066 | 2015-10-11 19:42:50 -0700 | [diff] [blame] | 28 | FlutterCommandRunner get runner => super.runner; |
| 29 | |
Devon Carew | 494d1e0 | 2015-10-28 20:57:51 -0700 | [diff] [blame] | 30 | /// Whether this command needs to be run from the root of a project. |
Steve Messick | 913315b | 2016-03-07 05:58:02 -0800 | [diff] [blame] | 31 | bool get requiresProjectRoot => true; |
Devon Carew | 494d1e0 | 2015-10-28 20:57:51 -0700 | [diff] [blame] | 32 | |
Devon Carew | 37290d8 | 2016-02-24 10:06:59 -0800 | [diff] [blame] | 33 | /// Whether this command requires a (single) Flutter target device to be connected. |
| 34 | bool get requiresDevice => false; |
| 35 | |
| 36 | /// Whether this command only applies to Android devices. |
| 37 | bool get androidOnly => false; |
| 38 | |
Devon Carew | f8374cd | 2016-03-16 14:37:46 -0700 | [diff] [blame] | 39 | /// Whether this command uses the 'target' option. |
| 40 | bool _usesTargetOption = false; |
| 41 | |
| 42 | bool _usesPubOption = false; |
Steve Messick | 913315b | 2016-03-07 05:58:02 -0800 | [diff] [blame] | 43 | |
Devon Carew | b172dd5 | 2016-04-13 19:55:28 -0700 | [diff] [blame] | 44 | bool get shouldRunPub => _usesPubOption && argResults['pub']; |
| 45 | |
Ian Hickson | 3419068 | 2015-11-09 00:30:45 -0800 | [diff] [blame] | 46 | List<BuildConfiguration> get buildConfigurations => runner.buildConfigurations; |
| 47 | |
Devon Carew | f8374cd | 2016-03-16 14:37:46 -0700 | [diff] [blame] | 48 | void usesTargetOption() { |
| 49 | argParser.addOption('target', |
| 50 | abbr: 't', |
| 51 | defaultsTo: flx.defaultMainPath, |
| 52 | help: 'Target app path / main entry-point file.'); |
| 53 | _usesTargetOption = true; |
| 54 | } |
| 55 | |
| 56 | void usesPubOption() { |
| 57 | argParser.addFlag('pub', |
| 58 | defaultsTo: true, |
| 59 | help: 'Whether to run "pub get" before executing this command.'); |
| 60 | _usesPubOption = true; |
| 61 | } |
| 62 | |
Devon Carew | 1c0a966 | 2016-04-18 16:41:15 -0700 | [diff] [blame] | 63 | void addBuildModeFlags() { |
| 64 | argParser.addFlag('debug', |
| 65 | negatable: false, |
| 66 | help: 'Build a debug version of your app (the default).'); |
Jason Simmons | 07951ee | 2016-04-25 13:36:31 -0700 | [diff] [blame^] | 67 | argParser.addFlag('profile', |
| 68 | hide: true, |
| 69 | negatable: false, |
| 70 | help: 'Build a profile (ahead of time compilation) version of your app.'); |
Devon Carew | 6f0bb20 | 2016-04-19 11:17:00 -0700 | [diff] [blame] | 71 | argParser.addFlag('release', |
Devon Carew | 1c0a966 | 2016-04-18 16:41:15 -0700 | [diff] [blame] | 72 | negatable: false, |
Devon Carew | 6f0bb20 | 2016-04-19 11:17:00 -0700 | [diff] [blame] | 73 | help: 'Build a release version of your app.'); |
Devon Carew | 1c0a966 | 2016-04-18 16:41:15 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | BuildMode getBuildMode() { |
Jason Simmons | 07951ee | 2016-04-25 13:36:31 -0700 | [diff] [blame^] | 77 | List<bool> modeFlags = [argResults['debug'], argResults['profile'], argResults['release']]; |
| 78 | if (modeFlags.where((bool flag) => flag).length > 1) |
| 79 | throw new UsageException('Only one of --debug, --profile, or --release should be specified.', null); |
Devon Carew | 1c0a966 | 2016-04-18 16:41:15 -0700 | [diff] [blame] | 80 | |
| 81 | BuildMode mode = BuildMode.debug; |
Jason Simmons | 07951ee | 2016-04-25 13:36:31 -0700 | [diff] [blame^] | 82 | if (argResults['profile']) |
| 83 | mode = BuildMode.profile; |
Devon Carew | 6f0bb20 | 2016-04-19 11:17:00 -0700 | [diff] [blame] | 84 | if (argResults['release']) |
| 85 | mode = BuildMode.release; |
Devon Carew | 1c0a966 | 2016-04-18 16:41:15 -0700 | [diff] [blame] | 86 | return mode; |
| 87 | } |
| 88 | |
Devon Carew | 653566d | 2016-04-04 13:10:56 -0700 | [diff] [blame] | 89 | void _setupToolchain() { |
| 90 | toolchain ??= Toolchain.forConfigs(buildConfigurations); |
Adam Barth | bdd2066 | 2015-10-11 19:42:50 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Devon Carew | 653566d | 2016-04-04 13:10:56 -0700 | [diff] [blame] | 93 | void _setupApplicationPackages() { |
Devon Carew | f132aca | 2016-04-15 21:08:03 -0700 | [diff] [blame] | 94 | applicationPackages ??= new ApplicationPackageStore(); |
Devon Carew | 37290d8 | 2016-02-24 10:06:59 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 97 | @override |
Devon Carew | 4d93c37 | 2016-02-26 23:54:18 -0800 | [diff] [blame] | 98 | Future<int> run() { |
| 99 | Stopwatch stopwatch = new Stopwatch()..start(); |
| 100 | |
| 101 | return _run().then((int exitCode) { |
Devon Carew | 4daee0c | 2016-03-14 09:41:00 -0700 | [diff] [blame] | 102 | int ms = stopwatch.elapsedMilliseconds; |
| 103 | printTrace("'flutter $name' took ${ms}ms; exiting with code $exitCode."); |
Devon Carew | 4d93c37 | 2016-02-26 23:54:18 -0800 | [diff] [blame] | 104 | return exitCode; |
| 105 | }); |
| 106 | } |
| 107 | |
| 108 | Future<int> _run() async { |
Devon Carew | f8374cd | 2016-03-16 14:37:46 -0700 | [diff] [blame] | 109 | if (requiresProjectRoot && !commandValidator()) |
Adam Barth | c7e0044 | 2015-11-23 13:31:24 -0800 | [diff] [blame] | 110 | return 1; |
Devon Carew | 37290d8 | 2016-02-24 10:06:59 -0800 | [diff] [blame] | 111 | |
| 112 | // Ensure at least one toolchain is installed. |
| 113 | if (requiresDevice && !doctor.canLaunchAnything) { |
| 114 | printError("Unable to locate a development device; please run 'flutter doctor' " |
| 115 | "for information about installing additional components."); |
| 116 | return 1; |
| 117 | } |
| 118 | |
| 119 | // Validate devices. |
| 120 | if (requiresDevice) { |
| 121 | List<Device> devices = await deviceManager.getDevices(); |
| 122 | |
| 123 | if (devices.isEmpty && deviceManager.hasSpecifiedDeviceId) { |
| 124 | printError("No device found with id '${deviceManager.specifiedDeviceId}'."); |
| 125 | return 1; |
| 126 | } else if (devices.isEmpty) { |
| 127 | printStatus('No connected devices.'); |
| 128 | return 1; |
| 129 | } |
| 130 | |
| 131 | devices = devices.where((Device device) => device.isSupported()).toList(); |
| 132 | |
| 133 | if (androidOnly) |
Devon Carew | e939b15 | 2016-03-12 11:08:21 -0800 | [diff] [blame] | 134 | devices = devices.where((Device device) => device.platform == TargetPlatform.android_arm).toList(); |
Devon Carew | 37290d8 | 2016-02-24 10:06:59 -0800 | [diff] [blame] | 135 | |
Devon Carew | 37290d8 | 2016-02-24 10:06:59 -0800 | [diff] [blame] | 136 | if (devices.isEmpty) { |
| 137 | printStatus('No supported devices connected.'); |
| 138 | return 1; |
Devon Carew | 5ad6a57 | 2016-03-11 08:49:55 -0800 | [diff] [blame] | 139 | } else if (devices.length > 1) { |
| 140 | printStatus("More than one device connected; please specify a device with " |
| 141 | "the '-d <deviceId>' flag."); |
| 142 | printStatus(''); |
| 143 | devices = await deviceManager.getAllConnectedDevices(); |
Devon Carew | 4daee0c | 2016-03-14 09:41:00 -0700 | [diff] [blame] | 144 | Device.printDevices(devices); |
Devon Carew | 5ad6a57 | 2016-03-11 08:49:55 -0800 | [diff] [blame] | 145 | return 1; |
| 146 | } else { |
| 147 | _deviceForCommand = devices.single; |
Devon Carew | 37290d8 | 2016-02-24 10:06:59 -0800 | [diff] [blame] | 148 | } |
Devon Carew | 37290d8 | 2016-02-24 10:06:59 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Devon Carew | b172dd5 | 2016-04-13 19:55:28 -0700 | [diff] [blame] | 151 | if (shouldRunPub) { |
Devon Carew | f8374cd | 2016-03-16 14:37:46 -0700 | [diff] [blame] | 152 | int exitCode = await pubGet(); |
| 153 | if (exitCode != 0) |
| 154 | return exitCode; |
| 155 | } |
| 156 | |
Devon Carew | 4059844 | 2016-04-07 09:15:58 -0700 | [diff] [blame] | 157 | // Populate the cache. |
Devon Carew | a729b02 | 2016-04-07 13:31:44 -0700 | [diff] [blame] | 158 | await cache.updateAll(); |
Devon Carew | 4059844 | 2016-04-07 09:15:58 -0700 | [diff] [blame] | 159 | |
Devon Carew | 653566d | 2016-04-04 13:10:56 -0700 | [diff] [blame] | 160 | _setupToolchain(); |
| 161 | _setupApplicationPackages(); |
| 162 | |
Hixie | a0227ca | 2015-11-11 10:29:05 -0800 | [diff] [blame] | 163 | return await runInProject(); |
Devon Carew | 494d1e0 | 2015-10-28 20:57:51 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Collin Jackson | beaffec | 2016-01-29 12:54:15 -0800 | [diff] [blame] | 166 | // This is a field so that you can modify the value for testing. |
Devon Carew | f8374cd | 2016-03-16 14:37:46 -0700 | [diff] [blame] | 167 | Validator commandValidator; |
| 168 | |
| 169 | bool _commandValidator() { |
Adam Barth | c7e0044 | 2015-11-23 13:31:24 -0800 | [diff] [blame] | 170 | if (!FileSystemEntity.isFileSync('pubspec.yaml')) { |
Collin Jackson | beaffec | 2016-01-29 12:54:15 -0800 | [diff] [blame] | 171 | printError('Error: No pubspec.yaml file found.\n' |
| 172 | 'This command should be run from the root of your Flutter project.\n' |
| 173 | 'Do not run this command from the root of your git clone of Flutter.'); |
Adam Barth | c7e0044 | 2015-11-23 13:31:24 -0800 | [diff] [blame] | 174 | return false; |
| 175 | } |
Devon Carew | f8374cd | 2016-03-16 14:37:46 -0700 | [diff] [blame] | 176 | |
| 177 | if (_usesTargetOption) { |
| 178 | String targetPath = argResults['target']; |
| 179 | if (!FileSystemEntity.isFileSync(targetPath)) { |
| 180 | printError('Target file "$targetPath" not found.'); |
| 181 | return false; |
| 182 | } |
| 183 | } |
| 184 | |
Jason Simmons | f7b1799 | 2016-04-08 09:34:42 -0700 | [diff] [blame] | 185 | // Validate the current package map only if we will not be running "pub get" later. |
| 186 | if (!(_usesPubOption && argResults['pub'])) { |
| 187 | String error = PackageMap.instance.checkValid(); |
| 188 | if (error != null) { |
| 189 | printError(error); |
| 190 | return false; |
| 191 | } |
Adam Barth | 2e062df | 2016-03-28 13:07:42 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Adam Barth | c7e0044 | 2015-11-23 13:31:24 -0800 | [diff] [blame] | 194 | return true; |
Devon Carew | f8374cd | 2016-03-16 14:37:46 -0700 | [diff] [blame] | 195 | } |
Adam Barth | c7e0044 | 2015-11-23 13:31:24 -0800 | [diff] [blame] | 196 | |
Devon Carew | 494d1e0 | 2015-10-28 20:57:51 -0700 | [diff] [blame] | 197 | Future<int> runInProject(); |
| 198 | |
Devon Carew | 37290d8 | 2016-02-24 10:06:59 -0800 | [diff] [blame] | 199 | // This is caculated in run() if the command has [requiresDevice] specified. |
Devon Carew | 5ad6a57 | 2016-03-11 08:49:55 -0800 | [diff] [blame] | 200 | Device _deviceForCommand; |
| 201 | |
| 202 | Device get deviceForCommand => _deviceForCommand; |
Devon Carew | 37290d8 | 2016-02-24 10:06:59 -0800 | [diff] [blame] | 203 | |
Adam Barth | bdd2066 | 2015-10-11 19:42:50 -0700 | [diff] [blame] | 204 | ApplicationPackageStore applicationPackages; |
Adam Barth | 12f7581 | 2015-10-13 10:00:06 -0700 | [diff] [blame] | 205 | Toolchain toolchain; |
Adam Barth | bdd2066 | 2015-10-11 19:42:50 -0700 | [diff] [blame] | 206 | } |