Skip to content

Commit

Permalink
Allow to set a custom path for adb
Browse files Browse the repository at this point in the history
Use the environment variable ADB to use a custom adb executable.
  • Loading branch information
rom1v committed Dec 20, 2019
1 parent 7899961 commit 0817148
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ To stop a client:
-n com.genymobile.gnirehtet/.GnirehtetActivity


## Environment variables

`ADB` defines a custom path to the `adb` executable:

```bash
ADB=/path/to/my/adb ./gnirehtet run
```


## Why _gnirehtet_?

rev <<< tethering
Expand Down
7 changes: 6 additions & 1 deletion relay-java/src/main/java/com/genymobile/gnirehtet/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ private Main() {
// not instantiable
}

private static String getAdbPath() {
String adb = System.getenv("ADB");
return adb != null ? adb : "adb";
}

enum Command {
INSTALL("install", CommandLineArguments.PARAM_SERIAL) {
@Override
Expand Down Expand Up @@ -317,7 +322,7 @@ private static void execAdb(String serial, String... adbArgs) throws Interrupted

private static List<String> createAdbCommand(String serial, String... adbArgs) {
List<String> command = new ArrayList<>();
command.add("adb");
command.add(getAdbPath());
if (serial != null) {
command.add("-s");
command.add(serial);
Expand Down
27 changes: 19 additions & 8 deletions relay-rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ use std::time::Duration;
const TAG: &str = "Main";
const REQUIRED_APK_VERSION_CODE: &str = "7";

#[inline]
fn get_adb_path() -> String {
if let Some(env_adb) = std::env::var_os("ADB") {
env_adb.into_string().expect("invalid ADB value")
} else {
"adb".to_string()
}
}

const COMMANDS: &[&dyn Command] = &[
&InstallCommand,
&UninstallCommand,
Expand Down Expand Up @@ -498,18 +507,19 @@ fn exec_adb<S: Into<String>>(
args: Vec<S>,
) -> Result<(), CommandExecutionError> {
let adb_args = create_adb_args(serial, args);
debug!(target: TAG, "Execute: adb {:?}", adb_args);
match process::Command::new("adb").args(&adb_args[..]).status() {
let adb = get_adb_path();
debug!(target: TAG, "Execute: {:?} {:?}", adb, adb_args);
match process::Command::new(&adb).args(&adb_args[..]).status() {
Ok(exit_status) => {
if exit_status.success() {
Ok(())
} else {
let cmd = Cmd::new("adb", adb_args);
let cmd = Cmd::new(adb, adb_args);
Err(ProcessStatusError::new(cmd, exit_status).into())
}
}
Err(err) => {
let cmd = Cmd::new("adb", adb_args);
let cmd = Cmd::new(adb, adb_args);
Err(ProcessIoError::new(cmd, err).into())
}
}
Expand All @@ -521,8 +531,9 @@ fn must_install_client(serial: Option<&str>) -> Result<bool, CommandExecutionErr
serial,
vec!["shell", "dumpsys", "package", "com.genymobile.gnirehtet"],
);
debug!(target: TAG, "Execute: adb {:?}", args);
match process::Command::new("adb").args(&args[..]).output() {
let adb = get_adb_path();
debug!(target: TAG, "Execute: {:?} {:?}", adb, args);
match process::Command::new(&adb).args(&args[..]).output() {
Ok(output) => {
if output.status.success() {
// the "regex" crate makes the binary far bigger, so just parse the versionCode
Expand All @@ -543,12 +554,12 @@ fn must_install_client(serial: Option<&str>) -> Result<bool, CommandExecutionErr
Ok(true)
}
} else {
let cmd = Cmd::new("adb", args);
let cmd = Cmd::new(adb, args);
Err(ProcessStatusError::new(cmd, output.status).into())
}
}
Err(err) => {
let cmd = Cmd::new("adb", args);
let cmd = Cmd::new(adb, args);
Err(ProcessIoError::new(cmd, err).into())
}
}
Expand Down

0 comments on commit 0817148

Please sign in to comment.