Revert "Replace ideviceinfo and idevice_id with xcdevice (#49854)" (#50243)

This reverts commit 3aa7a80053.
This commit is contained in:
Jenn Magder
2020-02-05 18:00:31 -08:00
committed by GitHub
parent eb4b6dc91b
commit ce6fbf6668
6 changed files with 264 additions and 863 deletions

View File

@@ -166,11 +166,6 @@ Future<T> runInContext<T>(
fileSystem: globals.fs,
xcodeProjectInterpreter: xcodeProjectInterpreter,
),
XCDevice: () => XCDevice(
processManager: globals.processManager,
logger: globals.logger,
xcode: globals.xcode,
),
XcodeProjectInterpreter: () => XcodeProjectInterpreter(
logger: globals.logger,
processManager: globals.processManager,

View File

@@ -57,8 +57,6 @@ Xcode get xcode => context.get<Xcode>();
FlutterVersion get flutterVersion => context.get<FlutterVersion>();
IMobileDevice get iMobileDevice => context.get<IMobileDevice>();
XCDevice get xcdevice => context.get<XCDevice>();
/// Display an error level message to the user. Commands should use this if they
/// fail in some way.
///

View File

@@ -5,7 +5,6 @@
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:platform/platform.dart';
import '../application_package.dart';
import '../artifacts.dart';
@@ -19,7 +18,6 @@ import '../build_info.dart';
import '../convert.dart';
import '../device.dart';
import '../globals.dart' as globals;
import '../macos/xcode.dart';
import '../mdns_discovery.dart';
import '../project.dart';
import '../protocol_discovery.dart';
@@ -113,18 +111,11 @@ class IOSDevices extends PollingDeviceDiscovery {
bool get canListAnything => iosWorkflow.canListDevices;
@override
Future<List<Device>> pollingGetDevices() => IOSDevice.getAttachedDevices(globals.platform, globals.xcdevice);
@override
Future<List<String>> getDiagnostics() => IOSDevice.getDiagnostics(globals.platform, globals.xcdevice);
Future<List<Device>> pollingGetDevices() => IOSDevice.getAttachedDevices();
}
class IOSDevice extends Device {
IOSDevice(String id, {
@required this.name,
@required this.cpuArchitecture,
@required String sdkVersion,
})
IOSDevice(String id, { this.name, String sdkVersion })
: _sdkVersion = sdkVersion,
super(
id,
@@ -166,8 +157,6 @@ class IOSDevice extends Device {
@override
final String name;
final DarwinArch cpuArchitecture;
Map<IOSApp, DeviceLogReader> _logReaders;
DevicePortForwarder _portForwarder;
@@ -181,20 +170,34 @@ class IOSDevice extends Device {
@override
bool get supportsStartPaused => false;
static Future<List<IOSDevice>> getAttachedDevices(Platform platform, XCDevice xcdevice) async {
if (!platform.isMacOS) {
throw UnsupportedError('Control of iOS devices or simulators only supported on macOS.');
static Future<List<IOSDevice>> getAttachedDevices() async {
if (!globals.platform.isMacOS) {
throw UnsupportedError('Control of iOS devices or simulators only supported on Mac OS.');
}
if (!globals.iMobileDevice.isInstalled) {
return <IOSDevice>[];
}
return await xcdevice.getAvailableTetheredIOSDevices();
}
final List<IOSDevice> devices = <IOSDevice>[];
for (String id in (await globals.iMobileDevice.getAvailableDeviceIDs()).split('\n')) {
id = id.trim();
if (id.isEmpty) {
continue;
}
static Future<List<String>> getDiagnostics(Platform platform, XCDevice xcdevice) async {
if (!platform.isMacOS) {
return const <String>['Control of iOS devices or simulators only supported on macOS.'];
try {
final String deviceName = await globals.iMobileDevice.getInfoForDevice(id, 'DeviceName');
final String sdkVersion = await globals.iMobileDevice.getInfoForDevice(id, 'ProductVersion');
devices.add(IOSDevice(id, name: deviceName, sdkVersion: sdkVersion));
} on IOSDeviceNotFoundError catch (error) {
// Unable to find device with given udid. Possibly a network device.
globals.printTrace('Error getting attached iOS device: $error');
} on IOSDeviceNotTrustedError catch (error) {
globals.printTrace('Error getting attached iOS device information: $error');
UsageEvent('device', 'ios-trust-failure').send();
}
}
return await xcdevice.getDiagnostics();
return devices;
}
@override
@@ -277,13 +280,24 @@ class IOSDevice extends Device {
// TODO(chinmaygarde): Use mainPath, route.
globals.printTrace('Building ${package.name} for $id');
String cpuArchitecture;
try {
cpuArchitecture = await globals.iMobileDevice.getInfoForDevice(id, 'CPUArchitecture');
} on IOSDeviceNotFoundError catch (e) {
globals.printError(e.message);
return LaunchResult.failed();
}
final DarwinArch iosArch = getIOSArchForName(cpuArchitecture);
// Step 1: Build the precompiled/DBC application if necessary.
final XcodeBuildResult buildResult = await buildXcodeProject(
app: package as BuildableIOSApp,
buildInfo: debuggingOptions.buildInfo,
targetOverride: mainPath,
buildForDevice: true,
activeArch: cpuArchitecture,
activeArch: iosArch,
);
if (!buildResult.success) {
globals.printError('Could not build the precompiled application for the device.');

View File

@@ -13,11 +13,7 @@ import '../base/file_system.dart';
import '../base/io.dart';
import '../base/logger.dart';
import '../base/process.dart';
import '../build_info.dart';
import '../convert.dart';
import '../ios/devices.dart';
import '../ios/xcodeproj.dart';
import '../reporting/reporting.dart';
const int kXcodeRequiredVersionMajor = 10;
const int kXcodeRequiredVersionMinor = 2;
@@ -189,315 +185,3 @@ class Xcode {
);
}
}
/// A utility class for interacting with Xcode xcdevice command line tools.
class XCDevice {
XCDevice({
@required ProcessManager processManager,
@required Logger logger,
@required Xcode xcode,
}) : _processUtils = ProcessUtils(logger: logger, processManager: processManager),
_logger = logger,
_xcode = xcode;
final ProcessUtils _processUtils;
final Logger _logger;
final Xcode _xcode;
bool get isInstalled => _xcode.isInstalledAndMeetsVersionCheck && xcdevicePath != null;
String _xcdevicePath;
String get xcdevicePath {
if (_xcdevicePath == null) {
try {
_xcdevicePath = _processUtils.runSync(
<String>[
'xcrun',
'--find',
'xcdevice'
],
throwOnError: true,
).stdout.trim();
} on ProcessException catch (exception) {
_logger.printTrace('Process exception finding xcdevice:\n$exception');
} on ArgumentError catch (exception) {
_logger.printTrace('Argument exception finding xcdevice:\n$exception');
}
}
return _xcdevicePath;
}
Future<List<dynamic>> _getAllDevices({bool useCache = false}) 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(
<String>[
'xcrun',
'xcdevice',
'list',
'--timeout',
'1',
],
throwOnError: true,
);
if (result.exitCode == 0) {
final List<dynamic> listResults = json.decode(result.stdout) as List<dynamic>;
_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;
}
List<dynamic> _cachedListResults;
/// List of devices available over USB.
Future<List<IOSDevice>> getAvailableTetheredIOSDevices() async {
final List<dynamic> allAvailableDevices = await _getAllDevices();
if (allAvailableDevices == null) {
return const <IOSDevice>[];
}
// [
// {
// "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<IOSDevice> devices = <IOSDevice>[];
for (final dynamic device in allAvailableDevices) {
if (device is! Map) {
continue;
}
final Map<String, dynamic> deviceProperties = device as Map<String, dynamic>;
// Only include iPhone, iPad, iPod, or other iOS devices.
if (!_isIPhoneOSDevice(deviceProperties)) {
continue;
}
final String errorMessage = _parseErrorMessage(deviceProperties);
if (errorMessage != null) {
if (errorMessage.contains('not paired')) {
UsageEvent('device', 'ios-trust-failure').send();
}
_logger.printTrace(errorMessage);
continue;
}
// In case unavailable without an error (may not be possible...)
if (!_isAvailable(deviceProperties)) {
continue;
}
// Only support USB devices, skip "network" interface (Xcode > Window > Devices and Simulators > Connect via network).
if (!_isUSBTethered(deviceProperties)) {
continue;
}
devices.add(IOSDevice(
device['identifier'] as String,
name: device['name'] as String,
cpuArchitecture: _cpuArchitecture(deviceProperties),
sdkVersion: _sdkVersion(deviceProperties),
));
}
return devices;
}
/// Despite the name, com.apple.platform.iphoneos includes iPhone, iPads, and all iOS devices.
/// Excludes simulators.
static bool _isIPhoneOSDevice(Map<String, dynamic> deviceProperties) {
if (deviceProperties.containsKey('platform')) {
final String platform = deviceProperties['platform'] as String;
return platform == 'com.apple.platform.iphoneos';
}
return false;
}
static bool _isAvailable(Map<String, dynamic> deviceProperties) {
return deviceProperties.containsKey('available') && (deviceProperties['available'] as bool);
}
static bool _isUSBTethered(Map<String, dynamic> deviceProperties) {
// Interface can be "usb", "network", or not present for simulators.
return deviceProperties.containsKey('interface') &&
(deviceProperties['interface'] as String).toLowerCase() == 'usb';
}
static String _sdkVersion(Map<String, dynamic> 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<String, dynamic> deviceProperties) {
DarwinArch cpuArchitecture;
if (deviceProperties.containsKey('architecture')) {
final String architecture = deviceProperties['architecture'] as String;
try {
cpuArchitecture = getIOSArchForName(architecture);
} catch (error) {
// Fallback to default iOS architecture. Future-proof against a theoretical version
// of Xcode that changes this string to something slightly different like "ARM64".
cpuArchitecture ??= defaultIOSArchs.first;
_logger.printError('Unknown architecture $architecture, defaulting to ${getNameForDarwinArch(cpuArchitecture)}');
}
}
return cpuArchitecture;
}
/// Error message parsed from xcdevice. null if no error.
static String _parseErrorMessage(Map<String, dynamic> deviceProperties) {
// {
// "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: <DVTPlatform:0x7f804ce32880:'com.apple.platform.iphoneos':<DVTFilePath:0x7f804ce32800:'\/Users\/magder\/Applications\/Xcode_11-3-1.app\/Contents\/Developer\/Platforms\/iPhoneOS.platform'>>. DTDKDeviceIdentifierIsIDID: 0",
// "description" : "📱<DVTiOSDevice (0x7f801f190450), iPhone, iPhone, 13.3 (17C54), d83d5bc53967baa0ee18626ba87b6254b2ab5418> -- 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 (!deviceProperties.containsKey('error')) {
return null;
}
final Map<String, dynamic> error = deviceProperties['error'] as Map<String, dynamic>;
final StringBuffer errorMessage = StringBuffer('Error: ');
if (error.containsKey('description')) {
final String description = error['description'] as String;
errorMessage.write(description);
if (!description.endsWith('.')) {
errorMessage.write('.');
}
} else {
errorMessage.write('Xcode pairing error.');
}
if (error.containsKey('recoverySuggestion')) {
final String recoverySuggestion = error['recoverySuggestion'] as String;
errorMessage.write(' $recoverySuggestion');
}
if (error.containsKey('code') && error['code'] is int) {
final int code = error['code'] as int;
errorMessage.write(' (code $code)');
}
return errorMessage.toString();
}
/// List of all devices reporting errors.
Future<List<String>> getDiagnostics() async {
final List<dynamic> allAvailableDevices = await _getAllDevices(useCache: true);
if (allAvailableDevices == null) {
return const <String>[];
}
final List<String> diagnostics = <String>[];
for (final dynamic device in allAvailableDevices) {
if (device is! Map) {
continue;
}
final Map<String, dynamic> deviceProperties = device as Map<String, dynamic>;
final String errorMessage = _parseErrorMessage(deviceProperties);
if (errorMessage != null) {
diagnostics.add(errorMessage);
}
}
return diagnostics;
}
}