From e365453eacb8163afc68a75d2b73085e64caff09 Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Wed, 4 May 2016 12:32:46 -0700 Subject: [PATCH] better parsing of adb devices output (#3726) * better parsing of adb devices output * re-order lines --- .../flutter_tools/lib/src/android/adb.dart | 4 +- .../lib/src/android/android_device.dart | 53 +++++++++---------- .../test/android_device_test.dart | 8 +++ 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/packages/flutter_tools/lib/src/android/adb.dart b/packages/flutter_tools/lib/src/android/adb.dart index aa939b58a5..6bdaa64148 100644 --- a/packages/flutter_tools/lib/src/android/adb.dart +++ b/packages/flutter_tools/lib/src/android/adb.dart @@ -166,7 +166,7 @@ class AdbDevice { // 'TA95000FQA device usb:340787200X product:peregrine_retus model:XT1045 device:peregrine' // '015d172c98400a03 device usb:340787200X product:nakasi model:Nexus_7 device:grouper' - Match match = deviceRegex.firstMatch(deviceInfo); + Match match = kDeviceRegex.firstMatch(deviceInfo); id = match[1]; status = match[2]; @@ -185,7 +185,7 @@ class AdbDevice { modelID = cleanAdbDeviceName(modelID); } - static final RegExp deviceRegex = new RegExp(r'^(\S+)\s+(\S+)(.*)'); + static final RegExp kDeviceRegex = new RegExp(r'^(\S+)\s+(\S+)(.*)'); /// Always non-null; something like `TA95000FQA`. String id; diff --git a/packages/flutter_tools/lib/src/android/android_device.dart b/packages/flutter_tools/lib/src/android/android_device.dart index ced99aa6a1..4a7dc67005 100644 --- a/packages/flutter_tools/lib/src/android/android_device.dart +++ b/packages/flutter_tools/lib/src/android/android_device.dart @@ -403,6 +403,9 @@ class AndroidDevice extends Device { } } +// 015d172c98400a03 device usb:340787200X product:nakasi model:Nexus_7 device:grouper +final RegExp _kDeviceRegex = new RegExp(r'^(\S+)\s+(\S+)(.*)'); + /// Return the list of connected ADB devices. /// /// [mockAdbOutput] is public for testing. @@ -419,15 +422,6 @@ List getAdbDevices({ String mockAdbOutput }) { output = mockAdbOutput.trim().split('\n'); } - // 015d172c98400a03 device usb:340787200X product:nakasi model:Nexus_7 device:grouper - RegExp deviceRegExLong = new RegExp( - r'^(\S+)\s+device\s+.*product:(\S+)\s+model:(\S+)\s+device:(\S+)$'); - - // 0149947A0D01500C device usb:340787200X - // emulator-5612 host features:shell_2 - // emulator-5554 offline - RegExp deviceRegExShort = new RegExp(r'^(\S+)\s+(\S+)(\s+\S+)?$'); - for (String line in output) { // Skip lines like: * daemon started successfully * if (line.startsWith('* daemon ')) @@ -436,26 +430,26 @@ List getAdbDevices({ String mockAdbOutput }) { if (line.startsWith('List of devices')) continue; - if (deviceRegExLong.hasMatch(line)) { - Match match = deviceRegExLong.firstMatch(line); - String deviceID = match[1]; - String productID = match[2]; - String modelID = match[3]; - String deviceCodeName = match[4]; + if (_kDeviceRegex.hasMatch(line)) { + Match match = _kDeviceRegex.firstMatch(line); - if (modelID != null) - modelID = cleanAdbDeviceName(modelID); - - devices.add(new AndroidDevice( - deviceID, - productID: productID, - modelID: modelID, - deviceCodeName: deviceCodeName - )); - } else if (deviceRegExShort.hasMatch(line)) { - Match match = deviceRegExShort.firstMatch(line); String deviceID = match[1]; String deviceState = match[2]; + String rest = match[3]; + + Map info = {}; + if (rest != null && rest.isNotEmpty) { + rest = rest.trim(); + for (String data in rest.split(' ')) { + if (data.contains(':')) { + List fields = data.split(':'); + info[fields[0]] = fields[1]; + } + } + } + + if (info['model'] != null) + info['model'] = cleanAdbDeviceName(info['model']); if (deviceState == 'unauthorized') { printError( @@ -465,7 +459,12 @@ List getAdbDevices({ String mockAdbOutput }) { } else if (deviceState == 'offline') { printError('Device $deviceID is offline.'); } else { - devices.add(new AndroidDevice(deviceID, modelID: deviceID)); + devices.add(new AndroidDevice( + deviceID, + productID: info['product'], + modelID: info['model'] ?? deviceID, + deviceCodeName: info['device'] + )); } } else { printError( diff --git a/packages/flutter_tools/test/android_device_test.dart b/packages/flutter_tools/test/android_device_test.dart index 5978778917..103d7465f6 100644 --- a/packages/flutter_tools/test/android_device_test.dart +++ b/packages/flutter_tools/test/android_device_test.dart @@ -38,5 +38,13 @@ emulator-5612 host features:shell_2 expect(devices, hasLength(3)); expect(devices.first.name, 'localhost:36790'); }); + + testUsingContext('android n', () { + List devices = getAdbDevices(mockAdbOutput: ''' +ZX1G22JJWR device usb:3-3 product:shamu model:Nexus_6 device:shamu features:cmd,shell_v2 +'''); + expect(devices, hasLength(1)); + expect(devices.first.name, 'Nexus 6'); + }); }); }