Skip to content

Commit

Permalink
feat: impl the run command
Browse files Browse the repository at this point in the history
Allows you to build the project and launch the main activity
  • Loading branch information
SyedAhkam committed Aug 12, 2023
1 parent 6f295ce commit 994559e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/commands/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn post_create(args: Create) -> Result<()> {
let dest = args.dest.clone().unwrap();

android_cli::create_local_properties_file(&dest, &args.sdk_path.unwrap())?;
android_cli::create_dot_file(&dest, args.package_id.unwrap())?;
android_cli::create_dot_android(&dest, args.package_id.unwrap())?;

Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod create;
pub mod build;
pub mod install;
pub mod run;
44 changes: 44 additions & 0 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use anyhow::{bail, Context, Result, anyhow};
use clap::{ArgAction, Parser};

#[derive(Parser, Debug)]
pub struct Run {
/// Should build in release mode
#[clap(short, long, default_value="false", action = ArgAction::SetTrue)]
release: bool,
}

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")?;

if !build_status.success() {
bail!("failed to build project");
}

// Fetch and deserialize .android
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");
}

Ok(())
}
15 changes: 13 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ const DEFAULT_TEMPLATE_REPO: &str = "https://github.com/SyedAhkam/android-cli-te
const TEMPLATE_REV: &str = "master";

const DOTFILE_COMMENT: &str = "// DO NOT MODIFY; Generated by Android CLI for internal usage.\n";
const DEFAULT_MAIN_ACTIVITY: &str = "MainActivity";

#[derive(Debug, Serialize, Deserialize)]
struct DotAndroid {
pub struct DotAndroid {
pub package_id: String,
pub gen_at_version: String,
pub main_activity_name: String
}

pub fn copy_template(dest: &Path, vars: BTreeMap<String, String>) -> Result<()> {
Expand Down Expand Up @@ -80,11 +82,12 @@ pub fn invoke_adb_command(args: &[&str]) -> Result<ExitStatus> {
Ok(run.status()?)
}

pub fn create_dot_file(dest: &Path, package_id: String) -> Result<()> {
pub fn create_dot_android(dest: &Path, package_id: String) -> Result<()> {
// Construct the structure
let dot_android = DotAndroid {
package_id,
gen_at_version: VERSION.to_owned(),
main_activity_name: DEFAULT_MAIN_ACTIVITY.to_owned()
};

// Serialize into Ron
Expand All @@ -100,3 +103,11 @@ pub fn create_dot_file(dest: &Path, package_id: String) -> Result<()> {

Ok(())
}

pub fn get_dot_android() -> Option<DotAndroid> {
if let Ok(contents) = std::fs::read_to_string(".android") {
return ron::from_str::<DotAndroid>(&contents).ok();
};

None
}
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ struct Cli {
enum SubCommand {
Create(commands::create::Create),
Build(commands::build::Build),
Install(commands::install::Install)
Install(commands::install::Install),
Run(commands::run::Run)
}

fn main() {
Expand All @@ -29,7 +30,8 @@ fn main() {
let result = match args.command {
SubCommand::Create(args) => commands::create::handle(args),
SubCommand::Build(args) => commands::build::handle(args),
SubCommand::Install(args) => commands::install::handle(args)
SubCommand::Install(args) => commands::install::handle(args),
SubCommand::Run(args) => commands::run::handle(args)
};

if result.is_err() {
Expand Down

0 comments on commit 994559e

Please sign in to comment.