diff --git a/src/commands/build.rs b/src/commands/build.rs index a2bc8da..319680a 100644 --- a/src/commands/build.rs +++ b/src/commands/build.rs @@ -1,4 +1,4 @@ -use anyhow::{Context, Result}; +use anyhow::Result; use clap::{ArgAction, Parser}; #[derive(Parser, Debug)] @@ -9,15 +9,7 @@ pub struct Build { } pub fn handle(args: Build) -> Result<()> { - // Decide gradle subcommand to use - let cmd = match args.release { - true => "assembleRelease", - false => "assembleDebug", - }; - - // Invoke gradle as child process - let status = - android_cli::invoke_gradle_command(cmd).context("failed to invoke gradle command")?; + let status = android_cli::trigger_build(args.release)?; match status.success() { true => println!("Success!"), diff --git a/src/commands/install.rs b/src/commands/install.rs index b76d9a7..2943dd0 100644 --- a/src/commands/install.rs +++ b/src/commands/install.rs @@ -1,7 +1,5 @@ +use anyhow::Result; use clap::{ArgAction, Parser}; -use anyhow::{Result, Context}; - -use std::path::PathBuf; #[derive(Parser, Debug)] pub struct Install { @@ -11,18 +9,7 @@ pub struct Install { } pub fn handle(args: Install) -> Result<()> { - 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() - ]) - .context("failed to run adb command")?; + let status = android_cli::install_apk(args.release)?; match status.success() { true => println!("Successfully installed APK."), diff --git a/src/commands/run.rs b/src/commands/run.rs index 7aed457..3e55cd9 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -1,4 +1,4 @@ -use anyhow::{bail, Context, Result, anyhow}; +use anyhow::{anyhow, bail, Context, Result}; use clap::{ArgAction, Parser}; #[derive(Parser, Debug)] @@ -9,35 +9,27 @@ pub struct Run { } pub fn handle(args: Run) -> Result<()> { - // Decide gradle subcommand to use - let build_cmd = match args.release { - true => "assembleRelease", - false => "assembleDebug", - }; - // Trigger a build - let build_status = - android_cli::invoke_gradle_command(build_cmd).context("failed to invoke gradle command")?; - + let build_status = android_cli::trigger_build(args.release)?; if !build_status.success() { bail!("failed to build project"); } + // Request an install to device + let install_status = android_cli::install_apk(args.release)?; + if !install_status.success() { + bail!("failed to install APK"); + } + // Fetch and deserialize .android - let dot_android = android_cli::get_dot_android().ok_or_else(|| anyhow!(".android not found, can't launch activity"))?; + let dot_android = android_cli::get_dot_android() + .ok_or_else(|| anyhow!(".android not found, can't launch activity"))?; // Try to launch MainActivity using ADB - let run_status = android_cli::invoke_adb_command(&[ - "shell", - "am", - "start", - "-n", - &format!("{}/.{}", dot_android.package_id, dot_android.main_activity_name) - ]) - .context("failed to invoke adb command")?; - - if !run_status.success() { - bail!("failed to run the APK"); + let launch_status = + android_cli::launch_activity(dot_android.package_id, dot_android.main_activity_name)?; + if !launch_status.success() { + bail!("failed to launch activity"); } Ok(()) diff --git a/src/lib.rs b/src/lib.rs index 196fd97..66666c1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,9 @@ mod utils; +use anyhow::{Context, Result}; use guidon::{GitOptions, Guidon, TryNew}; use itertools::Itertools; use serde::{Deserialize, Serialize}; -use anyhow::{Result, Context}; use std::{ collections::BTreeMap, @@ -25,7 +25,7 @@ const DEFAULT_MAIN_ACTIVITY: &str = "MainActivity"; pub struct DotAndroid { pub package_id: String, pub gen_at_version: String, - pub main_activity_name: String + pub main_activity_name: String, } pub fn copy_template(dest: &Path, vars: BTreeMap) -> Result<()> { @@ -87,7 +87,7 @@ pub fn create_dot_android(dest: &Path, package_id: String) -> Result<()> { let dot_android = DotAndroid { package_id, gen_at_version: VERSION.to_owned(), - main_activity_name: DEFAULT_MAIN_ACTIVITY.to_owned() + main_activity_name: DEFAULT_MAIN_ACTIVITY.to_owned(), }; // Serialize into Ron @@ -111,3 +111,36 @@ pub fn get_dot_android() -> Option { None } + +pub fn install_apk(is_release: bool) -> Result { + let output_dir = PathBuf::from("app/build/outputs/apk"); + + let apk_path = match is_release { + true => output_dir.join("release/app-release.apk"), + false => output_dir.join("debug/app-debug.apk"), + }; + + Ok(invoke_adb_command(&["install", apk_path.to_str().unwrap()]) + .context("failed to run adb command")?) +} + +pub fn trigger_build(is_release: bool) -> Result { + // Decide gradle subcommand to use + let cmd = match is_release { + true => "assembleRelease", + false => "assembleDebug", + }; + + Ok(invoke_gradle_command(cmd).context("failed to invoke gradle command")?) +} + +pub fn launch_activity(package_id: String, activity_name: String) -> Result { + Ok(invoke_adb_command(&[ + "shell", + "am", + "start", + "-n", + &format!("{}/.{}", package_id, activity_name), + ]) + .context("failed to invoke adb command")?) +}