From ddc1c296a1782b966313d6d50adb34a971965164 Mon Sep 17 00:00:00 2001 From: Jenn Magder Date: Tue, 13 Apr 2021 18:10:21 -0700 Subject: [PATCH] Pull XCDevice out of xcode.dart (#80324) --- .../flutter_tools/lib/src/context_runner.dart | 1 + .../lib/src/flutter_device_manager.dart | 2 +- packages/flutter_tools/lib/src/globals.dart | 1 + .../flutter_tools/lib/src/ios/devices.dart | 2 +- .../flutter_tools/lib/src/macos/xcdevice.dart | 503 ++++++++++++++++++ .../flutter_tools/lib/src/macos/xcode.dart | 485 ----------------- .../test/general.shard/ios/devices_test.dart | 2 +- .../test/general.shard/macos/xcode_test.dart | 1 + 8 files changed, 509 insertions(+), 488 deletions(-) create mode 100644 packages/flutter_tools/lib/src/macos/xcdevice.dart diff --git a/packages/flutter_tools/lib/src/context_runner.dart b/packages/flutter_tools/lib/src/context_runner.dart index 378c59768d..ed9e6e0346 100644 --- a/packages/flutter_tools/lib/src/context_runner.dart +++ b/packages/flutter_tools/lib/src/context_runner.dart @@ -53,6 +53,7 @@ import 'ios/xcodeproj.dart'; import 'macos/cocoapods.dart'; import 'macos/cocoapods_validator.dart'; import 'macos/macos_workflow.dart'; +import 'macos/xcdevice.dart'; import 'macos/xcode.dart'; import 'mdns_discovery.dart'; import 'persistent_tool_state.dart'; diff --git a/packages/flutter_tools/lib/src/flutter_device_manager.dart b/packages/flutter_tools/lib/src/flutter_device_manager.dart index 24f5e202ce..08b005be1b 100644 --- a/packages/flutter_tools/lib/src/flutter_device_manager.dart +++ b/packages/flutter_tools/lib/src/flutter_device_manager.dart @@ -31,7 +31,7 @@ import 'ios/simulators.dart'; import 'linux/linux_device.dart'; import 'macos/macos_device.dart'; import 'macos/macos_workflow.dart'; -import 'macos/xcode.dart'; +import 'macos/xcdevice.dart'; import 'tester/flutter_tester.dart'; import 'version.dart'; import 'web/web_device.dart'; diff --git a/packages/flutter_tools/lib/src/globals.dart b/packages/flutter_tools/lib/src/globals.dart index 84fb2116d8..ec4d8a2a76 100644 --- a/packages/flutter_tools/lib/src/globals.dart +++ b/packages/flutter_tools/lib/src/globals.dart @@ -17,6 +17,7 @@ import 'ios/simulators.dart'; import 'ios/xcodeproj.dart'; import 'macos/cocoapods.dart'; import 'macos/cocoapods_validator.dart'; +import 'macos/xcdevice.dart'; import 'macos/xcode.dart'; import 'project.dart'; import 'reporting/crash_reporting.dart'; diff --git a/packages/flutter_tools/lib/src/ios/devices.dart b/packages/flutter_tools/lib/src/ios/devices.dart index e54d8a0110..442b06f21d 100644 --- a/packages/flutter_tools/lib/src/ios/devices.dart +++ b/packages/flutter_tools/lib/src/ios/devices.dart @@ -22,7 +22,7 @@ import '../convert.dart'; import '../device.dart'; import '../device_port_forwarder.dart'; import '../globals.dart' as globals; -import '../macos/xcode.dart'; +import '../macos/xcdevice.dart'; import '../project.dart'; import '../protocol_discovery.dart'; import '../vmservice.dart'; diff --git a/packages/flutter_tools/lib/src/macos/xcdevice.dart b/packages/flutter_tools/lib/src/macos/xcdevice.dart new file mode 100644 index 0000000000..a734437908 --- /dev/null +++ b/packages/flutter_tools/lib/src/macos/xcdevice.dart @@ -0,0 +1,503 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// @dart = 2.8 + +import 'dart:async'; + +import 'package:meta/meta.dart'; +import 'package:process/process.dart'; + +import '../artifacts.dart'; +import '../base/common.dart'; +import '../base/io.dart'; +import '../base/logger.dart'; +import '../base/platform.dart'; +import '../base/process.dart'; +import '../build_info.dart'; +import '../cache.dart'; +import '../convert.dart'; +import '../globals.dart' as globals; +import '../ios/devices.dart'; +import '../ios/ios_deploy.dart'; +import '../ios/iproxy.dart'; +import '../ios/mac.dart'; +import '../reporting/reporting.dart'; +import 'xcode.dart'; + +enum XCDeviceEvent { + attach, + detach, +} + +/// A utility class for interacting with Xcode xcdevice command line tools. +class XCDevice { + XCDevice({ + @required Artifacts artifacts, + @required Cache cache, + @required ProcessManager processManager, + @required Logger logger, + @required Xcode xcode, + @required Platform platform, + @required IProxy iproxy, + }) : _processUtils = ProcessUtils(logger: logger, processManager: processManager), + _logger = logger, + _iMobileDevice = IMobileDevice( + artifacts: artifacts, + cache: cache, + logger: logger, + processManager: processManager, + ), + _iosDeploy = IOSDeploy( + artifacts: artifacts, + cache: cache, + logger: logger, + platform: platform, + processManager: processManager, + ), + _iProxy = iproxy, + _xcode = xcode { + + _setupDeviceIdentifierByEventStream(); + } + + void dispose() { + _deviceObservationProcess?.kill(); + } + + final ProcessUtils _processUtils; + final Logger _logger; + final IMobileDevice _iMobileDevice; + final IOSDeploy _iosDeploy; + final Xcode _xcode; + final IProxy _iProxy; + + List _cachedListResults; + Process _deviceObservationProcess; + StreamController> _deviceIdentifierByEvent; + + void _setupDeviceIdentifierByEventStream() { + // _deviceIdentifierByEvent Should always be available for listeners + // in case polling needs to be stopped and restarted. + _deviceIdentifierByEvent = StreamController>.broadcast( + onListen: _startObservingTetheredIOSDevices, + onCancel: _stopObservingTetheredIOSDevices, + ); + } + + bool get isInstalled => _xcode.isInstalledAndMeetsVersionCheck; + + Future> _getAllDevices({ + bool useCache = false, + @required Duration timeout + }) async { + if (!isInstalled) { + _logger.printTrace("Xcode not found. Run 'flutter doctor' for more information."); + return null; + } + if (useCache && _cachedListResults != null) { + return _cachedListResults; + } + try { + // USB-tethered devices should be found quickly. 1 second timeout is faster than the default. + final RunResult result = await _processUtils.run( + [ + ..._xcode.xcrunCommand(), + 'xcdevice', + 'list', + '--timeout', + timeout.inSeconds.toString(), + ], + throwOnError: true, + ); + if (result.exitCode == 0) { + final List listResults = json.decode(result.stdout) as List; + _cachedListResults = listResults; + return listResults; + } + _logger.printTrace('xcdevice returned an error:\n${result.stderr}'); + } on ProcessException catch (exception) { + _logger.printTrace('Process exception running xcdevice list:\n$exception'); + } on ArgumentError catch (exception) { + _logger.printTrace('Argument exception running xcdevice list:\n$exception'); + } + + return null; + } + + /// Observe identifiers (UDIDs) of devices as they attach and detach. + /// + /// Each attach and detach event is a tuple of one event type + /// and identifier. + Stream> observedDeviceEvents() { + if (!isInstalled) { + _logger.printTrace("Xcode not found. Run 'flutter doctor' for more information."); + return null; + } + return _deviceIdentifierByEvent.stream; + } + + // Attach: d83d5bc53967baa0ee18626ba87b6254b2ab5418 + // Attach: 00008027-00192736010F802E + // Detach: d83d5bc53967baa0ee18626ba87b6254b2ab5418 + final RegExp _observationIdentifierPattern = RegExp(r'^(\w*): ([\w-]*)$'); + + Future _startObservingTetheredIOSDevices() async { + try { + if (_deviceObservationProcess != null) { + throw Exception('xcdevice observe restart failed'); + } + + // Run in interactive mode (via script) to convince + // xcdevice it has a terminal attached in order to redirect stdout. + _deviceObservationProcess = await _processUtils.start( + [ + 'script', + '-t', + '0', + '/dev/null', + ..._xcode.xcrunCommand(), + 'xcdevice', + 'observe', + '--both', + ], + ); + + final StreamSubscription stdoutSubscription = _deviceObservationProcess.stdout + .transform(utf8.decoder) + .transform(const LineSplitter()) + .listen((String line) { + + // xcdevice observe example output of UDIDs: + // + // Listening for all devices, on both interfaces. + // Attach: d83d5bc53967baa0ee18626ba87b6254b2ab5418 + // Attach: 00008027-00192736010F802E + // Detach: d83d5bc53967baa0ee18626ba87b6254b2ab5418 + // Attach: d83d5bc53967baa0ee18626ba87b6254b2ab5418 + final RegExpMatch match = _observationIdentifierPattern.firstMatch(line); + if (match != null && match.groupCount == 2) { + final String verb = match.group(1).toLowerCase(); + final String identifier = match.group(2); + if (verb.startsWith('attach')) { + _deviceIdentifierByEvent.add({ + XCDeviceEvent.attach: identifier + }); + } else if (verb.startsWith('detach')) { + _deviceIdentifierByEvent.add({ + XCDeviceEvent.detach: identifier + }); + } + } + }); + final StreamSubscription stderrSubscription = _deviceObservationProcess.stderr + .transform(utf8.decoder) + .transform(const LineSplitter()) + .listen((String line) { + _logger.printTrace('xcdevice observe error: $line'); + }); + unawaited(_deviceObservationProcess.exitCode.then((int status) { + _logger.printTrace('xcdevice exited with code $exitCode'); + unawaited(stdoutSubscription.cancel()); + unawaited(stderrSubscription.cancel()); + }).whenComplete(() async { + if (_deviceIdentifierByEvent.hasListener) { + // Tell listeners the process died. + await _deviceIdentifierByEvent.close(); + } + _deviceObservationProcess = null; + + // Reopen it so new listeners can resume polling. + _setupDeviceIdentifierByEventStream(); + })); + } on ProcessException catch (exception, stackTrace) { + _deviceIdentifierByEvent.addError(exception, stackTrace); + } on ArgumentError catch (exception, stackTrace) { + _deviceIdentifierByEvent.addError(exception, stackTrace); + } + } + + void _stopObservingTetheredIOSDevices() { + _deviceObservationProcess?.kill(); + } + + /// [timeout] defaults to 2 seconds. + Future> getAvailableIOSDevices({ Duration timeout }) async { + final List allAvailableDevices = await _getAllDevices(timeout: timeout ?? const Duration(seconds: 2)); + + if (allAvailableDevices == null) { + return const []; + } + + // [ + // { + // "simulator" : true, + // "operatingSystemVersion" : "13.3 (17K446)", + // "available" : true, + // "platform" : "com.apple.platform.appletvsimulator", + // "modelCode" : "AppleTV5,3", + // "identifier" : "CBB5E1ED-2172-446E-B4E7-F2B5823DBBA6", + // "architecture" : "x86_64", + // "modelName" : "Apple TV", + // "name" : "Apple TV" + // }, + // { + // "simulator" : false, + // "operatingSystemVersion" : "13.3 (17C54)", + // "interface" : "usb", + // "available" : true, + // "platform" : "com.apple.platform.iphoneos", + // "modelCode" : "iPhone8,1", + // "identifier" : "d83d5bc53967baa0ee18626ba87b6254b2ab5418", + // "architecture" : "arm64", + // "modelName" : "iPhone 6s", + // "name" : "iPhone" + // }, + // { + // "simulator" : true, + // "operatingSystemVersion" : "6.1.1 (17S445)", + // "available" : true, + // "platform" : "com.apple.platform.watchsimulator", + // "modelCode" : "Watch5,4", + // "identifier" : "2D74FB11-88A0-44D0-B81E-C0C142B1C94A", + // "architecture" : "i386", + // "modelName" : "Apple Watch Series 5 - 44mm", + // "name" : "Apple Watch Series 5 - 44mm" + // }, + // ... + + final List devices = []; + for (final dynamic device in allAvailableDevices) { + if (device is! Map) { + continue; + } + final Map deviceProperties = device as Map; + + // Only include iPhone, iPad, iPod, or other iOS devices. + if (!_isIPhoneOSDevice(deviceProperties)) { + continue; + } + + final Map errorProperties = _errorProperties(deviceProperties); + if (errorProperties != null) { + final String errorMessage = _parseErrorMessage(errorProperties); + if (errorMessage.contains('not paired')) { + UsageEvent('device', 'ios-trust-failure', flutterUsage: globals.flutterUsage).send(); + } + _logger.printTrace(errorMessage); + + final int code = _errorCode(errorProperties); + + // Temporary error -10: iPhone is busy: Preparing debugger support for iPhone. + // Sometimes the app launch will fail on these devices until Xcode is done setting up the device. + // Other times this is a false positive and the app will successfully launch despite the error. + if (code != -10) { + continue; + } + } + + final IOSDeviceInterface interface = _interfaceType(deviceProperties); + + // Only support USB devices, skip "network" interface (Xcode > Window > Devices and Simulators > Connect via network). + // TODO(jmagman): Remove this check once wirelessly detected devices can be observed and attached, https://github.com/flutter/flutter/issues/15072. + if (interface != IOSDeviceInterface.usb) { + continue; + } + + devices.add(IOSDevice( + device['identifier'] as String, + name: device['name'] as String, + cpuArchitecture: _cpuArchitecture(deviceProperties), + interfaceType: interface, + sdkVersion: _sdkVersion(deviceProperties), + iProxy: _iProxy, + fileSystem: globals.fs, + logger: _logger, + iosDeploy: _iosDeploy, + iMobileDevice: _iMobileDevice, + platform: globals.platform, + )); + } + return devices; + } + + /// Despite the name, com.apple.platform.iphoneos includes iPhone, iPads, and all iOS devices. + /// Excludes simulators. + static bool _isIPhoneOSDevice(Map deviceProperties) { + if (deviceProperties.containsKey('platform')) { + final String platform = deviceProperties['platform'] as String; + return platform == 'com.apple.platform.iphoneos'; + } + return false; + } + + static Map _errorProperties(Map deviceProperties) { + if (deviceProperties.containsKey('error')) { + return deviceProperties['error'] as Map; + } + return null; + } + + static int _errorCode(Map errorProperties) { + if (errorProperties.containsKey('code') && errorProperties['code'] is int) { + return errorProperties['code'] as int; + } + return null; + } + + static IOSDeviceInterface _interfaceType(Map deviceProperties) { + // Interface can be "usb", "network", or "none" for simulators + // and unknown future interfaces. + if (deviceProperties.containsKey('interface')) { + if ((deviceProperties['interface'] as String).toLowerCase() == 'network') { + return IOSDeviceInterface.network; + } else { + return IOSDeviceInterface.usb; + } + } + + return IOSDeviceInterface.none; + } + + static String _sdkVersion(Map deviceProperties) { + if (deviceProperties.containsKey('operatingSystemVersion')) { + // Parse out the OS version, ignore the build number in parentheses. + // "13.3 (17C54)" + final RegExp operatingSystemRegex = RegExp(r'(.*) \(.*\)$'); + final String operatingSystemVersion = deviceProperties['operatingSystemVersion'] as String; + return operatingSystemRegex.firstMatch(operatingSystemVersion.trim())?.group(1); + } + return null; + } + + DarwinArch _cpuArchitecture(Map deviceProperties) { + DarwinArch cpuArchitecture; + if (deviceProperties.containsKey('architecture')) { + final String architecture = deviceProperties['architecture'] as String; + try { + cpuArchitecture = getIOSArchForName(architecture); + } on Exception { + // Fallback to default iOS architecture. Future-proof against a + // theoretical version of Xcode that changes this string to something + // slightly different like "ARM64", or armv7 variations like + // armv7s and armv7f. + if (architecture.startsWith('armv7')) { + cpuArchitecture = DarwinArch.armv7; + } else { + cpuArchitecture = DarwinArch.arm64; + } + _logger.printError( + 'Unknown architecture $architecture, defaulting to ' + '${getNameForDarwinArch(cpuArchitecture)}', + ); + } + } + return cpuArchitecture; + } + + /// Error message parsed from xcdevice. null if no error. + static String _parseErrorMessage(Map errorProperties) { + // { + // "simulator" : false, + // "operatingSystemVersion" : "13.3 (17C54)", + // "interface" : "usb", + // "available" : false, + // "platform" : "com.apple.platform.iphoneos", + // "modelCode" : "iPhone8,1", + // "identifier" : "98206e7a4afd4aedaff06e687594e089dede3c44", + // "architecture" : "arm64", + // "modelName" : "iPhone 6s", + // "name" : "iPhone", + // "error" : { + // "code" : -9, + // "failureReason" : "", + // "underlyingErrors" : [ + // { + // "code" : 5, + // "failureReason" : "allowsSecureServices: 1. isConnected: 0. Platform: >. DTDKDeviceIdentifierIsIDID: 0", + // "description" : "📱 -- Failed _shouldMakeReadyForDevelopment check even though device is not locked by passcode.", + // "recoverySuggestion" : "", + // "domain" : "com.apple.platform.iphoneos" + // } + // ], + // "description" : "iPhone is not paired with your computer.", + // "recoverySuggestion" : "To use iPhone with Xcode, unlock it and choose to trust this computer when prompted.", + // "domain" : "com.apple.platform.iphoneos" + // } + // }, + // { + // "simulator" : false, + // "operatingSystemVersion" : "13.3 (17C54)", + // "interface" : "usb", + // "available" : false, + // "platform" : "com.apple.platform.iphoneos", + // "modelCode" : "iPhone8,1", + // "identifier" : "d83d5bc53967baa0ee18626ba87b6254b2ab5418", + // "architecture" : "arm64", + // "modelName" : "iPhone 6s", + // "name" : "iPhone", + // "error" : { + // "code" : -9, + // "failureReason" : "", + // "description" : "iPhone is not paired with your computer.", + // "domain" : "com.apple.platform.iphoneos" + // } + // } + // ... + + if (errorProperties == null) { + return null; + } + + final StringBuffer errorMessage = StringBuffer('Error: '); + + if (errorProperties.containsKey('description')) { + final String description = errorProperties['description'] as String; + errorMessage.write(description); + if (!description.endsWith('.')) { + errorMessage.write('.'); + } + } else { + errorMessage.write('Xcode pairing error.'); + } + + if (errorProperties.containsKey('recoverySuggestion')) { + final String recoverySuggestion = errorProperties['recoverySuggestion'] as String; + errorMessage.write(' $recoverySuggestion'); + } + + final int code = _errorCode(errorProperties); + if (code != null) { + errorMessage.write(' (code $code)'); + } + + return errorMessage.toString(); + } + + /// List of all devices reporting errors. + Future> getDiagnostics() async { + final List allAvailableDevices = await _getAllDevices( + useCache: true, + timeout: const Duration(seconds: 2) + ); + + if (allAvailableDevices == null) { + return const []; + } + + final List diagnostics = []; + for (final dynamic device in allAvailableDevices) { + if (device is! Map) { + continue; + } + final Map deviceProperties = device as Map; + final Map errorProperties = _errorProperties(deviceProperties); + final String errorMessage = _parseErrorMessage(errorProperties); + if (errorMessage != null) { + diagnostics.add(errorMessage); + } + } + return diagnostics; + } +} diff --git a/packages/flutter_tools/lib/src/macos/xcode.dart b/packages/flutter_tools/lib/src/macos/xcode.dart index a181927b74..d48dca75f7 100644 --- a/packages/flutter_tools/lib/src/macos/xcode.dart +++ b/packages/flutter_tools/lib/src/macos/xcode.dart @@ -10,7 +10,6 @@ import 'package:file/memory.dart'; import 'package:meta/meta.dart'; import 'package:process/process.dart'; -import '../artifacts.dart'; import '../base/common.dart'; import '../base/file_system.dart'; import '../base/io.dart'; @@ -19,15 +18,7 @@ import '../base/platform.dart'; import '../base/process.dart'; import '../base/version.dart'; import '../build_info.dart'; -import '../cache.dart'; -import '../convert.dart'; -import '../globals.dart' as globals; -import '../ios/devices.dart'; -import '../ios/ios_deploy.dart'; -import '../ios/iproxy.dart'; -import '../ios/mac.dart'; import '../ios/xcodeproj.dart'; -import '../reporting/reporting.dart'; Version get xcodeRequiredVersion => Version(12, 0, 1, text: '12.0.1'); @@ -227,479 +218,3 @@ EnvironmentType environmentTypeFromSdkroot(Directory sdkroot) { assert(false); return null; } - -enum XCDeviceEvent { - attach, - detach, -} - -/// A utility class for interacting with Xcode xcdevice command line tools. -class XCDevice { - XCDevice({ - @required Artifacts artifacts, - @required Cache cache, - @required ProcessManager processManager, - @required Logger logger, - @required Xcode xcode, - @required Platform platform, - @required IProxy iproxy, - }) : _processUtils = ProcessUtils(logger: logger, processManager: processManager), - _logger = logger, - _iMobileDevice = IMobileDevice( - artifacts: artifacts, - cache: cache, - logger: logger, - processManager: processManager, - ), - _iosDeploy = IOSDeploy( - artifacts: artifacts, - cache: cache, - logger: logger, - platform: platform, - processManager: processManager, - ), - _iProxy = iproxy, - _xcode = xcode { - - _setupDeviceIdentifierByEventStream(); - } - - void dispose() { - _deviceObservationProcess?.kill(); - } - - final ProcessUtils _processUtils; - final Logger _logger; - final IMobileDevice _iMobileDevice; - final IOSDeploy _iosDeploy; - final Xcode _xcode; - final IProxy _iProxy; - - List _cachedListResults; - Process _deviceObservationProcess; - StreamController> _deviceIdentifierByEvent; - - void _setupDeviceIdentifierByEventStream() { - // _deviceIdentifierByEvent Should always be available for listeners - // in case polling needs to be stopped and restarted. - _deviceIdentifierByEvent = StreamController>.broadcast( - onListen: _startObservingTetheredIOSDevices, - onCancel: _stopObservingTetheredIOSDevices, - ); - } - - bool get isInstalled => _xcode.isInstalledAndMeetsVersionCheck; - - Future> _getAllDevices({ - bool useCache = false, - @required Duration timeout - }) async { - if (!isInstalled) { - _logger.printTrace("Xcode not found. Run 'flutter doctor' for more information."); - return null; - } - if (useCache && _cachedListResults != null) { - return _cachedListResults; - } - try { - // USB-tethered devices should be found quickly. 1 second timeout is faster than the default. - final RunResult result = await _processUtils.run( - [ - ..._xcode.xcrunCommand(), - 'xcdevice', - 'list', - '--timeout', - timeout.inSeconds.toString(), - ], - throwOnError: true, - ); - if (result.exitCode == 0) { - final List listResults = json.decode(result.stdout) as List; - _cachedListResults = listResults; - return listResults; - } - _logger.printTrace('xcdevice returned an error:\n${result.stderr}'); - } on ProcessException catch (exception) { - _logger.printTrace('Process exception running xcdevice list:\n$exception'); - } on ArgumentError catch (exception) { - _logger.printTrace('Argument exception running xcdevice list:\n$exception'); - } - - return null; - } - - /// Observe identifiers (UDIDs) of devices as they attach and detach. - /// - /// Each attach and detach event is a tuple of one event type - /// and identifier. - Stream> observedDeviceEvents() { - if (!isInstalled) { - _logger.printTrace("Xcode not found. Run 'flutter doctor' for more information."); - return null; - } - return _deviceIdentifierByEvent.stream; - } - - // Attach: d83d5bc53967baa0ee18626ba87b6254b2ab5418 - // Attach: 00008027-00192736010F802E - // Detach: d83d5bc53967baa0ee18626ba87b6254b2ab5418 - final RegExp _observationIdentifierPattern = RegExp(r'^(\w*): ([\w-]*)$'); - - Future _startObservingTetheredIOSDevices() async { - try { - if (_deviceObservationProcess != null) { - throw Exception('xcdevice observe restart failed'); - } - - // Run in interactive mode (via script) to convince - // xcdevice it has a terminal attached in order to redirect stdout. - _deviceObservationProcess = await _processUtils.start( - [ - 'script', - '-t', - '0', - '/dev/null', - ..._xcode.xcrunCommand(), - 'xcdevice', - 'observe', - '--both', - ], - ); - - final StreamSubscription stdoutSubscription = _deviceObservationProcess.stdout - .transform(utf8.decoder) - .transform(const LineSplitter()) - .listen((String line) { - - // xcdevice observe example output of UDIDs: - // - // Listening for all devices, on both interfaces. - // Attach: d83d5bc53967baa0ee18626ba87b6254b2ab5418 - // Attach: 00008027-00192736010F802E - // Detach: d83d5bc53967baa0ee18626ba87b6254b2ab5418 - // Attach: d83d5bc53967baa0ee18626ba87b6254b2ab5418 - final RegExpMatch match = _observationIdentifierPattern.firstMatch(line); - if (match != null && match.groupCount == 2) { - final String verb = match.group(1).toLowerCase(); - final String identifier = match.group(2); - if (verb.startsWith('attach')) { - _deviceIdentifierByEvent.add({ - XCDeviceEvent.attach: identifier - }); - } else if (verb.startsWith('detach')) { - _deviceIdentifierByEvent.add({ - XCDeviceEvent.detach: identifier - }); - } - } - }); - final StreamSubscription stderrSubscription = _deviceObservationProcess.stderr - .transform(utf8.decoder) - .transform(const LineSplitter()) - .listen((String line) { - _logger.printTrace('xcdevice observe error: $line'); - }); - unawaited(_deviceObservationProcess.exitCode.then((int status) { - _logger.printTrace('xcdevice exited with code $exitCode'); - unawaited(stdoutSubscription.cancel()); - unawaited(stderrSubscription.cancel()); - }).whenComplete(() async { - if (_deviceIdentifierByEvent.hasListener) { - // Tell listeners the process died. - await _deviceIdentifierByEvent.close(); - } - _deviceObservationProcess = null; - - // Reopen it so new listeners can resume polling. - _setupDeviceIdentifierByEventStream(); - })); - } on ProcessException catch (exception, stackTrace) { - _deviceIdentifierByEvent.addError(exception, stackTrace); - } on ArgumentError catch (exception, stackTrace) { - _deviceIdentifierByEvent.addError(exception, stackTrace); - } - } - - void _stopObservingTetheredIOSDevices() { - _deviceObservationProcess?.kill(); - } - - /// [timeout] defaults to 2 seconds. - Future> getAvailableIOSDevices({ Duration timeout }) async { - final List allAvailableDevices = await _getAllDevices(timeout: timeout ?? const Duration(seconds: 2)); - - if (allAvailableDevices == null) { - return const []; - } - - // [ - // { - // "simulator" : true, - // "operatingSystemVersion" : "13.3 (17K446)", - // "available" : true, - // "platform" : "com.apple.platform.appletvsimulator", - // "modelCode" : "AppleTV5,3", - // "identifier" : "CBB5E1ED-2172-446E-B4E7-F2B5823DBBA6", - // "architecture" : "x86_64", - // "modelName" : "Apple TV", - // "name" : "Apple TV" - // }, - // { - // "simulator" : false, - // "operatingSystemVersion" : "13.3 (17C54)", - // "interface" : "usb", - // "available" : true, - // "platform" : "com.apple.platform.iphoneos", - // "modelCode" : "iPhone8,1", - // "identifier" : "d83d5bc53967baa0ee18626ba87b6254b2ab5418", - // "architecture" : "arm64", - // "modelName" : "iPhone 6s", - // "name" : "iPhone" - // }, - // { - // "simulator" : true, - // "operatingSystemVersion" : "6.1.1 (17S445)", - // "available" : true, - // "platform" : "com.apple.platform.watchsimulator", - // "modelCode" : "Watch5,4", - // "identifier" : "2D74FB11-88A0-44D0-B81E-C0C142B1C94A", - // "architecture" : "i386", - // "modelName" : "Apple Watch Series 5 - 44mm", - // "name" : "Apple Watch Series 5 - 44mm" - // }, - // ... - - final List devices = []; - for (final dynamic device in allAvailableDevices) { - if (device is! Map) { - continue; - } - final Map deviceProperties = device as Map; - - // Only include iPhone, iPad, iPod, or other iOS devices. - if (!_isIPhoneOSDevice(deviceProperties)) { - continue; - } - - final Map errorProperties = _errorProperties(deviceProperties); - if (errorProperties != null) { - final String errorMessage = _parseErrorMessage(errorProperties); - if (errorMessage.contains('not paired')) { - UsageEvent('device', 'ios-trust-failure', flutterUsage: globals.flutterUsage).send(); - } - _logger.printTrace(errorMessage); - - final int code = _errorCode(errorProperties); - - // Temporary error -10: iPhone is busy: Preparing debugger support for iPhone. - // Sometimes the app launch will fail on these devices until Xcode is done setting up the device. - // Other times this is a false positive and the app will successfully launch despite the error. - if (code != -10) { - continue; - } - } - - final IOSDeviceInterface interface = _interfaceType(deviceProperties); - - // Only support USB devices, skip "network" interface (Xcode > Window > Devices and Simulators > Connect via network). - // TODO(jmagman): Remove this check once wirelessly detected devices can be observed and attached, https://github.com/flutter/flutter/issues/15072. - if (interface != IOSDeviceInterface.usb) { - continue; - } - - devices.add(IOSDevice( - device['identifier'] as String, - name: device['name'] as String, - cpuArchitecture: _cpuArchitecture(deviceProperties), - interfaceType: interface, - sdkVersion: _sdkVersion(deviceProperties), - iProxy: _iProxy, - fileSystem: globals.fs, - logger: _logger, - iosDeploy: _iosDeploy, - iMobileDevice: _iMobileDevice, - platform: globals.platform, - )); - } - return devices; - } - - /// Despite the name, com.apple.platform.iphoneos includes iPhone, iPads, and all iOS devices. - /// Excludes simulators. - static bool _isIPhoneOSDevice(Map deviceProperties) { - if (deviceProperties.containsKey('platform')) { - final String platform = deviceProperties['platform'] as String; - return platform == 'com.apple.platform.iphoneos'; - } - return false; - } - - static Map _errorProperties(Map deviceProperties) { - if (deviceProperties.containsKey('error')) { - return deviceProperties['error'] as Map; - } - return null; - } - - static int _errorCode(Map errorProperties) { - if (errorProperties.containsKey('code') && errorProperties['code'] is int) { - return errorProperties['code'] as int; - } - return null; - } - - static IOSDeviceInterface _interfaceType(Map deviceProperties) { - // Interface can be "usb", "network", or "none" for simulators - // and unknown future interfaces. - if (deviceProperties.containsKey('interface')) { - if ((deviceProperties['interface'] as String).toLowerCase() == 'network') { - return IOSDeviceInterface.network; - } else { - return IOSDeviceInterface.usb; - } - } - - return IOSDeviceInterface.none; - } - - static String _sdkVersion(Map deviceProperties) { - if (deviceProperties.containsKey('operatingSystemVersion')) { - // Parse out the OS version, ignore the build number in parentheses. - // "13.3 (17C54)" - final RegExp operatingSystemRegex = RegExp(r'(.*) \(.*\)$'); - final String operatingSystemVersion = deviceProperties['operatingSystemVersion'] as String; - return operatingSystemRegex.firstMatch(operatingSystemVersion.trim())?.group(1); - } - return null; - } - - DarwinArch _cpuArchitecture(Map deviceProperties) { - DarwinArch cpuArchitecture; - if (deviceProperties.containsKey('architecture')) { - final String architecture = deviceProperties['architecture'] as String; - try { - cpuArchitecture = getIOSArchForName(architecture); - } on Exception { - // Fallback to default iOS architecture. Future-proof against a - // theoretical version of Xcode that changes this string to something - // slightly different like "ARM64", or armv7 variations like - // armv7s and armv7f. - if (architecture.startsWith('armv7')) { - cpuArchitecture = DarwinArch.armv7; - } else { - cpuArchitecture = DarwinArch.arm64; - } - _logger.printError( - 'Unknown architecture $architecture, defaulting to ' - '${getNameForDarwinArch(cpuArchitecture)}', - ); - } - } - return cpuArchitecture; - } - - /// Error message parsed from xcdevice. null if no error. - static String _parseErrorMessage(Map errorProperties) { - // { - // "simulator" : false, - // "operatingSystemVersion" : "13.3 (17C54)", - // "interface" : "usb", - // "available" : false, - // "platform" : "com.apple.platform.iphoneos", - // "modelCode" : "iPhone8,1", - // "identifier" : "98206e7a4afd4aedaff06e687594e089dede3c44", - // "architecture" : "arm64", - // "modelName" : "iPhone 6s", - // "name" : "iPhone", - // "error" : { - // "code" : -9, - // "failureReason" : "", - // "underlyingErrors" : [ - // { - // "code" : 5, - // "failureReason" : "allowsSecureServices: 1. isConnected: 0. Platform: >. DTDKDeviceIdentifierIsIDID: 0", - // "description" : "📱 -- Failed _shouldMakeReadyForDevelopment check even though device is not locked by passcode.", - // "recoverySuggestion" : "", - // "domain" : "com.apple.platform.iphoneos" - // } - // ], - // "description" : "iPhone is not paired with your computer.", - // "recoverySuggestion" : "To use iPhone with Xcode, unlock it and choose to trust this computer when prompted.", - // "domain" : "com.apple.platform.iphoneos" - // } - // }, - // { - // "simulator" : false, - // "operatingSystemVersion" : "13.3 (17C54)", - // "interface" : "usb", - // "available" : false, - // "platform" : "com.apple.platform.iphoneos", - // "modelCode" : "iPhone8,1", - // "identifier" : "d83d5bc53967baa0ee18626ba87b6254b2ab5418", - // "architecture" : "arm64", - // "modelName" : "iPhone 6s", - // "name" : "iPhone", - // "error" : { - // "code" : -9, - // "failureReason" : "", - // "description" : "iPhone is not paired with your computer.", - // "domain" : "com.apple.platform.iphoneos" - // } - // } - // ... - - if (errorProperties == null) { - return null; - } - - final StringBuffer errorMessage = StringBuffer('Error: '); - - if (errorProperties.containsKey('description')) { - final String description = errorProperties['description'] as String; - errorMessage.write(description); - if (!description.endsWith('.')) { - errorMessage.write('.'); - } - } else { - errorMessage.write('Xcode pairing error.'); - } - - if (errorProperties.containsKey('recoverySuggestion')) { - final String recoverySuggestion = errorProperties['recoverySuggestion'] as String; - errorMessage.write(' $recoverySuggestion'); - } - - final int code = _errorCode(errorProperties); - if (code != null) { - errorMessage.write(' (code $code)'); - } - - return errorMessage.toString(); - } - - /// List of all devices reporting errors. - Future> getDiagnostics() async { - final List allAvailableDevices = await _getAllDevices( - useCache: true, - timeout: const Duration(seconds: 2) - ); - - if (allAvailableDevices == null) { - return const []; - } - - final List diagnostics = []; - for (final dynamic device in allAvailableDevices) { - if (device is! Map) { - continue; - } - final Map deviceProperties = device as Map; - final Map errorProperties = _errorProperties(deviceProperties); - final String errorMessage = _parseErrorMessage(errorProperties); - if (errorMessage != null) { - diagnostics.add(errorMessage); - } - } - return diagnostics; - } -} diff --git a/packages/flutter_tools/test/general.shard/ios/devices_test.dart b/packages/flutter_tools/test/general.shard/ios/devices_test.dart index bd2256e293..7d81aa1799 100644 --- a/packages/flutter_tools/test/general.shard/ios/devices_test.dart +++ b/packages/flutter_tools/test/general.shard/ios/devices_test.dart @@ -24,7 +24,7 @@ import 'package:flutter_tools/src/ios/ios_deploy.dart'; import 'package:flutter_tools/src/ios/ios_workflow.dart'; import 'package:flutter_tools/src/ios/iproxy.dart'; import 'package:flutter_tools/src/ios/mac.dart'; -import 'package:flutter_tools/src/macos/xcode.dart'; +import 'package:flutter_tools/src/macos/xcdevice.dart'; import 'package:mockito/mockito.dart'; import '../../src/common.dart'; diff --git a/packages/flutter_tools/test/general.shard/macos/xcode_test.dart b/packages/flutter_tools/test/general.shard/macos/xcode_test.dart index e0ac06ba92..e6a03fe43b 100644 --- a/packages/flutter_tools/test/general.shard/macos/xcode_test.dart +++ b/packages/flutter_tools/test/general.shard/macos/xcode_test.dart @@ -15,6 +15,7 @@ import 'package:flutter_tools/src/cache.dart'; import 'package:flutter_tools/src/ios/devices.dart'; import 'package:flutter_tools/src/ios/iproxy.dart'; import 'package:flutter_tools/src/ios/xcodeproj.dart'; +import 'package:flutter_tools/src/macos/xcdevice.dart'; import 'package:flutter_tools/src/macos/xcode.dart'; import 'package:mockito/mockito.dart';