Skip to content

Commit

Permalink
Merge pull request #2332 from gogolon/feature/add-adb-forward-list-to…
Browse files Browse the repository at this point in the history
…-adb-wrapper

Add `adb forward --list` to adb wrapper
  • Loading branch information
gogolon authored Sep 11, 2024
2 parents 36517c0 + c52af2d commit 7695660
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 deletions.
5 changes: 3 additions & 2 deletions packages/adb/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
46 changes: 46 additions & 0 deletions packages/adb/lib/src/adb.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io' as io;

import 'package:adb/adb.dart';
Expand Down Expand Up @@ -161,6 +162,25 @@ class Adb {
};
}

/// Returns a parsed result of `adb forward --list` command.
Future<AdbForwardList> 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.
///
Expand Down Expand Up @@ -243,3 +263,29 @@ class Adb {
}
}
}

/// Represents a parsed result of adb forward --list command.
extension type AdbForwardList._(Map<String, Map<int, int>> 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<int, int> getMappedPortsForDevice(String deviceId) {
return map[deviceId] ?? {};
}
}
4 changes: 2 additions & 2 deletions packages/adb/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
39 changes: 39 additions & 0 deletions packages/adb/test/adb_forward_list_test.dart
Original file line number Diff line number Diff line change
@@ -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), <String, Map<int, int>>{});
});

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,
},
},
);
});
},
);
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7695660

Please sign in to comment.