diff --git a/src/commands/install.rs b/src/commands/install.rs new file mode 100644 index 0000000..5651bce --- /dev/null +++ b/src/commands/install.rs @@ -0,0 +1,30 @@ +use std::path::PathBuf; + +use clap::{ArgAction, Parser}; + +#[derive(Parser, Debug)] +pub struct Install { + /// Should install release APK + #[clap(short, long, default_value="false", action = ArgAction::SetTrue)] + release: bool, +} + +pub fn handle(args: Install) { + let output_dir = PathBuf::from("app/build/outputs/apk"); + + let apk_path = match args.release { + true => output_dir.join("release/app-release.apk"), + false => output_dir.join("debug/app-debug.apk") + }; + + let status = android_cli::invoke_adb_command(&[ + "install", + apk_path.to_str().unwrap() + ]) + .unwrap(); + + match status.success() { + true => println!("Successfully installed APK."), + false => eprintln!("Failed to install APK"), + }; +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 7eb33f3..27387a3 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,2 +1,3 @@ pub mod create; pub mod build; +pub mod install; diff --git a/src/lib.rs b/src/lib.rs index 8c3f54e..fcd1162 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,8 @@ use std::{ use utils::find_gradle; +use crate::utils::find_adb; + const DEFAULT_TEMPLATE_REPO: &str = "https://github.com/SyedAhkam/android-cli-template"; const TEMPLATE_REV: &str = "master"; @@ -50,3 +52,20 @@ pub fn invoke_gradle_command(cmd: &str) -> Result Result> { + let adb_path = find_adb().expect("ERROR: ADB not found on system"); + + let mut run = Command::new(adb_path); + run.args(args); + + println!( + "Invoking ADB: {} {}", + &run.get_program().to_string_lossy(), + &run.get_args() + .map(|arg| arg.to_string_lossy()) + .join(" ") + ); + + Ok(run.status()?) +} diff --git a/src/main.rs b/src/main.rs index 2175652..6de0ed6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,8 @@ struct Cli { #[derive(Subcommand, Debug)] enum SubCommand { Create(commands::create::Create), - Build(commands::build::Build) + Build(commands::build::Build), + Install(commands::install::Install) } fn main() { @@ -27,6 +28,7 @@ fn main() { match args.command { SubCommand::Create(args) => commands::create::handle(args), - SubCommand::Build(args) => commands::build::handle(args) + SubCommand::Build(args) => commands::build::handle(args), + SubCommand::Install(args) => commands::install::handle(args) } } diff --git a/src/utils.rs b/src/utils.rs index 3c7f39e..ccc467e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -18,3 +18,11 @@ pub fn find_gradle() -> Option { None } + +pub fn find_adb() -> Option { + if which("adb").is_ok() { + return Some("adb".to_owned()); + } + + None +}