Skip to content

Commit

Permalink
fix/adb starting (#98)
Browse files Browse the repository at this point in the history
* fix `Adb` crashing when daemon not running

* set version to 0.1.2
  • Loading branch information
bartekpacia authored Jul 6, 2022
1 parent 4fa2f57 commit 9eca5e8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 21 deletions.
4 changes: 4 additions & 0 deletions packages/adb/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.2

- Move `adb start-server` to `Adb.init` method

## 0.1.1

- Ensure that `adbd` is running in `Adb` constructor
Expand Down
5 changes: 3 additions & 2 deletions packages/adb/bin/example.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'package:adb/adb.dart';

void main() {
void main() async {
final adb = Adb();
await adb.init();
const apk = '/Users/bartek/.config/maestro/server.apk';
adb.install(apk);
await adb.install(apk);
}
42 changes: 24 additions & 18 deletions packages/adb/lib/src/adb.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import 'dart:async';
import 'dart:io';
import 'dart:io' as io;

import 'package:adb/adb.dart';
import 'package:adb/src/exceptions.dart';
import 'package:adb/src/extensions.dart';
import 'package:adb/src/internals.dart';

/// Provides Dart interface for common `adb` functionality.
/// Provides Dart interface for common Android Debug Bridge features.
///
/// See also:
/// * https://developer.android.com/studio/command-line/adb
class Adb {
/// Creates [Adb] instance.
///
/// If the daemon is not running, it will be started. This constructor will
/// complete when the daemon started.
/// Creates a new [Adb] instance.
Adb({AdbInternals adbInternals = const AdbInternals()})
: _adbInternals = adbInternals {
: _adbInternals = adbInternals;

/// Initializes this [Adb] instance.
///
/// If the ADB daemon is not running, it will be started.
Future<void> init() async {
while (true) {
final result = Process.runSync('adb start-server', [], runInShell: true);
if (result.stdErr.contains('daemon not running; starting now at')) {
sleep(const Duration(milliseconds: 100));
final result = await io.Process.run(
'adb',
['start-server'],
runInShell: true,
);
if (result.stdErr.contains(AdbDaemonNotRunning.trigger)) {
await Future<void>.delayed(const Duration(milliseconds: 300));
} else {
break;
}
Expand All @@ -35,11 +41,11 @@ class Adb {
/// passing [device].
///
/// Throws if there are no devices attached.
Future<ProcessResult> install(
Future<io.ProcessResult> install(
String path, {
String? device,
}) async {
final result = await Process.run(
final result = await io.Process.run(
'adb',
[
if (device != null) ...[
Expand Down Expand Up @@ -74,11 +80,11 @@ class Adb {
/// passing [device].
///
/// Thorws if there are no devices attached.
Future<ProcessResult> uninstall(
Future<io.ProcessResult> uninstall(
String packageName, {
String? device,
}) async {
final result = await Process.run(
final result = await io.Process.run(
'adb',
[
if (device != null) ...[
Expand Down Expand Up @@ -117,7 +123,7 @@ class Adb {
String? device,
String protocol = 'tcp',
}) async {
final result = await Process.run(
final result = await io.Process.run(
'adb',
[
if (device != null) ...[
Expand Down Expand Up @@ -158,7 +164,7 @@ class Adb {
void Function(String)? onStderr,
Map<String, String> arguments = const {},
}) async {
final process = await Process.start(
final process = await io.Process.start(
'adb',
[
if (device != null) ...[
Expand All @@ -180,12 +186,12 @@ class Adb {
);

final stdoutSub = process.stdout.listen((data) {
final text = systemEncoding.decode(data);
final text = io.systemEncoding.decode(data);
onStdout?.call(text);
});

final stderrSub = process.stderr.listen((data) {
final text = systemEncoding.decode(data);
final text = io.systemEncoding.decode(data);
onStderr?.call(text);
});

Expand Down
9 changes: 9 additions & 0 deletions packages/adb/lib/src/internals.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import 'dart:io';

import 'package:adb/src/exceptions.dart';
import 'package:adb/src/extensions.dart';

/// This class makes it easy to swap out the implementation of the some ADB
/// commands.
class AdbInternals {
/// Creates a new [AdbInternals] instance.
const AdbInternals();

/// Calls `adb devices` and returns its stdout.
Future<String> devices() async {
final result = await Process.run(
'adb',
Expand All @@ -13,6 +18,10 @@ class AdbInternals {
);

if (result.stdErr.isNotEmpty) {
if (result.stdErr.contains(AdbDaemonNotRunning.trigger)) {
throw const AdbDaemonNotRunning();
}

throw Exception(result.stdErr);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/adb/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: adb
description: Simple wrapper for adb.
version: 0.1.1
version: 0.1.2
homepage: https://github.com/leancodepl/maestro

environment:
Expand Down

0 comments on commit 9eca5e8

Please sign in to comment.