Skip to content

Commit

Permalink
Merge pull request #12 from thulasi-ram/fix-work-profiles
Browse files Browse the repository at this point in the history
Fix Work Profiles & Set stage for Dependency Injection
  • Loading branch information
thulasi-ram authored Feb 18, 2024
2 parents 253d63a + ff64d50 commit a7dcbef
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 140 deletions.
138 changes: 85 additions & 53 deletions src-tauri/src/adb_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,33 @@ use std::os::windows::process::CommandExt;

use log::info;

pub trait ADBCommand {
pub trait ADBCommand: Sized {
fn execute(&self) -> Result<String, ADBError>;
fn arg<S: AsRef<str>>(self, arg: S) -> Self;
fn args<I, S>(self, args: I) -> Self
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let mut s1 = self;
for arg in args {
s1 = s1.arg(arg);
}
s1
}

fn arg_prepend<S: AsRef<str>>(self, arg: S) -> Self;
fn args_prepend<I, S>(self, args: I) -> Self
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let mut s1 = self;
for arg in args {
s1 = s1.arg_prepend(arg);
}
s1
}
}

#[derive(Debug, thiserror::Error)]
Expand All @@ -15,65 +40,50 @@ pub enum ADBError {
Unknown(String),
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ADBRaw {
adb_path: String,
sub_commands: Vec<String>,
cmd_str: String,
argsv: Vec<String>,
}

impl ADBRaw {
pub fn new(adb_path: String, value: Vec<String>) -> Self {
pub fn new(adb_path: String) -> Self {
let mut cmd_str = "adb";
if !adb_path.is_empty() {
cmd_str = adb_path.as_str();
}
Self {
adb_path: adb_path,
sub_commands: value,
cmd_str: cmd_str.to_string(),
argsv: vec![],
}
}
}

#[derive(Debug, Clone)]
pub struct ADBShell {
adb_path: String,
sub_commands: Vec<String>,
device_id: String,
}

impl ADBShell {
pub fn new(adb_path: String) -> Self {
Self {
adb_path: adb_path,
sub_commands: vec![],
device_id: String::from(""),
}
}
impl ADBCommand for ADBRaw {
fn arg<S: AsRef<str>>(self, arg: S) -> Self {
// https://users.rust-lang.org/t/best-way-to-clone-and-append-a-single-element/68675/2

pub fn for_device(mut self, device_id: String) -> Self {
self.device_id = device_id;
return self;
let mut s1 = self;
s1.argsv.push(arg.as_ref().to_owned());
return s1;
}

pub fn with_commands(mut self, sub_commands: &[&str]) -> Self {
self.sub_commands = sub_commands.iter().map(|s| String::from(*s)).collect();
return self;
fn arg_prepend<S: AsRef<str>>(self, arg: S) -> Self {
let mut s1 = self;
s1.argsv.insert(0, arg.as_ref().to_owned());
return s1;
}
}

impl ADBCommand for ADBRaw {
fn execute(&self) -> Result<String, ADBError> {
let mut command = Command::new(self.cmd_str.to_owned());
command.args(self.argsv.to_vec());

let mut cmd_str = "adb";
if !self.adb_path.is_empty() {
cmd_str = self.adb_path.as_str();
}

let mut command = Command::new(cmd_str);

// https://stackoverflow.com/a/38186733/6323666
let args = self
.sub_commands
.iter()
.map(|s| s.as_str())
.collect::<Vec<&str>>();
command.args(args);
// let args = self
// .sub_commands
// .iter()
// .map(|s| s.as_str())
// .collect::<Vec<&str>>();
// command.args(args);

info!("command {:?}", command);

Expand Down Expand Up @@ -107,17 +117,39 @@ impl ADBCommand for ADBRaw {
}
}

#[derive(Debug, Clone)]
pub struct ADBShell {
adb_raw: ADBRaw,
}

impl ADBShell {
pub fn new(adb_path: String) -> Self {
let adbr = ADBRaw::new(adb_path).arg("shell");
Self { adb_raw: adbr }
}
}

impl ADBCommand for ADBShell {
fn execute(&self) -> Result<String, ADBError> {
let mut sub_commands_with_shell: Vec<String> = vec![String::from("shell")];
fn arg<S: AsRef<str>>(self, arg: S) -> Self {
let mut s1 = self;
s1.adb_raw = s1.adb_raw.arg(arg.as_ref());
return s1;
}

if !String::is_empty(&self.device_id.to_owned()) {
sub_commands_with_shell.insert(0, String::from("-s"));
sub_commands_with_shell.insert(1, self.device_id.to_owned());
}
fn arg_prepend<S: AsRef<str>>(self, arg: S) -> Self {
let mut s1 = self;
s1.adb_raw = s1.adb_raw.arg_prepend(arg.as_ref());
return s1;
}

sub_commands_with_shell.extend(self.sub_commands.to_owned());
let adb_raw = ADBRaw::new(self.adb_path.to_owned(), sub_commands_with_shell);
return adb_raw.execute();
fn execute(&self) -> Result<String, ADBError> {
return self.adb_raw.execute();
}
}

pub fn for_device<'a, T: ADBCommand + Clone>(abdc: &'a T, device_id: String) -> T {
// ideally its -s <device_id> but we send in reverse so prepend works properly
return abdc
.clone()
.args_prepend(vec!["-s", &device_id].into_iter().rev());
}
11 changes: 7 additions & 4 deletions src-tauri/src/devices.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::adb_cmd::{ADBCommand, ADBRaw, ADBShell};
use crate::adb_cmd::{self, ADBCommand, ADBRaw, ADBShell};
use anyhow::{anyhow, Error, Result};
use core::result::Result::Ok;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -61,7 +61,9 @@ pub struct ADBTerminalImpl {

impl ADBTerminalImpl {
pub fn list_devices(&self) -> Result<Vec<Device>> {
let res = ADBRaw::new(self.adb_path.to_owned(), vec![String::from("devices")]).execute();
let res = ADBRaw::new(self.adb_path.to_owned())
.arg("devices")
.execute();
match res {
Err(e) => {
return Err(e.into());
Expand Down Expand Up @@ -101,8 +103,9 @@ impl ADBTerminalImpl {
return Err(e);
}
Ok(d) => {
let shell_cmd: ADBShell = ADBShell::new(self.adb_path.to_owned()).for_device(d.id.to_owned());
let res = shell_cmd.with_commands(&["getprop"]).execute();
let shell_cmd: ADBShell =
adb_cmd::for_device(&ADBShell::new(self.adb_path.to_owned()), d.id.to_owned());
let res = shell_cmd.arg("getprop").execute();
match res {
Err(e) => {
return Err(e.into());
Expand Down
Loading

0 comments on commit a7dcbef

Please sign in to comment.