Ian Hickson | 449f4a6 | 2019-11-27 15:04:02 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 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 | 7ae584b | 2016-06-03 12:54:27 -0700 | [diff] [blame] | 6 | import 'dart:math' as math; |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 7 | |
Jonah Williams | 79323f7 | 2019-10-30 19:40:19 -0700 | [diff] [blame] | 8 | import 'package:meta/meta.dart'; |
| 9 | |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 10 | import '../application_package.dart'; |
Dan Rubel | e20ee04 | 2016-10-13 10:44:20 -0400 | [diff] [blame] | 11 | import '../base/common.dart'; |
Yegor Jbanov | 677e63b | 2016-02-25 15:58:09 -0800 | [diff] [blame] | 12 | import '../base/context.dart'; |
Todd Volkert | 8bb2703 | 2017-01-06 16:51:44 -0800 | [diff] [blame] | 13 | import '../base/file_system.dart'; |
Todd Volkert | 016b5ab | 2017-01-09 08:37:00 -0800 | [diff] [blame] | 14 | import '../base/io.dart'; |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 15 | import '../base/process.dart'; |
Devon Carew | 9d9836f | 2018-07-09 12:22:46 -0700 | [diff] [blame] | 16 | import '../base/utils.dart'; |
Jason Simmons | a590ee2 | 2016-05-12 12:22:15 -0700 | [diff] [blame] | 17 | import '../build_info.dart'; |
Emmanuel Garcia | d9c1962 | 2019-06-11 14:46:00 -0700 | [diff] [blame] | 18 | import '../bundle.dart'; |
Jonah Williams | 91fd89e | 2019-01-25 16:16:26 -0800 | [diff] [blame] | 19 | import '../convert.dart'; |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 20 | import '../device.dart'; |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 21 | import '../globals.dart' as globals; |
Jonah Williams | 6b19184 | 2019-04-25 12:25:12 -0700 | [diff] [blame] | 22 | import '../project.dart'; |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 23 | import '../protocol_discovery.dart'; |
Chris Bracken | 615410d | 2017-06-15 16:11:08 -0700 | [diff] [blame] | 24 | import 'ios_workflow.dart'; |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 25 | import 'mac.dart'; |
Todd Volkert | c22ce95 | 2019-08-16 17:10:07 -0700 | [diff] [blame] | 26 | import 'plist_parser.dart'; |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 27 | |
| 28 | const String _xcrunPath = '/usr/bin/xcrun'; |
Danny Tuppeny | f3be1d9 | 2019-06-26 16:39:23 +0100 | [diff] [blame] | 29 | const String iosSimulatorId = 'apple_ios_simulator'; |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 30 | |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 31 | class IOSSimulators extends PollingDeviceDiscovery { |
Todd Volkert | 6a4b08b | 2017-05-03 16:12:08 -0700 | [diff] [blame] | 32 | IOSSimulators() : super('iOS simulators'); |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 33 | |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 34 | @override |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 35 | bool get supportsPlatform => globals.platform.isMacOS; |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 36 | |
| 37 | @override |
Chris Bracken | 615410d | 2017-06-15 16:11:08 -0700 | [diff] [blame] | 38 | bool get canListAnything => iosWorkflow.canListDevices; |
Todd Volkert | 6a4b08b | 2017-05-03 16:12:08 -0700 | [diff] [blame] | 39 | |
| 40 | @override |
Chris Bracken | 1d9f009 | 2017-06-19 13:14:57 -0700 | [diff] [blame] | 41 | Future<List<Device>> pollingGetDevices() async => IOSSimulatorUtils.instance.getAttachedDevices(); |
Yegor Jbanov | 677e63b | 2016-02-25 15:58:09 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | class IOSSimulatorUtils { |
| 45 | /// Returns [IOSSimulatorUtils] active in the current app context (i.e. zone). |
Jonah Williams | 0acd3e6 | 2019-04-25 15:51:08 -0700 | [diff] [blame] | 46 | static IOSSimulatorUtils get instance => context.get<IOSSimulatorUtils>(); |
Yegor Jbanov | 677e63b | 2016-02-25 15:58:09 -0800 | [diff] [blame] | 47 | |
Jonah Williams | 2185825 | 2019-08-10 00:57:23 -0700 | [diff] [blame] | 48 | Future<List<IOSSimulator>> getAttachedDevices() async { |
Jenn Magder | 91f7902 | 2020-01-25 11:18:02 -0800 | [diff] [blame] | 49 | if (!globals.xcode.isInstalledAndMeetsVersionCheck) { |
Yegor Jbanov | 677e63b | 2016-02-25 15:58:09 -0800 | [diff] [blame] | 50 | return <IOSSimulator>[]; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 51 | } |
Yegor Jbanov | 677e63b | 2016-02-25 15:58:09 -0800 | [diff] [blame] | 52 | |
Jonah Williams | 2185825 | 2019-08-10 00:57:23 -0700 | [diff] [blame] | 53 | final List<SimDevice> connected = await SimControl.instance.getConnectedDevices(); |
| 54 | return connected.map<IOSSimulator>((SimDevice device) { |
Jonah Williams | 5c52498 | 2019-06-18 15:23:14 -0700 | [diff] [blame] | 55 | return IOSSimulator(device.udid, name: device.name, simulatorCategory: device.category); |
Yegor Jbanov | 677e63b | 2016-02-25 15:58:09 -0800 | [diff] [blame] | 56 | }).toList(); |
| 57 | } |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /// A wrapper around the `simctl` command line tool. |
| 61 | class SimControl { |
Yegor Jbanov | 677e63b | 2016-02-25 15:58:09 -0800 | [diff] [blame] | 62 | /// Returns [SimControl] active in the current app context (i.e. zone). |
Jonah Williams | 0acd3e6 | 2019-04-25 15:51:08 -0700 | [diff] [blame] | 63 | static SimControl get instance => context.get<SimControl>(); |
Yegor Jbanov | 677e63b | 2016-02-25 15:58:09 -0800 | [diff] [blame] | 64 | |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 65 | /// Runs `simctl list --json` and returns the JSON of the corresponding |
| 66 | /// [section]. |
Jonah Williams | 2185825 | 2019-08-10 00:57:23 -0700 | [diff] [blame] | 67 | Future<Map<String, dynamic>> _list(SimControlListSection section) async { |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 68 | // Sample output from `simctl list --json`: |
| 69 | // |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 70 | // { |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 71 | // "devicetypes": { ... }, |
| 72 | // "runtimes": { ... }, |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 73 | // "devices" : { |
| 74 | // "com.apple.CoreSimulator.SimRuntime.iOS-8-2" : [ |
| 75 | // { |
| 76 | // "state" : "Shutdown", |
| 77 | // "availability" : " (unavailable, runtime profile not found)", |
| 78 | // "name" : "iPhone 4s", |
| 79 | // "udid" : "1913014C-6DCB-485D-AC6B-7CD76D322F5B" |
| 80 | // }, |
| 81 | // ... |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 82 | // }, |
| 83 | // "pairs": { ... }, |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 84 | |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 85 | final List<String> command = <String>[_xcrunPath, 'simctl', 'list', '--json', section.name]; |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 86 | globals.printTrace(command.join(' ')); |
| 87 | final ProcessResult results = await globals.processManager.run(command); |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 88 | if (results.exitCode != 0) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 89 | globals.printError('Error executing simctl: ${results.exitCode}\n${results.stderr}'); |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 90 | return <String, Map<String, dynamic>>{}; |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 91 | } |
Jonah Williams | 2185825 | 2019-08-10 00:57:23 -0700 | [diff] [blame] | 92 | try { |
Jonah Williams | ca5411e | 2019-08-12 15:21:28 -0700 | [diff] [blame] | 93 | final Object decodeResult = json.decode(results.stdout?.toString())[section.name]; |
| 94 | if (decodeResult is Map<String, dynamic>) { |
| 95 | return decodeResult; |
| 96 | } |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 97 | globals.printError('simctl returned unexpected JSON response: ${results.stdout}'); |
Jonah Williams | ca5411e | 2019-08-12 15:21:28 -0700 | [diff] [blame] | 98 | return <String, dynamic>{}; |
Jonah Williams | 2185825 | 2019-08-10 00:57:23 -0700 | [diff] [blame] | 99 | } on FormatException { |
| 100 | // We failed to parse the simctl output, or it returned junk. |
| 101 | // One known message is "Install Started" isn't valid JSON but is |
| 102 | // returned sometimes. |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 103 | globals.printError('simctl returned non-JSON response: ${results.stdout}'); |
Jonah Williams | 2185825 | 2019-08-10 00:57:23 -0700 | [diff] [blame] | 104 | return <String, dynamic>{}; |
| 105 | } |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /// Returns a list of all available devices, both potential and connected. |
Jonah Williams | 2185825 | 2019-08-10 00:57:23 -0700 | [diff] [blame] | 109 | Future<List<SimDevice>> getDevices() async { |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 110 | final List<SimDevice> devices = <SimDevice>[]; |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 111 | |
Jonah Williams | 2185825 | 2019-08-10 00:57:23 -0700 | [diff] [blame] | 112 | final Map<String, dynamic> devicesSection = await _list(SimControlListSection.devices); |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 113 | |
Alexandre Ardhuin | 4f9b6cf | 2020-01-07 16:32:04 +0100 | [diff] [blame] | 114 | for (final String deviceCategory in devicesSection.keys) { |
Jonah Williams | ca5411e | 2019-08-12 15:21:28 -0700 | [diff] [blame] | 115 | final Object devicesData = devicesSection[deviceCategory]; |
| 116 | if (devicesData != null && devicesData is List<dynamic>) { |
Alexandre Ardhuin | 4f9b6cf | 2020-01-07 16:32:04 +0100 | [diff] [blame] | 117 | for (final Map<String, dynamic> data in devicesData.map<Map<String, dynamic>>(castStringKeyedMap)) { |
Jonah Williams | ca5411e | 2019-08-12 15:21:28 -0700 | [diff] [blame] | 118 | devices.add(SimDevice(deviceCategory, data)); |
| 119 | } |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | return devices; |
| 124 | } |
| 125 | |
| 126 | /// Returns all the connected simulator devices. |
Jonah Williams | 2185825 | 2019-08-10 00:57:23 -0700 | [diff] [blame] | 127 | Future<List<SimDevice>> getConnectedDevices() async { |
| 128 | final List<SimDevice> simDevices = await getDevices(); |
| 129 | return simDevices.where((SimDevice device) => device.isBooted).toList(); |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 130 | } |
| 131 | |
xster | 432ffde | 2017-06-15 18:25:09 -0700 | [diff] [blame] | 132 | Future<bool> isInstalled(String deviceId, String appId) { |
Zachary Anderson | 73c10e8 | 2019-09-11 18:20:42 -0700 | [diff] [blame] | 133 | return processUtils.exitsHappy(<String>[ |
Todd Volkert | 51d4ab3 | 2016-05-27 11:05:10 -0700 | [diff] [blame] | 134 | _xcrunPath, |
| 135 | 'simctl', |
| 136 | 'get_app_container', |
xster | 432ffde | 2017-06-15 18:25:09 -0700 | [diff] [blame] | 137 | deviceId, |
Todd Volkert | 51d4ab3 | 2016-05-27 11:05:10 -0700 | [diff] [blame] | 138 | appId, |
| 139 | ]); |
| 140 | } |
| 141 | |
Devon Carew | 9d9836f | 2018-07-09 12:22:46 -0700 | [diff] [blame] | 142 | Future<RunResult> install(String deviceId, String appPath) { |
Greg Spencer | c81f4c7 | 2018-10-08 19:11:31 -0700 | [diff] [blame] | 143 | Future<RunResult> result; |
| 144 | try { |
Zachary Anderson | 73c10e8 | 2019-09-11 18:20:42 -0700 | [diff] [blame] | 145 | result = processUtils.run( |
| 146 | <String>[_xcrunPath, 'simctl', 'install', deviceId, appPath], |
| 147 | throwOnError: true, |
| 148 | ); |
Greg Spencer | c81f4c7 | 2018-10-08 19:11:31 -0700 | [diff] [blame] | 149 | } on ProcessException catch (exception) { |
| 150 | throwToolExit('Unable to install $appPath on $deviceId:\n$exception'); |
| 151 | } |
| 152 | return result; |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Devon Carew | 9d9836f | 2018-07-09 12:22:46 -0700 | [diff] [blame] | 155 | Future<RunResult> uninstall(String deviceId, String appId) { |
Greg Spencer | c81f4c7 | 2018-10-08 19:11:31 -0700 | [diff] [blame] | 156 | Future<RunResult> result; |
| 157 | try { |
Zachary Anderson | 73c10e8 | 2019-09-11 18:20:42 -0700 | [diff] [blame] | 158 | result = processUtils.run( |
| 159 | <String>[_xcrunPath, 'simctl', 'uninstall', deviceId, appId], |
| 160 | throwOnError: true, |
| 161 | ); |
Greg Spencer | c81f4c7 | 2018-10-08 19:11:31 -0700 | [diff] [blame] | 162 | } on ProcessException catch (exception) { |
| 163 | throwToolExit('Unable to uninstall $appId from $deviceId:\n$exception'); |
| 164 | } |
| 165 | return result; |
Todd Volkert | 51d4ab3 | 2016-05-27 11:05:10 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 168 | Future<RunResult> launch(String deviceId, String appIdentifier, [ List<String> launchArgs ]) { |
Greg Spencer | c81f4c7 | 2018-10-08 19:11:31 -0700 | [diff] [blame] | 169 | Future<RunResult> result; |
| 170 | try { |
Zachary Anderson | 73c10e8 | 2019-09-11 18:20:42 -0700 | [diff] [blame] | 171 | result = processUtils.run( |
| 172 | <String>[ |
| 173 | _xcrunPath, |
| 174 | 'simctl', |
| 175 | 'launch', |
| 176 | deviceId, |
| 177 | appIdentifier, |
| 178 | ...?launchArgs, |
| 179 | ], |
| 180 | throwOnError: true, |
| 181 | ); |
Greg Spencer | c81f4c7 | 2018-10-08 19:11:31 -0700 | [diff] [blame] | 182 | } on ProcessException catch (exception) { |
| 183 | throwToolExit('Unable to launch $appIdentifier on $deviceId:\n$exception'); |
| 184 | } |
| 185 | return result; |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 186 | } |
xster | 3985ddb | 2017-02-16 12:50:27 -0800 | [diff] [blame] | 187 | |
Devon Carew | 9d9836f | 2018-07-09 12:22:46 -0700 | [diff] [blame] | 188 | Future<void> takeScreenshot(String deviceId, String outputPath) async { |
Greg Spencer | c81f4c7 | 2018-10-08 19:11:31 -0700 | [diff] [blame] | 189 | try { |
Zachary Anderson | 73c10e8 | 2019-09-11 18:20:42 -0700 | [diff] [blame] | 190 | await processUtils.run( |
| 191 | <String>[_xcrunPath, 'simctl', 'io', deviceId, 'screenshot', outputPath], |
| 192 | throwOnError: true, |
| 193 | ); |
Greg Spencer | c81f4c7 | 2018-10-08 19:11:31 -0700 | [diff] [blame] | 194 | } on ProcessException catch (exception) { |
| 195 | throwToolExit('Unable to take screenshot of $deviceId:\n$exception'); |
| 196 | } |
xster | 3985ddb | 2017-02-16 12:50:27 -0800 | [diff] [blame] | 197 | } |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 200 | /// Enumerates all data sections of `xcrun simctl list --json` command. |
| 201 | class SimControlListSection { |
Ian Hickson | d745e20 | 2016-03-12 00:32:34 -0800 | [diff] [blame] | 202 | const SimControlListSection._(this.name); |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 203 | |
| 204 | final String name; |
Ian Hickson | d745e20 | 2016-03-12 00:32:34 -0800 | [diff] [blame] | 205 | |
Alexandre Ardhuin | eda03e2 | 2018-08-02 12:02:32 +0200 | [diff] [blame] | 206 | static const SimControlListSection devices = SimControlListSection._('devices'); |
| 207 | static const SimControlListSection devicetypes = SimControlListSection._('devicetypes'); |
| 208 | static const SimControlListSection runtimes = SimControlListSection._('runtimes'); |
| 209 | static const SimControlListSection pairs = SimControlListSection._('pairs'); |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Yegor Jbanov | 225686b | 2016-03-14 22:31:32 -0700 | [diff] [blame] | 212 | /// A simulated device type. |
| 213 | /// |
| 214 | /// Simulated device types can be listed using the command |
| 215 | /// `xcrun simctl list devicetypes`. |
| 216 | class SimDeviceType { |
| 217 | SimDeviceType(this.name, this.identifier); |
| 218 | |
| 219 | /// The name of the device type. |
| 220 | /// |
| 221 | /// Examples: |
| 222 | /// |
| 223 | /// "iPhone 6s" |
| 224 | /// "iPhone 6 Plus" |
| 225 | final String name; |
| 226 | |
| 227 | /// The identifier of the device type. |
| 228 | /// |
| 229 | /// Examples: |
| 230 | /// |
| 231 | /// "com.apple.CoreSimulator.SimDeviceType.iPhone-6s" |
| 232 | /// "com.apple.CoreSimulator.SimDeviceType.iPhone-6-Plus" |
| 233 | final String identifier; |
| 234 | } |
| 235 | |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 236 | class SimDevice { |
| 237 | SimDevice(this.category, this.data); |
| 238 | |
| 239 | final String category; |
Mark Fielbig | af5d4c6 | 2018-06-30 17:17:35 -0700 | [diff] [blame] | 240 | final Map<String, dynamic> data; |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 241 | |
Jonah Williams | ca5411e | 2019-08-12 15:21:28 -0700 | [diff] [blame] | 242 | String get state => data['state']?.toString(); |
| 243 | String get availability => data['availability']?.toString(); |
| 244 | String get name => data['name']?.toString(); |
| 245 | String get udid => data['udid']?.toString(); |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 246 | |
| 247 | bool get isBooted => state == 'Booted'; |
| 248 | } |
| 249 | |
| 250 | class IOSSimulator extends Device { |
Jonah Williams | 5c52498 | 2019-06-18 15:23:14 -0700 | [diff] [blame] | 251 | IOSSimulator(String id, { this.name, this.simulatorCategory }) : super( |
| 252 | id, |
| 253 | category: Category.mobile, |
| 254 | platformType: PlatformType.ios, |
| 255 | ephemeral: true, |
| 256 | ); |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 257 | |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 258 | @override |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 259 | final String name; |
| 260 | |
Jonah Williams | 5c52498 | 2019-06-18 15:23:14 -0700 | [diff] [blame] | 261 | final String simulatorCategory; |
Dan Rubel | 0b49d81 | 2016-10-27 09:07:21 +0100 | [diff] [blame] | 262 | |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 263 | @override |
Todd Volkert | 60c5ffc | 2017-04-25 17:23:00 -0700 | [diff] [blame] | 264 | Future<bool> get isLocalEmulator async => true; |
Yegor Jbanov | 677e63b | 2016-02-25 15:58:09 -0800 | [diff] [blame] | 265 | |
John McCutchan | 0de6916 | 2016-07-20 12:59:30 -0700 | [diff] [blame] | 266 | @override |
Danny Tuppeny | f3be1d9 | 2019-06-26 16:39:23 +0100 | [diff] [blame] | 267 | Future<String> get emulatorId async => iosSimulatorId; |
| 268 | |
| 269 | @override |
Jonah Williams | c11633e | 2018-11-09 10:33:22 -0800 | [diff] [blame] | 270 | bool get supportsHotReload => true; |
| 271 | |
| 272 | @override |
| 273 | bool get supportsHotRestart => true; |
John McCutchan | 0de6916 | 2016-07-20 12:59:30 -0700 | [diff] [blame] | 274 | |
Todd Volkert | 9cb914d | 2016-11-16 17:19:00 -0800 | [diff] [blame] | 275 | Map<ApplicationPackage, _IOSSimulatorLogReader> _logReaders; |
John McCutchan | 5e140b7 | 2016-03-10 15:48:37 -0800 | [diff] [blame] | 276 | _IOSSimulatorDevicePortForwarder _portForwarder; |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 277 | |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 278 | String get xcrunPath => globals.fs.path.join('/usr', 'bin', 'xcrun'); |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 279 | |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 280 | @override |
Todd Volkert | 60c5ffc | 2017-04-25 17:23:00 -0700 | [diff] [blame] | 281 | Future<bool> isAppInstalled(ApplicationPackage app) { |
xster | 432ffde | 2017-06-15 18:25:09 -0700 | [diff] [blame] | 282 | return SimControl.instance.isInstalled(id, app.id); |
Todd Volkert | 51d4ab3 | 2016-05-27 11:05:10 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | @override |
Todd Volkert | 60c5ffc | 2017-04-25 17:23:00 -0700 | [diff] [blame] | 286 | Future<bool> isLatestBuildInstalled(ApplicationPackage app) async => false; |
Jakob Andersen | a745fd5 | 2017-02-22 14:35:49 +0100 | [diff] [blame] | 287 | |
| 288 | @override |
Jonah Williams | ca5411e | 2019-08-12 15:21:28 -0700 | [diff] [blame] | 289 | Future<bool> installApp(covariant IOSApp app) async { |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 290 | try { |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 291 | final IOSApp iosApp = app; |
xster | e28765a | 2017-09-20 17:24:43 -0700 | [diff] [blame] | 292 | await SimControl.instance.install(id, iosApp.simulatorBundlePath); |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 293 | return true; |
Zachary Anderson | 483f499 | 2020-02-26 23:42:19 -0800 | [diff] [blame] | 294 | } catch (e) { |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 295 | return false; |
| 296 | } |
| 297 | } |
| 298 | |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 299 | @override |
Todd Volkert | 60c5ffc | 2017-04-25 17:23:00 -0700 | [diff] [blame] | 300 | Future<bool> uninstallApp(ApplicationPackage app) async { |
Todd Volkert | 51d4ab3 | 2016-05-27 11:05:10 -0700 | [diff] [blame] | 301 | try { |
Todd Volkert | 60c5ffc | 2017-04-25 17:23:00 -0700 | [diff] [blame] | 302 | await SimControl.instance.uninstall(id, app.id); |
Todd Volkert | 51d4ab3 | 2016-05-27 11:05:10 -0700 | [diff] [blame] | 303 | return true; |
Zachary Anderson | 483f499 | 2020-02-26 23:42:19 -0800 | [diff] [blame] | 304 | } catch (e) { |
Todd Volkert | 51d4ab3 | 2016-05-27 11:05:10 -0700 | [diff] [blame] | 305 | return false; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | @override |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 310 | bool isSupported() { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 311 | if (!globals.platform.isMacOS) { |
Chris Bracken | cce70d7 | 2017-02-03 12:54:07 -0800 | [diff] [blame] | 312 | _supportMessage = 'iOS devices require a Mac host machine.'; |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 313 | return false; |
| 314 | } |
| 315 | |
Chris Bracken | a751678 | 2018-05-09 13:34:39 -0700 | [diff] [blame] | 316 | // Check if the device is part of a blacklisted category. |
| 317 | // We do not yet support WatchOS or tvOS devices. |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 318 | final RegExp blacklist = RegExp(r'Apple (TV|Watch)', caseSensitive: false); |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 319 | if (blacklist.hasMatch(name)) { |
Chris Bracken | a751678 | 2018-05-09 13:34:39 -0700 | [diff] [blame] | 320 | _supportMessage = 'Flutter does not support Apple TV or Apple Watch.'; |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 321 | return false; |
| 322 | } |
Chris Bracken | fb72f21 | 2017-02-03 12:25:05 -0800 | [diff] [blame] | 323 | return true; |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | String _supportMessage; |
| 327 | |
| 328 | @override |
| 329 | String supportMessage() { |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 330 | if (isSupported()) { |
Chris Bracken | 104fcf8 | 2017-02-03 11:50:49 -0800 | [diff] [blame] | 331 | return 'Supported'; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 332 | } |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 333 | |
Alexandre Ardhuin | 4fa32df | 2019-05-16 22:25:51 +0200 | [diff] [blame] | 334 | return _supportMessage ?? 'Unknown'; |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | @override |
Devon Carew | b0dca79 | 2016-04-27 14:43:42 -0700 | [diff] [blame] | 338 | Future<LaunchResult> startApp( |
Jonah Williams | ca5411e | 2019-08-12 15:21:28 -0700 | [diff] [blame] | 339 | covariant IOSApp package, { |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 340 | String mainPath, |
| 341 | String route, |
Devon Carew | b0dca79 | 2016-04-27 14:43:42 -0700 | [diff] [blame] | 342 | DebuggingOptions debuggingOptions, |
John McCutchan | ca8070f | 2016-09-28 08:46:16 -0700 | [diff] [blame] | 343 | Map<String, dynamic> platformArgs, |
Alexandre Ardhuin | 09276be | 2018-06-05 08:50:40 +0200 | [diff] [blame] | 344 | bool prebuiltApplication = false, |
Alexandre Ardhuin | 09276be | 2018-06-05 08:50:40 +0200 | [diff] [blame] | 345 | bool ipv6 = false, |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 346 | }) async { |
Jonah Williams | ca5411e | 2019-08-12 15:21:28 -0700 | [diff] [blame] | 347 | if (!prebuiltApplication && package is BuildableIOSApp) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 348 | globals.printTrace('Building ${package.name} for $id.'); |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 349 | |
Dan Rubel | e384c0d | 2016-11-13 22:09:03 -0500 | [diff] [blame] | 350 | try { |
Jonah Williams | 6f71ce2 | 2019-08-27 14:20:51 -0700 | [diff] [blame] | 351 | await _setupUpdatedApplicationBundle(package, debuggingOptions.buildInfo, mainPath); |
Todd Volkert | 7536404 | 2016-12-06 10:09:00 -0800 | [diff] [blame] | 352 | } on ToolExit catch (e) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 353 | globals.printError(e.message); |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 354 | return LaunchResult.failed(); |
Dan Rubel | e384c0d | 2016-11-13 22:09:03 -0500 | [diff] [blame] | 355 | } |
John McCutchan | b8403c7 | 2016-11-23 07:33:33 -0800 | [diff] [blame] | 356 | } else { |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 357 | if (!await installApp(package)) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 358 | return LaunchResult.failed(); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 359 | } |
Todd Volkert | 904d524 | 2016-10-13 16:17:50 -0700 | [diff] [blame] | 360 | } |
Chinmay Garde | 9f4fdcb | 2016-03-02 11:40:10 -0800 | [diff] [blame] | 361 | |
| 362 | // Prepare launch arguments. |
Alexandre Ardhuin | df4bf45 | 2019-09-17 16:23:44 +0200 | [diff] [blame] | 363 | final List<String> args = <String>[ |
| 364 | '--enable-dart-profiling', |
| 365 | if (debuggingOptions.debuggingEnabled) ...<String>[ |
| 366 | if (debuggingOptions.buildInfo.isDebug) ...<String>[ |
sjindel-google | 95b34d7 | 2019-01-30 16:33:55 +0100 | [diff] [blame] | 367 | '--enable-checked-mode', |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 368 | '--verify-entry-points', |
Alexandre Ardhuin | df4bf45 | 2019-09-17 16:23:44 +0200 | [diff] [blame] | 369 | ], |
| 370 | if (debuggingOptions.startPaused) '--start-paused', |
| 371 | if (debuggingOptions.disableServiceAuthCodes) '--disable-service-auth-codes', |
| 372 | if (debuggingOptions.skiaDeterministicRendering) '--skia-deterministic-rendering', |
| 373 | if (debuggingOptions.useTestFonts) '--use-test-fonts', |
Jonah Williams | e3cb2c3 | 2019-11-13 16:02:46 -0800 | [diff] [blame] | 374 | '--observatory-port=${debuggingOptions.hostVmServicePort ?? 0}', |
Alexandre Ardhuin | 89427d6 | 2019-09-24 08:06:09 +0200 | [diff] [blame] | 375 | ], |
Alexandre Ardhuin | df4bf45 | 2019-09-17 16:23:44 +0200 | [diff] [blame] | 376 | ]; |
Chinmay Garde | 9f4fdcb | 2016-03-02 11:40:10 -0800 | [diff] [blame] | 377 | |
Dan Rubel | b265a8f | 2016-12-06 12:41:55 -0800 | [diff] [blame] | 378 | ProtocolDiscovery observatoryDiscovery; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 379 | if (debuggingOptions.debuggingEnabled) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 380 | observatoryDiscovery = ProtocolDiscovery.observatory( |
Jonah Williams | e3cb2c3 | 2019-11-13 16:02:46 -0800 | [diff] [blame] | 381 | getLogReader(app: package), |
| 382 | ipv6: ipv6, |
| 383 | hostPort: debuggingOptions.hostVmServicePort, |
| 384 | devicePort: debuggingOptions.deviceVmServicePort, |
| 385 | ); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 386 | } |
Dan Rubel | b265a8f | 2016-12-06 12:41:55 -0800 | [diff] [blame] | 387 | |
Chinmay Garde | 9f4fdcb | 2016-03-02 11:40:10 -0800 | [diff] [blame] | 388 | // Launch the updated application in the simulator. |
| 389 | try { |
Victor Maraccini | 74c6237 | 2019-05-01 14:21:43 -0300 | [diff] [blame] | 390 | // Use the built application's Info.plist to get the bundle identifier, |
| 391 | // which should always yield the correct value and does not require |
| 392 | // parsing the xcodeproj or configuration files. |
| 393 | // See https://github.com/flutter/flutter/issues/31037 for more information. |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 394 | final String plistPath = globals.fs.path.join(package.simulatorBundlePath, 'Info.plist'); |
Jonah Williams | 80619f1 | 2020-02-26 18:31:42 -0800 | [diff] [blame] | 395 | final String bundleIdentifier = globals.plistParser.getValueFromFile(plistPath, PlistParser.kCFBundleIdentifierKey); |
Victor Maraccini | 74c6237 | 2019-05-01 14:21:43 -0300 | [diff] [blame] | 396 | |
| 397 | await SimControl.instance.launch(id, bundleIdentifier, args); |
Zachary Anderson | 483f499 | 2020-02-26 23:42:19 -0800 | [diff] [blame] | 398 | } catch (error) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 399 | globals.printError('$error'); |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 400 | return LaunchResult.failed(); |
Chinmay Garde | 9f4fdcb | 2016-03-02 11:40:10 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Devon Carew | b0dca79 | 2016-04-27 14:43:42 -0700 | [diff] [blame] | 403 | if (!debuggingOptions.debuggingEnabled) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 404 | return LaunchResult.succeeded(); |
Dan Rubel | 93e662a | 2016-12-06 09:19:12 -0800 | [diff] [blame] | 405 | } |
Chinmay Garde | 9f4fdcb | 2016-03-02 11:40:10 -0800 | [diff] [blame] | 406 | |
Dan Rubel | 93e662a | 2016-12-06 09:19:12 -0800 | [diff] [blame] | 407 | // Wait for the service protocol port here. This will complete once the |
| 408 | // device has printed "Observatory is listening on..." |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 409 | globals.printTrace('Waiting for observatory port to be available...'); |
Dan Rubel | 93e662a | 2016-12-06 09:19:12 -0800 | [diff] [blame] | 410 | |
Dan Rubel | 93e662a | 2016-12-06 09:19:12 -0800 | [diff] [blame] | 411 | try { |
Todd Volkert | 10decc7 | 2017-05-16 08:25:51 -0700 | [diff] [blame] | 412 | final Uri deviceUri = await observatoryDiscovery.uri; |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 413 | return LaunchResult.succeeded(observatoryUri: deviceUri); |
Zachary Anderson | 483f499 | 2020-02-26 23:42:19 -0800 | [diff] [blame] | 414 | } catch (error) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 415 | globals.printError('Error waiting for a debug connection: $error'); |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 416 | return LaunchResult.failed(); |
Dan Rubel | 93e662a | 2016-12-06 09:19:12 -0800 | [diff] [blame] | 417 | } finally { |
xster | e28765a | 2017-09-20 17:24:43 -0700 | [diff] [blame] | 418 | await observatoryDiscovery.cancel(); |
Devon Carew | 2fc8e9a | 2016-03-25 11:49:07 -0700 | [diff] [blame] | 419 | } |
Chinmay Garde | 9f4fdcb | 2016-03-02 11:40:10 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Jonah Williams | 6f71ce2 | 2019-08-27 14:20:51 -0700 | [diff] [blame] | 422 | Future<void> _setupUpdatedApplicationBundle(covariant BuildableIOSApp app, BuildInfo buildInfo, String mainPath) async { |
Jonah Williams | 79323f7 | 2019-10-30 19:40:19 -0700 | [diff] [blame] | 423 | await sideloadUpdatedAssetsForInstalledApplicationBundle(buildInfo, mainPath); |
Yegor Jbanov | 58b2c63 | 2016-03-03 15:58:34 -0800 | [diff] [blame] | 424 | |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 425 | // Step 1: Build the Xcode project. |
Chinmay Garde | 66fee3a | 2016-05-23 12:58:42 -0700 | [diff] [blame] | 426 | // The build mode for the simulator is always debug. |
Alexander Aprelev | 9917657 | 2018-01-11 13:24:51 -0800 | [diff] [blame] | 427 | |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 428 | final BuildInfo debugBuildInfo = BuildInfo(BuildMode.debug, buildInfo.flavor, |
Jacob Richman | 62febaa | 2018-05-30 14:53:35 -0700 | [diff] [blame] | 429 | trackWidgetCreation: buildInfo.trackWidgetCreation, |
Alexander Aprelev | 9917657 | 2018-01-11 13:24:51 -0800 | [diff] [blame] | 430 | extraFrontEndOptions: buildInfo.extraFrontEndOptions, |
Dan Field | 4b8efad | 2020-02-04 20:34:24 -0800 | [diff] [blame] | 431 | extraGenSnapshotOptions: buildInfo.extraGenSnapshotOptions, |
| 432 | treeShakeIcons: buildInfo.treeShakeIcons); |
Alexander Aprelev | 9917657 | 2018-01-11 13:24:51 -0800 | [diff] [blame] | 433 | |
Simon Lightfoot | be6501a | 2018-05-21 11:54:38 +0100 | [diff] [blame] | 434 | final XcodeBuildResult buildResult = await buildXcodeProject( |
| 435 | app: app, |
| 436 | buildInfo: debugBuildInfo, |
| 437 | targetOverride: mainPath, |
| 438 | buildForDevice: false, |
Simon Lightfoot | be6501a | 2018-05-21 11:54:38 +0100 | [diff] [blame] | 439 | ); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 440 | if (!buildResult.success) { |
Dan Rubel | e384c0d | 2016-11-13 22:09:03 -0500 | [diff] [blame] | 441 | throwToolExit('Could not build the application for the simulator.'); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 442 | } |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 443 | |
| 444 | // Step 2: Assert that the Xcode project was successfully built. |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 445 | final Directory bundle = globals.fs.directory(app.simulatorBundlePath); |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 446 | final bool bundleExists = bundle.existsSync(); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 447 | if (!bundleExists) { |
Dan Rubel | e384c0d | 2016-11-13 22:09:03 -0500 | [diff] [blame] | 448 | throwToolExit('Could not find the built application bundle at ${bundle.path}.'); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 449 | } |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 450 | |
| 451 | // Step 3: Install the updated bundle to the simulator. |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 452 | await SimControl.instance.install(id, globals.fs.path.absolute(bundle.path)); |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 453 | } |
| 454 | |
Jonah Williams | 79323f7 | 2019-10-30 19:40:19 -0700 | [diff] [blame] | 455 | @visibleForTesting |
| 456 | Future<void> sideloadUpdatedAssetsForInstalledApplicationBundle(BuildInfo buildInfo, String mainPath) { |
Chris Bracken | ed0b8be | 2018-09-04 10:05:26 -0700 | [diff] [blame] | 457 | // Run compiler to produce kernel file for the application. |
Emmanuel Garcia | d9c1962 | 2019-06-11 14:46:00 -0700 | [diff] [blame] | 458 | return BundleBuilder().build( |
Jonah Williams | 79323f7 | 2019-10-30 19:40:19 -0700 | [diff] [blame] | 459 | platform: TargetPlatform.ios, |
Simon Lightfoot | be6501a | 2018-05-21 11:54:38 +0100 | [diff] [blame] | 460 | mainPath: mainPath, |
Chris Bracken | ed0b8be | 2018-09-04 10:05:26 -0700 | [diff] [blame] | 461 | precompiledSnapshot: false, |
Ryan Macnak | b7773da | 2019-10-14 15:26:14 -0700 | [diff] [blame] | 462 | buildMode: buildInfo.mode, |
Jacob Richman | 2c05eca | 2018-02-12 10:44:31 -0800 | [diff] [blame] | 463 | trackWidgetCreation: buildInfo.trackWidgetCreation, |
Dan Field | 4b8efad | 2020-02-04 20:34:24 -0800 | [diff] [blame] | 464 | treeShakeIcons: false, |
Jacob Richman | 2c05eca | 2018-02-12 10:44:31 -0800 | [diff] [blame] | 465 | ); |
| 466 | } |
Chinmay Garde | 9f4fdcb | 2016-03-02 11:40:10 -0800 | [diff] [blame] | 467 | |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 468 | @override |
| 469 | Future<bool> stopApp(ApplicationPackage app) async { |
| 470 | // Currently we don't have a way to stop an app running on iOS. |
| 471 | return false; |
| 472 | } |
| 473 | |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 474 | String get logFilePath { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 475 | return globals.platform.environment.containsKey('IOS_SIMULATOR_LOG_FILE_PATH') |
Zachary Anderson | 390ed1c | 2020-02-03 14:33:03 -0800 | [diff] [blame] | 476 | ? globals.platform.environment['IOS_SIMULATOR_LOG_FILE_PATH'].replaceAll('%{id}', id) |
| 477 | : globals.fs.path.join( |
| 478 | globals.fsUtils.homeDirPath, |
| 479 | 'Library', |
| 480 | 'Logs', |
| 481 | 'CoreSimulator', |
| 482 | id, |
| 483 | 'system.log', |
| 484 | ); |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | @override |
Todd Volkert | 60c5ffc | 2017-04-25 17:23:00 -0700 | [diff] [blame] | 488 | Future<TargetPlatform> get targetPlatform async => TargetPlatform.ios; |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 489 | |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 490 | @override |
Jonah Williams | 5c52498 | 2019-06-18 15:23:14 -0700 | [diff] [blame] | 491 | Future<String> get sdkNameAndVersion async => simulatorCategory; |
Dan Rubel | 0b49d81 | 2016-10-27 09:07:21 +0100 | [diff] [blame] | 492 | |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 493 | final RegExp _iosSdkRegExp = RegExp(r'iOS( |-)(\d+)'); |
Todd Volkert | 137f075 | 2017-11-29 08:22:05 -0800 | [diff] [blame] | 494 | |
| 495 | Future<int> get sdkMajorVersion async { |
| 496 | final Match sdkMatch = _iosSdkRegExp.firstMatch(await sdkNameAndVersion); |
Jonah Williams | ca5411e | 2019-08-12 15:21:28 -0700 | [diff] [blame] | 497 | return int.parse(sdkMatch?.group(2) ?? '11'); |
Todd Volkert | 137f075 | 2017-11-29 08:22:05 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Dan Rubel | 0b49d81 | 2016-10-27 09:07:21 +0100 | [diff] [blame] | 500 | @override |
Jonah Williams | ca5411e | 2019-08-12 15:21:28 -0700 | [diff] [blame] | 501 | DeviceLogReader getLogReader({ covariant IOSApp app }) { |
Todd Volkert | ce59412 | 2017-09-28 13:11:16 -0700 | [diff] [blame] | 502 | assert(app is IOSApp); |
Todd Volkert | 9cb914d | 2016-11-16 17:19:00 -0800 | [diff] [blame] | 503 | _logReaders ??= <ApplicationPackage, _IOSSimulatorLogReader>{}; |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 504 | return _logReaders.putIfAbsent(app, () => _IOSSimulatorLogReader(this, app)); |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 505 | } |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 506 | |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 507 | @override |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 508 | DevicePortForwarder get portForwarder => _portForwarder ??= _IOSSimulatorDevicePortForwarder(this); |
John McCutchan | 5e140b7 | 2016-03-10 15:48:37 -0800 | [diff] [blame] | 509 | |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 510 | @override |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 511 | void clearLogs() { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 512 | final File logFile = globals.fs.file(logFilePath); |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 513 | if (logFile.existsSync()) { |
Leaf Petersen | 32f9444 | 2018-07-20 15:07:24 -0700 | [diff] [blame] | 514 | final RandomAccessFile randomFile = logFile.openSync(mode: FileMode.write); |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 515 | randomFile.truncateSync(0); |
| 516 | randomFile.closeSync(); |
| 517 | } |
| 518 | } |
| 519 | |
Alexandre Ardhuin | 2d3ff10 | 2018-10-05 07:54:56 +0200 | [diff] [blame] | 520 | Future<void> ensureLogsExists() async { |
Todd Volkert | 137f075 | 2017-11-29 08:22:05 -0800 | [diff] [blame] | 521 | if (await sdkMajorVersion < 11) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 522 | final File logFile = globals.fs.file(logFilePath); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 523 | if (!logFile.existsSync()) { |
Todd Volkert | 137f075 | 2017-11-29 08:22:05 -0800 | [diff] [blame] | 524 | logFile.writeAsBytesSync(<int>[]); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 525 | } |
Todd Volkert | 137f075 | 2017-11-29 08:22:05 -0800 | [diff] [blame] | 526 | } |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 527 | } |
Devon Carew | 15b9e1d | 2016-03-25 16:04:22 -0700 | [diff] [blame] | 528 | |
xster | 3985ddb | 2017-02-16 12:50:27 -0800 | [diff] [blame] | 529 | bool get _xcodeVersionSupportsScreenshot { |
Jenn Magder | 91f7902 | 2020-01-25 11:18:02 -0800 | [diff] [blame] | 530 | return globals.xcode.majorVersion > 8 || (globals.xcode.majorVersion == 8 && globals.xcode.minorVersion >= 2); |
xster | 3985ddb | 2017-02-16 12:50:27 -0800 | [diff] [blame] | 531 | } |
Devon Carew | 15b9e1d | 2016-03-25 16:04:22 -0700 | [diff] [blame] | 532 | |
| 533 | @override |
xster | 3985ddb | 2017-02-16 12:50:27 -0800 | [diff] [blame] | 534 | bool get supportsScreenshot => _xcodeVersionSupportsScreenshot; |
Devon Carew | 15b9e1d | 2016-03-25 16:04:22 -0700 | [diff] [blame] | 535 | |
xster | 3985ddb | 2017-02-16 12:50:27 -0800 | [diff] [blame] | 536 | @override |
Keerti Parthasarathy | aecb7d9 | 2018-06-12 09:30:10 -0700 | [diff] [blame] | 537 | Future<void> takeScreenshot(File outputFile) { |
xster | 432ffde | 2017-06-15 18:25:09 -0700 | [diff] [blame] | 538 | return SimControl.instance.takeScreenshot(id, outputFile.path); |
Devon Carew | 15b9e1d | 2016-03-25 16:04:22 -0700 | [diff] [blame] | 539 | } |
Jonah Williams | 6b19184 | 2019-04-25 12:25:12 -0700 | [diff] [blame] | 540 | |
| 541 | @override |
| 542 | bool isSupportedForProject(FlutterProject flutterProject) { |
| 543 | return flutterProject.ios.existsSync(); |
| 544 | } |
Zachary Anderson | 99684ce | 2019-12-05 08:48:00 -0800 | [diff] [blame] | 545 | |
| 546 | @override |
| 547 | Future<void> dispose() async { |
| 548 | _logReaders?.forEach( |
| 549 | (ApplicationPackage application, _IOSSimulatorLogReader logReader) { |
| 550 | logReader.dispose(); |
| 551 | }, |
| 552 | ); |
| 553 | await _portForwarder?.dispose(); |
| 554 | } |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 555 | } |
| 556 | |
Chris Bracken | dd7e313 | 2017-09-14 12:28:21 -0700 | [diff] [blame] | 557 | /// Launches the device log reader process on the host. |
| 558 | Future<Process> launchDeviceLogTool(IOSSimulator device) async { |
Chris Bracken | dd7e313 | 2017-09-14 12:28:21 -0700 | [diff] [blame] | 559 | // Versions of iOS prior to iOS 11 log to the simulator syslog file. |
Zachary Anderson | 73c10e8 | 2019-09-11 18:20:42 -0700 | [diff] [blame] | 560 | if (await device.sdkMajorVersion < 11) { |
| 561 | return processUtils.start(<String>['tail', '-n', '0', '-F', device.logFilePath]); |
| 562 | } |
Chris Bracken | dd7e313 | 2017-09-14 12:28:21 -0700 | [diff] [blame] | 563 | |
| 564 | // For iOS 11 and above, use /usr/bin/log to tail process logs. |
| 565 | // Run in interactive mode (via script), otherwise /usr/bin/log buffers in 4k chunks. (radar: 34420207) |
Zachary Anderson | 73c10e8 | 2019-09-11 18:20:42 -0700 | [diff] [blame] | 566 | return processUtils.start(<String>[ |
Chris Bracken | dd7e313 | 2017-09-14 12:28:21 -0700 | [diff] [blame] | 567 | 'script', '/dev/null', '/usr/bin/log', 'stream', '--style', 'syslog', '--predicate', 'processImagePath CONTAINS "${device.id}"', |
| 568 | ]); |
| 569 | } |
| 570 | |
| 571 | Future<Process> launchSystemLogTool(IOSSimulator device) async { |
Chris Bracken | dd7e313 | 2017-09-14 12:28:21 -0700 | [diff] [blame] | 572 | // Versions of iOS prior to 11 tail the simulator syslog file. |
Zachary Anderson | 73c10e8 | 2019-09-11 18:20:42 -0700 | [diff] [blame] | 573 | if (await device.sdkMajorVersion < 11) { |
| 574 | return processUtils.start(<String>['tail', '-n', '0', '-F', '/private/var/log/system.log']); |
| 575 | } |
Chris Bracken | dd7e313 | 2017-09-14 12:28:21 -0700 | [diff] [blame] | 576 | |
| 577 | // For iOS 11 and later, all relevant detail is in the device log. |
| 578 | return null; |
| 579 | } |
| 580 | |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 581 | class _IOSSimulatorLogReader extends DeviceLogReader { |
Todd Volkert | ce59412 | 2017-09-28 13:11:16 -0700 | [diff] [blame] | 582 | _IOSSimulatorLogReader(this.device, IOSApp app) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 583 | _linesController = StreamController<String>.broadcast( |
Alexandre Ardhuin | 2166ea5 | 2017-03-15 23:09:58 +0100 | [diff] [blame] | 584 | onListen: _start, |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 585 | onCancel: _stop, |
Devon Carew | b0dca79 | 2016-04-27 14:43:42 -0700 | [diff] [blame] | 586 | ); |
Todd Volkert | 9cb914d | 2016-11-16 17:19:00 -0800 | [diff] [blame] | 587 | _appName = app == null ? null : app.name.replaceAll('.app', ''); |
Devon Carew | b0dca79 | 2016-04-27 14:43:42 -0700 | [diff] [blame] | 588 | } |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 589 | |
| 590 | final IOSSimulator device; |
| 591 | |
Alexandre Ardhuin | 2ea1d81 | 2018-10-04 07:28:07 +0200 | [diff] [blame] | 592 | String _appName; |
| 593 | |
Devon Carew | b0dca79 | 2016-04-27 14:43:42 -0700 | [diff] [blame] | 594 | StreamController<String> _linesController; |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 595 | |
Devon Carew | b0dca79 | 2016-04-27 14:43:42 -0700 | [diff] [blame] | 596 | // We log from two files: the device and the system log. |
Todd Volkert | 016b5ab | 2017-01-09 08:37:00 -0800 | [diff] [blame] | 597 | Process _deviceProcess; |
| 598 | Process _systemProcess; |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 599 | |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 600 | @override |
Devon Carew | b0dca79 | 2016-04-27 14:43:42 -0700 | [diff] [blame] | 601 | Stream<String> get logLines => _linesController.stream; |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 602 | |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 603 | @override |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 604 | String get name => device.name; |
| 605 | |
Alexandre Ardhuin | 2d3ff10 | 2018-10-05 07:54:56 +0200 | [diff] [blame] | 606 | Future<void> _start() async { |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 607 | // Device log. |
Todd Volkert | 137f075 | 2017-11-29 08:22:05 -0800 | [diff] [blame] | 608 | await device.ensureLogsExists(); |
Chris Bracken | dd7e313 | 2017-09-14 12:28:21 -0700 | [diff] [blame] | 609 | _deviceProcess = await launchDeviceLogTool(device); |
Alexandre Ardhuin | f62afdc | 2018-10-01 21:29:08 +0200 | [diff] [blame] | 610 | _deviceProcess.stdout.transform<String>(utf8.decoder).transform<String>(const LineSplitter()).listen(_onDeviceLine); |
| 611 | _deviceProcess.stderr.transform<String>(utf8.decoder).transform<String>(const LineSplitter()).listen(_onDeviceLine); |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 612 | |
| 613 | // Track system.log crashes. |
| 614 | // ReportCrash[37965]: Saved crash report for FlutterRunner[37941]... |
Chris Bracken | dd7e313 | 2017-09-14 12:28:21 -0700 | [diff] [blame] | 615 | _systemProcess = await launchSystemLogTool(device); |
| 616 | if (_systemProcess != null) { |
Alexandre Ardhuin | f62afdc | 2018-10-01 21:29:08 +0200 | [diff] [blame] | 617 | _systemProcess.stdout.transform<String>(utf8.decoder).transform<String>(const LineSplitter()).listen(_onSystemLine); |
| 618 | _systemProcess.stderr.transform<String>(utf8.decoder).transform<String>(const LineSplitter()).listen(_onSystemLine); |
Chris Bracken | dd7e313 | 2017-09-14 12:28:21 -0700 | [diff] [blame] | 619 | } |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 620 | |
xster | e28765a | 2017-09-20 17:24:43 -0700 | [diff] [blame] | 621 | // We don't want to wait for the process or its callback. Best effort |
| 622 | // cleanup in the callback. |
xster | 36c5e32 | 2019-02-05 15:00:51 -0800 | [diff] [blame] | 623 | unawaited(_deviceProcess.exitCode.whenComplete(() { |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 624 | if (_linesController.hasListener) { |
Devon Carew | b0dca79 | 2016-04-27 14:43:42 -0700 | [diff] [blame] | 625 | _linesController.close(); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 626 | } |
xster | 36c5e32 | 2019-02-05 15:00:51 -0800 | [diff] [blame] | 627 | })); |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | // Match the log prefix (in order to shorten it): |
Jenn Magder | 85e9be3 | 2019-12-20 17:45:16 -0800 | [diff] [blame] | 631 | // * Xcode 8: Sep 13 15:28:51 cbracken-macpro localhost Runner[37195]: (Flutter) Observatory listening on http://127.0.0.1:57701/ |
| 632 | // * Xcode 9: 2017-09-13 15:26:57.228948-0700 localhost Runner[37195]: (Flutter) Observatory listening on http://127.0.0.1:57701/ |
| 633 | static final RegExp _mapRegex = RegExp(r'\S+ +\S+ +\S+ +(\S+ +)?(\S+)\[\d+\]\)?: (\(.*?\))? *(.*)$'); |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 634 | |
| 635 | // Jan 31 19:23:28 --- last message repeated 1 time --- |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 636 | static final RegExp _lastMessageSingleRegex = RegExp(r'\S+ +\S+ +\S+ --- last message repeated 1 time ---$'); |
| 637 | static final RegExp _lastMessageMultipleRegex = RegExp(r'\S+ +\S+ +\S+ --- last message repeated (\d+) times ---$'); |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 638 | |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 639 | static final RegExp _flutterRunnerRegex = RegExp(r' FlutterRunner\[\d+\] '); |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 640 | |
| 641 | String _filterDeviceLine(String string) { |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 642 | final Match match = _mapRegex.matchAsPrefix(string); |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 643 | if (match != null) { |
Jenn Magder | 85e9be3 | 2019-12-20 17:45:16 -0800 | [diff] [blame] | 644 | final String category = match.group(2); |
| 645 | final String tag = match.group(3); |
| 646 | final String content = match.group(4); |
Chris Bracken | dd7e313 | 2017-09-14 12:28:21 -0700 | [diff] [blame] | 647 | |
| 648 | // Filter out non-Flutter originated noise from the engine. |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 649 | if (_appName != null && category != _appName) { |
Chris Bracken | dd7e313 | 2017-09-14 12:28:21 -0700 | [diff] [blame] | 650 | return null; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 651 | } |
Chris Bracken | dd7e313 | 2017-09-14 12:28:21 -0700 | [diff] [blame] | 652 | |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 653 | if (tag != null && tag != '(Flutter)') { |
Chris Bracken | dd7e313 | 2017-09-14 12:28:21 -0700 | [diff] [blame] | 654 | return null; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 655 | } |
Todd Volkert | ec7c49e | 2016-11-22 10:47:24 -0800 | [diff] [blame] | 656 | |
| 657 | // Filter out some messages that clearly aren't related to Flutter. |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 658 | if (string.contains(': could not find icon for representation -> com.apple.')) { |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 659 | return null; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 660 | } |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 661 | |
Devon Carew | fa04321 | 2017-02-22 20:55:08 -0800 | [diff] [blame] | 662 | // assertion failed: 15G1212 13E230: libxpc.dylib + 57882 [66C28065-C9DB-3C8E-926F-5A40210A6D1B]: 0x7d |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 663 | if (content.startsWith('assertion failed: ') && content.contains(' libxpc.dylib ')) { |
Devon Carew | fa04321 | 2017-02-22 20:55:08 -0800 | [diff] [blame] | 664 | return null; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 665 | } |
Devon Carew | fa04321 | 2017-02-22 20:55:08 -0800 | [diff] [blame] | 666 | |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 667 | if (_appName == null) { |
Todd Volkert | ec7c49e | 2016-11-22 10:47:24 -0800 | [diff] [blame] | 668 | return '$category: $content'; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 669 | } else if (category == _appName) { |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 670 | return content; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 671 | } |
Todd Volkert | ec7c49e | 2016-11-22 10:47:24 -0800 | [diff] [blame] | 672 | |
Devon Carew | 7ae584b | 2016-06-03 12:54:27 -0700 | [diff] [blame] | 673 | return null; |
Todd Volkert | ec7c49e | 2016-11-22 10:47:24 -0800 | [diff] [blame] | 674 | } |
| 675 | |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 676 | if (string.startsWith('Filtering the log data using ')) { |
Chris Bracken | dd7e313 | 2017-09-14 12:28:21 -0700 | [diff] [blame] | 677 | return null; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 678 | } |
Chris Bracken | dd7e313 | 2017-09-14 12:28:21 -0700 | [diff] [blame] | 679 | |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 680 | if (string.startsWith('Timestamp (process)[PID]')) { |
Chris Bracken | dd7e313 | 2017-09-14 12:28:21 -0700 | [diff] [blame] | 681 | return null; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 682 | } |
Chris Bracken | dd7e313 | 2017-09-14 12:28:21 -0700 | [diff] [blame] | 683 | |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 684 | if (_lastMessageSingleRegex.matchAsPrefix(string) != null) { |
Todd Volkert | ec7c49e | 2016-11-22 10:47:24 -0800 | [diff] [blame] | 685 | return null; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 686 | } |
Todd Volkert | ec7c49e | 2016-11-22 10:47:24 -0800 | [diff] [blame] | 687 | |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 688 | if (RegExp(r'assertion failed: .* libxpc.dylib .* 0x7d$').matchAsPrefix(string) != null) { |
Todd Volkert | ec7c49e | 2016-11-22 10:47:24 -0800 | [diff] [blame] | 689 | return null; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 690 | } |
Todd Volkert | ec7c49e | 2016-11-22 10:47:24 -0800 | [diff] [blame] | 691 | |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 692 | return string; |
| 693 | } |
| 694 | |
Devon Carew | 7ae584b | 2016-06-03 12:54:27 -0700 | [diff] [blame] | 695 | String _lastLine; |
| 696 | |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 697 | void _onDeviceLine(String line) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 698 | globals.printTrace('[DEVICE LOG] $line'); |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 699 | final Match multi = _lastMessageMultipleRegex.matchAsPrefix(line); |
Devon Carew | 7ae584b | 2016-06-03 12:54:27 -0700 | [diff] [blame] | 700 | |
| 701 | if (multi != null) { |
| 702 | if (_lastLine != null) { |
| 703 | int repeat = int.parse(multi.group(1)); |
| 704 | repeat = math.max(0, math.min(100, repeat)); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 705 | for (int i = 1; i < repeat; i++) { |
Devon Carew | 7ae584b | 2016-06-03 12:54:27 -0700 | [diff] [blame] | 706 | _linesController.add(_lastLine); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 707 | } |
Devon Carew | 7ae584b | 2016-06-03 12:54:27 -0700 | [diff] [blame] | 708 | } |
| 709 | } else { |
| 710 | _lastLine = _filterDeviceLine(line); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 711 | if (_lastLine != null) { |
Devon Carew | 7ae584b | 2016-06-03 12:54:27 -0700 | [diff] [blame] | 712 | _linesController.add(_lastLine); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 713 | } |
Devon Carew | 7ae584b | 2016-06-03 12:54:27 -0700 | [diff] [blame] | 714 | } |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | String _filterSystemLog(String string) { |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 718 | final Match match = _mapRegex.matchAsPrefix(string); |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 719 | return match == null ? string : '${match.group(1)}: ${match.group(2)}'; |
| 720 | } |
| 721 | |
| 722 | void _onSystemLine(String line) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 723 | globals.printTrace('[SYS LOG] $line'); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 724 | if (!_flutterRunnerRegex.hasMatch(line)) { |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 725 | return; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 726 | } |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 727 | |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 728 | final String filteredLine = _filterSystemLog(line); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 729 | if (filteredLine == null) { |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 730 | return; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 731 | } |
John McCutchan | 8803cec | 2016-03-07 13:52:27 -0800 | [diff] [blame] | 732 | |
Devon Carew | b0dca79 | 2016-04-27 14:43:42 -0700 | [diff] [blame] | 733 | _linesController.add(filteredLine); |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 734 | } |
| 735 | |
Devon Carew | b0dca79 | 2016-04-27 14:43:42 -0700 | [diff] [blame] | 736 | void _stop() { |
| 737 | _deviceProcess?.kill(); |
| 738 | _systemProcess?.kill(); |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 739 | } |
Zachary Anderson | 99684ce | 2019-12-05 08:48:00 -0800 | [diff] [blame] | 740 | |
| 741 | @override |
| 742 | void dispose() { |
| 743 | _stop(); |
| 744 | } |
Devon Carew | 67046f9 | 2016-02-20 22:00:11 -0800 | [diff] [blame] | 745 | } |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 746 | |
| 747 | int compareIosVersions(String v1, String v2) { |
Alexandre Ardhuin | f62afdc | 2018-10-01 21:29:08 +0200 | [diff] [blame] | 748 | final List<int> v1Fragments = v1.split('.').map<int>(int.parse).toList(); |
| 749 | final List<int> v2Fragments = v2.split('.').map<int>(int.parse).toList(); |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 750 | |
| 751 | int i = 0; |
Ian Hickson | efb45ea | 2017-09-27 16:13:48 -0700 | [diff] [blame] | 752 | while (i < v1Fragments.length && i < v2Fragments.length) { |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 753 | final int v1Fragment = v1Fragments[i]; |
| 754 | final int v2Fragment = v2Fragments[i]; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 755 | if (v1Fragment != v2Fragment) { |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 756 | return v1Fragment.compareTo(v2Fragment); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 757 | } |
Ian Hickson | efb45ea | 2017-09-27 16:13:48 -0700 | [diff] [blame] | 758 | i += 1; |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 759 | } |
| 760 | return v1Fragments.length.compareTo(v2Fragments.length); |
| 761 | } |
| 762 | |
| 763 | /// Matches on device type given an identifier. |
| 764 | /// |
| 765 | /// Example device type identifiers: |
| 766 | /// ✓ com.apple.CoreSimulator.SimDeviceType.iPhone-5 |
| 767 | /// ✓ com.apple.CoreSimulator.SimDeviceType.iPhone-6 |
| 768 | /// ✓ com.apple.CoreSimulator.SimDeviceType.iPhone-6s-Plus |
| 769 | /// ✗ com.apple.CoreSimulator.SimDeviceType.iPad-2 |
| 770 | /// ✗ com.apple.CoreSimulator.SimDeviceType.Apple-Watch-38mm |
| 771 | final RegExp _iosDeviceTypePattern = |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 772 | RegExp(r'com.apple.CoreSimulator.SimDeviceType.iPhone-(\d+)(.*)'); |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 773 | |
| 774 | int compareIphoneVersions(String id1, String id2) { |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 775 | final Match m1 = _iosDeviceTypePattern.firstMatch(id1); |
| 776 | final Match m2 = _iosDeviceTypePattern.firstMatch(id2); |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 777 | |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 778 | final int v1 = int.parse(m1[1]); |
| 779 | final int v2 = int.parse(m2[1]); |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 780 | |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 781 | if (v1 != v2) { |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 782 | return v1.compareTo(v2); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 783 | } |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 784 | |
| 785 | // Sorted in the least preferred first order. |
Alexandre Ardhuin | eda03e2 | 2018-08-02 12:02:32 +0200 | [diff] [blame] | 786 | const List<String> qualifiers = <String>['-Plus', '', 's-Plus', 's']; |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 787 | |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 788 | final int q1 = qualifiers.indexOf(m1[2]); |
| 789 | final int q2 = qualifiers.indexOf(m2[2]); |
Yegor Jbanov | 9383466 | 2016-03-10 11:04:02 -0800 | [diff] [blame] | 790 | return q1.compareTo(q2); |
| 791 | } |
John McCutchan | 5e140b7 | 2016-03-10 15:48:37 -0800 | [diff] [blame] | 792 | |
| 793 | class _IOSSimulatorDevicePortForwarder extends DevicePortForwarder { |
| 794 | _IOSSimulatorDevicePortForwarder(this.device); |
| 795 | |
| 796 | final IOSSimulator device; |
| 797 | |
| 798 | final List<ForwardedPort> _ports = <ForwardedPort>[]; |
| 799 | |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 800 | @override |
Zachary Anderson | 99684ce | 2019-12-05 08:48:00 -0800 | [diff] [blame] | 801 | List<ForwardedPort> get forwardedPorts => _ports; |
John McCutchan | 5e140b7 | 2016-03-10 15:48:37 -0800 | [diff] [blame] | 802 | |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 803 | @override |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 804 | Future<int> forward(int devicePort, { int hostPort }) async { |
Ian Hickson | 35ad2a7 | 2018-06-27 16:44:28 -0700 | [diff] [blame] | 805 | if (hostPort == null || hostPort == 0) { |
John McCutchan | 5e140b7 | 2016-03-10 15:48:37 -0800 | [diff] [blame] | 806 | hostPort = devicePort; |
| 807 | } |
| 808 | assert(devicePort == hostPort); |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 809 | _ports.add(ForwardedPort(devicePort, hostPort)); |
John McCutchan | 5e140b7 | 2016-03-10 15:48:37 -0800 | [diff] [blame] | 810 | return hostPort; |
| 811 | } |
| 812 | |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 813 | @override |
Alexandre Ardhuin | 2d3ff10 | 2018-10-05 07:54:56 +0200 | [diff] [blame] | 814 | Future<void> unforward(ForwardedPort forwardedPort) async { |
John McCutchan | 5e140b7 | 2016-03-10 15:48:37 -0800 | [diff] [blame] | 815 | _ports.remove(forwardedPort); |
| 816 | } |
Zachary Anderson | 99684ce | 2019-12-05 08:48:00 -0800 | [diff] [blame] | 817 | |
| 818 | @override |
| 819 | Future<void> dispose() async { |
Christopher Fujino | 0c2f51f | 2020-01-23 18:23:03 -0800 | [diff] [blame] | 820 | final List<ForwardedPort> portsCopy = List<ForwardedPort>.from(_ports); |
| 821 | for (final ForwardedPort port in portsCopy) { |
Zachary Anderson | 99684ce | 2019-12-05 08:48:00 -0800 | [diff] [blame] | 822 | await unforward(port); |
| 823 | } |
| 824 | } |
John McCutchan | 5e140b7 | 2016-03-10 15:48:37 -0800 | [diff] [blame] | 825 | } |