From 6a66b13bc46b26e451033a8ecef86c4b4cedb5f7 Mon Sep 17 00:00:00 2001 From: Jakub Lange Date: Mon, 9 Sep 2024 20:13:37 +0200 Subject: [PATCH 1/3] Add adb forward list to adb wrapper --- packages/adb/lib/src/adb.dart | 46 ++++++++++++++++++++ packages/adb/pubspec.yaml | 2 +- packages/adb/test/adb_forward_list_test.dart | 39 +++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 packages/adb/test/adb_forward_list_test.dart 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..a82abde02 100644 --- a/packages/adb/pubspec.yaml +++ b/packages/adb/pubspec.yaml @@ -5,7 +5,7 @@ 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-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, + }, + }, + ); + }); + }, + ); +} From 20bc8cd0cd2d030380ddeb2253bec688f7ce660f Mon Sep 17 00:00:00 2001 From: Jakub Lange Date: Mon, 9 Sep 2024 20:18:39 +0200 Subject: [PATCH 2/3] Bump version, update changelog --- packages/adb/CHANGELOG.md | 3 ++- packages/adb/pubspec.yaml | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/adb/CHANGELOG.md b/packages/adb/CHANGELOG.md index 81f9a6f4e..93d9bd5eb 100644 --- a/packages/adb/CHANGELOG.md +++ b/packages/adb/CHANGELOG.md @@ -1,6 +1,7 @@ ## Unreleased -- 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/pubspec.yaml b/packages/adb/pubspec.yaml index a82abde02..33cbc418a 100644 --- a/packages/adb/pubspec.yaml +++ b/packages/adb/pubspec.yaml @@ -1,6 +1,6 @@ name: adb description: Simple Dart wrapper around Android Debug Bridge. -version: 0.3.0 +version: 0.4.0-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 From c52af2dca15b470fbbad93ea7002fe80b1d5af77 Mon Sep 17 00:00:00 2001 From: Jakub Lange Date: Wed, 11 Sep 2024 14:23:10 +0200 Subject: [PATCH 3/3] Update package versions --- packages/adb/CHANGELOG.md | 2 +- packages/adb/pubspec.yaml | 4 ++-- pubspec.yaml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/adb/CHANGELOG.md b/packages/adb/CHANGELOG.md index 93d9bd5eb..1b705449f 100644 --- a/packages/adb/CHANGELOG.md +++ b/packages/adb/CHANGELOG.md @@ -1,4 +1,4 @@ -## Unreleased +## 0.4.0 - Add `getForwardedPorts` method to `Adb` (#2332) - Bump minimum Dart SDK to version 3.3.0-0 (#1917) diff --git a/packages/adb/pubspec.yaml b/packages/adb/pubspec.yaml index 33cbc418a..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.4.0-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.3.0-0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dev_dependencies: custom_lint: ^0.6.4 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