Skip to content

Commit

Permalink
feat: impl the install command
Browse files Browse the repository at this point in the history
Allows the user to run `android install` and it will automatically install the apk to the currently connected device.
  • Loading branch information
SyedAhkam committed Aug 11, 2023
1 parent 61f73e5 commit 37d5d22
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
@@ -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"),
};
}
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod create;
pub mod build;
pub mod install;
19 changes: 19 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -50,3 +52,20 @@ pub fn invoke_gradle_command(cmd: &str) -> Result<ExitStatus, Box<dyn std::error

Ok(run.status()?)
}

pub fn invoke_adb_command(args: &[&str]) -> Result<ExitStatus, Box<dyn std::error::Error>> {
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()?)
}
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
}
8 changes: 8 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ pub fn find_gradle() -> Option<String> {

None
}

pub fn find_adb() -> Option<String> {
if which("adb").is_ok() {
return Some("adb".to_owned());
}

None
}

0 comments on commit 37d5d22

Please sign in to comment.