Skip to content

Commit 37d5d22

Browse files
committed
feat: impl the install command
Allows the user to run `android install` and it will automatically install the apk to the currently connected device.
1 parent 61f73e5 commit 37d5d22

5 files changed

Lines changed: 62 additions & 2 deletions

File tree

src/commands/install.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use std::path::PathBuf;
2+
3+
use clap::{ArgAction, Parser};
4+
5+
#[derive(Parser, Debug)]
6+
pub struct Install {
7+
/// Should install release APK
8+
#[clap(short, long, default_value="false", action = ArgAction::SetTrue)]
9+
release: bool,
10+
}
11+
12+
pub fn handle(args: Install) {
13+
let output_dir = PathBuf::from("app/build/outputs/apk");
14+
15+
let apk_path = match args.release {
16+
true => output_dir.join("release/app-release.apk"),
17+
false => output_dir.join("debug/app-debug.apk")
18+
};
19+
20+
let status = android_cli::invoke_adb_command(&[
21+
"install",
22+
apk_path.to_str().unwrap()
23+
])
24+
.unwrap();
25+
26+
match status.success() {
27+
true => println!("Successfully installed APK."),
28+
false => eprintln!("Failed to install APK"),
29+
};
30+
}

src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod create;
22
pub mod build;
3+
pub mod install;

src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use std::{
1111

1212
use utils::find_gradle;
1313

14+
use crate::utils::find_adb;
15+
1416
const DEFAULT_TEMPLATE_REPO: &str = "https://github.com/SyedAhkam/android-cli-template";
1517
const TEMPLATE_REV: &str = "master";
1618

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

5153
Ok(run.status()?)
5254
}
55+
56+
pub fn invoke_adb_command(args: &[&str]) -> Result<ExitStatus, Box<dyn std::error::Error>> {
57+
let adb_path = find_adb().expect("ERROR: ADB not found on system");
58+
59+
let mut run = Command::new(adb_path);
60+
run.args(args);
61+
62+
println!(
63+
"Invoking ADB: {} {}",
64+
&run.get_program().to_string_lossy(),
65+
&run.get_args()
66+
.map(|arg| arg.to_string_lossy())
67+
.join(" ")
68+
);
69+
70+
Ok(run.status()?)
71+
}

src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ struct Cli {
1616
#[derive(Subcommand, Debug)]
1717
enum SubCommand {
1818
Create(commands::create::Create),
19-
Build(commands::build::Build)
19+
Build(commands::build::Build),
20+
Install(commands::install::Install)
2021
}
2122

2223
fn main() {
@@ -27,6 +28,7 @@ fn main() {
2728

2829
match args.command {
2930
SubCommand::Create(args) => commands::create::handle(args),
30-
SubCommand::Build(args) => commands::build::handle(args)
31+
SubCommand::Build(args) => commands::build::handle(args),
32+
SubCommand::Install(args) => commands::install::handle(args)
3133
}
3234
}

src/utils.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ pub fn find_gradle() -> Option<String> {
1818

1919
None
2020
}
21+
22+
pub fn find_adb() -> Option<String> {
23+
if which("adb").is_ok() {
24+
return Some("adb".to_owned());
25+
}
26+
27+
None
28+
}

0 commit comments

Comments
 (0)