Skip to content

Commit

Permalink
feat: install apk first while running + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
SyedAhkam committed Aug 12, 2023
1 parent 994559e commit 8455904
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 50 deletions.
12 changes: 2 additions & 10 deletions src/commands/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{Context, Result};
use anyhow::Result;
use clap::{ArgAction, Parser};

#[derive(Parser, Debug)]
Expand All @@ -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!"),
Expand Down
17 changes: 2 additions & 15 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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."),
Expand Down
36 changes: 14 additions & 22 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{bail, Context, Result, anyhow};
use anyhow::{anyhow, bail, Context, Result};
use clap::{ArgAction, Parser};

#[derive(Parser, Debug)]
Expand All @@ -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(())
Expand Down
39 changes: 36 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<String, String>) -> Result<()> {
Expand Down Expand Up @@ -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
Expand All @@ -111,3 +111,36 @@ pub fn get_dot_android() -> Option<DotAndroid> {

None
}

pub fn install_apk(is_release: bool) -> Result<ExitStatus> {
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<ExitStatus> {
// 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<ExitStatus> {
Ok(invoke_adb_command(&[
"shell",
"am",
"start",
"-n",
&format!("{}/.{}", package_id, activity_name),
])
.context("failed to invoke adb command")?)
}

0 comments on commit 8455904

Please sign in to comment.