diff --git a/packages/adb/CHANGELOG.md b/packages/adb/CHANGELOG.md index 81f9a6f4e..1b705449f 100644 --- a/packages/adb/CHANGELOG.md +++ b/packages/adb/CHANGELOG.md @@ -1,6 +1,7 @@ -## Unreleased +## 0.4.0 -- Bump minimum Dart SDK to version 3.2.0 (#1917) +- Add `getForwardedPorts` method to `Adb` (#2332) +- Bump minimum Dart SDK to version 3.3.0-0 (#1917) ## 0.3.0 diff --git a/packages/adb/lib/src/adb.dart b/packages/adb/lib/src/adb.dart index 66b7c4917..4bdbc3542 100644 --- a/packages/adb/lib/src/adb.dart +++ b/packages/adb/lib/src/adb.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:convert'; import 'dart:io' as io; import 'package:adb/adb.dart'; @@ -161,6 +162,25 @@ class Adb { }; } + /// Returns a parsed result of `adb forward --list` command. + Future getForwardedPorts() async { + await _adbInternals.ensureServerRunning(); + + final output = await io.Process.run( + 'adb', + ['forward', '--list'], + runInShell: true, + ); + + if (output.stdErr.isNotEmpty) { + _handleAdbExceptions(output.stdErr); + + throw Exception(output.stdErr); + } + + return AdbForwardList.parse(output.stdout as String); + } + /// Runs instrumentation test specified by [packageName] and [intentClass] on /// the attached device. /// @@ -243,3 +263,29 @@ class Adb { } } } + +/// Represents a parsed result of adb forward --list command. +extension type AdbForwardList._(Map> map) { + /// Parses the output of `adb forward --list` command into a map. + AdbForwardList.parse(String adbForwardOutput) : map = {} { + final nonEmptyLines = const LineSplitter() + .convert(adbForwardOutput) + .where((line) => line.isNotEmpty); + for (final line in nonEmptyLines) { + final parts = line.split(' '); + final device = parts[0]; + final hostPort = int.parse(parts[1].split(':')[1]); + final devicePort = int.parse(parts[2].split(':')[1]); + if (map[device] case final deviceMap?) { + deviceMap[hostPort] = devicePort; + } else { + map[device] = {hostPort: devicePort}; + } + } + } + + /// Returns port mapping for a device with the specified id. + Map getMappedPortsForDevice(String deviceId) { + return map[deviceId] ?? {}; + } +} diff --git a/packages/adb/pubspec.yaml b/packages/adb/pubspec.yaml index e1176356c..3c803ccef 100644 --- a/packages/adb/pubspec.yaml +++ b/packages/adb/pubspec.yaml @@ -1,11 +1,11 @@ name: adb description: Simple Dart wrapper around Android Debug Bridge. -version: 0.3.0 +version: 0.4.0 repository: https://github.com/leancodepl/patrol/tree/master/packages/adb issue_tracker: https://github.com/leancodepl/patrol/issues?q=is%3Aopen+is%3Aissue+label%3A%22package%3A+adb%22 environment: - sdk: '>=3.2.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dev_dependencies: custom_lint: ^0.6.4 diff --git a/packages/adb/test/adb_forward_list_test.dart b/packages/adb/test/adb_forward_list_test.dart new file mode 100644 index 000000000..dbb468f51 --- /dev/null +++ b/packages/adb/test/adb_forward_list_test.dart @@ -0,0 +1,39 @@ +import 'package:adb/adb.dart'; +import 'package:test/test.dart'; + +void main() { + group( + 'AdbForwardList', + () { + test('returns an empty map if adb forward --list output is empty', () { + const output = ''' + +'''; + expect(AdbForwardList.parse(output), >{}); + }); + + test('parses adb forward --list output correctly', () { + const output = ''' +emulator-5554 tcp:60000 tcp:60001 +emulator-5554 tcp:61234 tcp:61235 +emulator-5556 tcp:61341 tcp:62562 + +'''; + + final result = AdbForwardList.parse(output); + expect( + result, + { + 'emulator-5554': { + 60000: 60001, + 61234: 61235, + }, + 'emulator-5556': { + 61341: 62562, + }, + }, + ); + }); + }, + ); +} diff --git a/pubspec.yaml b/pubspec.yaml index c0d52a8e2..63876a955 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: location_workspace environment: - sdk: '>=3.3.0-0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dev_dependencies: melos: ^3.1.1