diff --git a/Cargo.lock b/Cargo.lock index e1bafa8..9aea983 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -51,6 +51,7 @@ dependencies = [ "dialoguer", "env_logger", "guidon", + "itertools", "which", ] @@ -571,6 +572,15 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "itertools" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.6" diff --git a/Cargo.toml b/Cargo.toml index 88176c9..41695b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,4 @@ guidon = "0.4.1" env_logger = "0.10.0" dialoguer = "0.10.4" which = "4.4.0" +itertools = "0.11.0" diff --git a/src/commands/build.rs b/src/commands/build.rs index 24feab5..c9a9069 100644 --- a/src/commands/build.rs +++ b/src/commands/build.rs @@ -1,37 +1,24 @@ -use std::process::Command; - -use clap::{ - Parser, - ArgAction -}; - -use crate::utils::find_gradle; +use clap::{ArgAction, Parser}; #[derive(Parser, Debug)] pub struct Build { /// Should build in release mode #[clap(short, long, default_value="false", action = ArgAction::SetTrue)] - release: bool + release: bool, } pub fn handle(args: Build) { - let gradle_path = find_gradle().expect("ERROR: Gradle not found on system"); - // Decide gradle subcommand to use let cmd = match args.release { true => "assembleRelease", - false => "assembleDebug" + false => "assembleDebug", }; // Invoke gradle as child process - println!("Invoking Gradle: {}", gradle_path); - let status = Command::new(gradle_path) - .arg(cmd) - .status() - .unwrap(); + let status = android_cli::invoke_gradle_command(cmd).unwrap(); match status.success() { true => println!("Success!"), - false => println!("Failed while executing Gradle.") + false => println!("Failed while executing Gradle."), } } diff --git a/src/commands/create.rs b/src/commands/create.rs index f49deee..f7569d9 100644 --- a/src/commands/create.rs +++ b/src/commands/create.rs @@ -1,16 +1,9 @@ -use std::{ - collections::BTreeMap, - path::{Path, PathBuf}, -}; +use std::{collections::BTreeMap, path::PathBuf}; use clap::Parser; -use guidon::{GitOptions, Guidon, TryNew}; use crate::utils::prompt_for_input; -const DEFAULT_TEMPLATE_REPO: &str = "https://github.com/SyedAhkam/android-cli-template"; -const TEMPLATE_REV: &str = "master"; - const DEFAULT_COMPILE_SDK_VERSION: &str = "33"; const DEFAULT_TARGET_SDK_VERSION: &str = "33"; const DEFAULT_MIN_SDK_VERSION: &str = "24"; @@ -46,7 +39,7 @@ pub struct Create { /// Minimum SDK version that the app supports #[clap(long, default_value = DEFAULT_MIN_SDK_VERSION)] - min_sdk_version: u32 + min_sdk_version: u32, } fn get_vars(args: &Create) -> BTreeMap { @@ -76,37 +69,21 @@ fn get_vars(args: &Create) -> BTreeMap { map.insert("package_id_name".into(), name); // Version numbers - map.insert("compile_sdk_version".into(), args.compile_sdk_version.to_string()); - map.insert("target_sdk_version".into(), args.target_sdk_version.to_string()); + map.insert( + "compile_sdk_version".into(), + args.compile_sdk_version.to_string(), + ); + map.insert( + "target_sdk_version".into(), + args.target_sdk_version.to_string(), + ); map.insert("min_sdk_version".into(), args.min_sdk_version.to_string()); map } -fn create_local_properties_file(args: &Create) { - let prop_file_path = PathBuf::new() - .join(args.dest.as_ref().unwrap()) - .join("local.properties"); - - let content = format!("sdk.dir={}", args.sdk_path.as_ref().unwrap()); - std::fs::write(prop_file_path, content).expect("Unable to write local.properties file") -} - -fn post_init(args: Create) { - create_local_properties_file(&args) -} - -fn copy_template(dest: &Path, vars: BTreeMap) { - let git_options = GitOptions::builder() - .repo(DEFAULT_TEMPLATE_REPO) - .rev(TEMPLATE_REV) - .build() - .unwrap(); - - let mut guidon = Guidon::try_new(git_options).unwrap(); - - guidon.variables(vars); - guidon.apply_template(dest).unwrap(); +fn post_create(args: Create) { + android_cli::create_local_properties_file(args.dest.unwrap().as_path(), &args.sdk_path.unwrap()) } fn ensure_valid_args(args: Create) -> Create { @@ -152,10 +129,10 @@ pub fn handle(args: Create) { // Copy template let dest = args.dest.as_ref().unwrap(); - copy_template(dest, vars); + android_cli::copy_template(dest, vars); // Perform post init tasks - post_init(args); + post_create(args); println!("Project created successfully"); } diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..8c3f54e --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,52 @@ +mod utils; + +use itertools::Itertools; +use guidon::{GitOptions, Guidon, TryNew}; + +use std::{ + collections::BTreeMap, + path::{Path, PathBuf}, + process::{Command, ExitStatus}, +}; + +use utils::find_gradle; + +const DEFAULT_TEMPLATE_REPO: &str = "https://github.com/SyedAhkam/android-cli-template"; +const TEMPLATE_REV: &str = "master"; + +pub fn copy_template(dest: &Path, vars: BTreeMap) { + let git_options = GitOptions::builder() + .repo(DEFAULT_TEMPLATE_REPO) + .rev(TEMPLATE_REV) + .build() + .unwrap(); + + let mut guidon = Guidon::try_new(git_options).unwrap(); + + guidon.variables(vars); + guidon.apply_template(dest).unwrap(); +} + +pub fn create_local_properties_file(root: &Path, sdk_path: &str) { + let prop_file_path = PathBuf::new().join(root).join("local.properties"); + + let content = format!("sdk.dir={}", sdk_path); + std::fs::write(prop_file_path, content).expect("Unable to write local.properties file") +} + +pub fn invoke_gradle_command(cmd: &str) -> Result> { + let gradle_path = find_gradle().expect("ERROR: Gradle not found on system"); + + let mut run = Command::new(gradle_path); + run.arg(cmd); + + println!( + "Invoking Gradle: {} {}", + &run.get_program().to_string_lossy(), + &run.get_args() + .map(|arg| arg.to_string_lossy()) + .join(" ") + ); + + Ok(run.status()?) +}