|
| 1 | +use std::{path::PathBuf, process::Command}; |
| 2 | + |
| 3 | +use clap::Parser; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + cli::{CommandExt as _, derive_adb_path}, |
| 7 | + shell::{Shell, Verbosity}, |
| 8 | +}; |
| 9 | + |
| 10 | +#[derive(Debug, Parser, Clone)] |
| 11 | +struct RunnerArgs { |
| 12 | + #[arg(long, env = "CARGO_NDK_ADB_SERIAL")] |
| 13 | + /// "Serial number" of the device to use for testing (e.g. "emulator-5554" or "0123456789ABCDEF") |
| 14 | + /// |
| 15 | + /// You can find the serial number of your device by running `adb devices`. |
| 16 | + /// |
| 17 | + /// If not set, the first available device will be used. |
| 18 | + adb_serial: Option<String>, |
| 19 | + |
| 20 | + #[arg(short)] |
| 21 | + /// Enable verbose output |
| 22 | + verbose: bool, |
| 23 | + |
| 24 | + #[arg(short)] |
| 25 | + /// Enable quiet output (no output except errors) |
| 26 | + quiet: bool, |
| 27 | + |
| 28 | + /// Path to the binary to run on the device |
| 29 | + executable: PathBuf, |
| 30 | + |
| 31 | + #[arg(allow_hyphen_values = true)] |
| 32 | + /// Arguments to be run |
| 33 | + runner_args: Vec<String>, |
| 34 | +} |
| 35 | + |
| 36 | +pub fn run(args: Vec<String>) -> anyhow::Result<()> { |
| 37 | + let mut shell = Shell::new(); |
| 38 | + |
| 39 | + let args = RunnerArgs::try_parse_from(args).unwrap_or_else(|e| { |
| 40 | + shell.error(e).unwrap(); |
| 41 | + std::process::exit(2); |
| 42 | + }); |
| 43 | + |
| 44 | + let adb_serial = args.adb_serial.as_deref(); |
| 45 | + let verbosity = if args.verbose { |
| 46 | + Verbosity::Verbose |
| 47 | + } else if args.quiet { |
| 48 | + Verbosity::Quiet |
| 49 | + } else { |
| 50 | + Verbosity::Normal |
| 51 | + }; |
| 52 | + |
| 53 | + shell.set_verbosity(verbosity); |
| 54 | + |
| 55 | + // Get adb path |
| 56 | + let adb_path = match derive_adb_path(&mut shell) { |
| 57 | + Ok(path) => path, |
| 58 | + Err(e) => { |
| 59 | + shell.error(e)?; |
| 60 | + std::process::exit(1); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + // Push binary to device |
| 65 | + let device_path = format!( |
| 66 | + "/data/local/tmp/{}", |
| 67 | + args.executable.file_name().unwrap().to_string_lossy() |
| 68 | + ); |
| 69 | + |
| 70 | + // Ugly but works |
| 71 | + shell.verbose(|shell| { |
| 72 | + shell.status_header("Pushing")?; |
| 73 | + shell.reset_err()?; |
| 74 | + shell |
| 75 | + .err() |
| 76 | + .write_fmt(format_args!("binary to device: {device_path}\r"))?; |
| 77 | + shell.set_needs_clear(true); |
| 78 | + Ok(()) |
| 79 | + })?; |
| 80 | + |
| 81 | + let push_status = Command::new(&adb_path) |
| 82 | + .with_serial(adb_serial.as_deref()) |
| 83 | + .arg("push") |
| 84 | + .arg(&args.executable) |
| 85 | + .arg(&device_path) |
| 86 | + .output()?; |
| 87 | + |
| 88 | + if !push_status.status.success() { |
| 89 | + shell.error("Failed to push test binary to device")?; |
| 90 | + eprintln!("{}", std::str::from_utf8(&push_status.stderr)?.trim()); |
| 91 | + shell.note("If multiple devices, use --adb-serial to specify one.")?; |
| 92 | + shell.note("Run `adb devices` to see connected devices.")?; |
| 93 | + std::process::exit(push_status.status.code().unwrap_or(1)); |
| 94 | + } |
| 95 | + |
| 96 | + shell.verbose(|shell| shell.status("Pushing", format!("binary to device ({device_path})")))?; |
| 97 | + |
| 98 | + // Make binary executable |
| 99 | + let chmod_status = Command::new(&adb_path) |
| 100 | + .with_serial(adb_serial.as_deref()) |
| 101 | + .arg("shell") |
| 102 | + .arg("chmod") |
| 103 | + .arg("755") |
| 104 | + .arg(&device_path) |
| 105 | + .status()?; |
| 106 | + |
| 107 | + if !chmod_status.success() { |
| 108 | + shell.error("Failed to make binary executable")?; |
| 109 | + std::process::exit(chmod_status.code().unwrap_or(1)); |
| 110 | + } |
| 111 | + |
| 112 | + shell.reset_err()?; |
| 113 | + |
| 114 | + let verbosity_arg = match verbosity { |
| 115 | + Verbosity::Quiet => "-q", |
| 116 | + _ => "", |
| 117 | + }; |
| 118 | + |
| 119 | + let run_status = Command::new(&adb_path) |
| 120 | + .with_serial(adb_serial.as_deref()) |
| 121 | + .arg("shell") |
| 122 | + .arg(&device_path) |
| 123 | + .arg(verbosity_arg) |
| 124 | + .args(&args.runner_args) |
| 125 | + .status()?; |
| 126 | + |
| 127 | + // Clean up the binary from device |
| 128 | + let _ = Command::new(&adb_path) |
| 129 | + .with_serial(adb_serial.as_deref()) |
| 130 | + .arg("shell") |
| 131 | + .arg("rm") |
| 132 | + .arg(&device_path) |
| 133 | + .status(); |
| 134 | + |
| 135 | + std::process::exit(run_status.code().unwrap_or(1)) |
| 136 | +} |
0 commit comments