Skip to content

Commit

Permalink
feat: better prompt handling + default values
Browse files Browse the repository at this point in the history
  • Loading branch information
SyedAhkam committed Aug 21, 2023
1 parent 5c863c9 commit fc4652e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
45 changes: 32 additions & 13 deletions src/commands/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,50 @@ fn post_create(args: Create) -> Result<()> {
}

fn ensure_valid_args(args: Create) -> Result<Create> {
let dest = args
.dest
.unwrap_or_else(|| prompt_for_input("Enter destination path").unwrap().into());
let safe_name = |name: String| name.to_lowercase().replace(" ", "_");

let dest_folder_name = dest.file_name().unwrap().to_str().unwrap().to_owned(); // FIXME: this could fail on non-unicode paths
let dest = args.dest.unwrap_or_else(|| {
prompt_for_input("Enter destination path", Some(".".into()))
.unwrap()
.into()
});

// Ensure dest exists
if !dest.exists() {
println!("doesnt exist");
std::fs::create_dir_all(&dest)?;
}

// Normalize or Canonicalize the path
let dest = dest.canonicalize()?;

let dest_folder_name = dest
.file_name()
.unwrap()
.to_str()
.map(|name| safe_name(name.into()))
.unwrap()
.to_owned();

let sdk_path = args.sdk_path.unwrap_or_else(|| {
std::env::var("ANDROID_SDK_ROOT")
.context("ANDROID_SDK_ROOT not set")
.unwrap()
});

let project_name = args
.project_name
.unwrap_or(dest_folder_name) // defaults to dest folder name
.to_lowercase()
.replace(" ", "_"); // Perform some cleanup
let project_name = args.project_name.map(safe_name).unwrap_or_else(|| {
prompt_for_input("Enter project name", Some(safe_name(dest_folder_name))).unwrap()
});

let app_name = args.name.unwrap_or_else(|| project_name.clone());

let package_id = args.package_id.unwrap_or_else(|| {
prompt_for_input("Enter package identifier [example: com.example.demo]")
.unwrap()
.to_lowercase()
.replace(" ", "_")
prompt_for_input(
"Enter package identifier [suggested to customize]",
Some(format!("com.example.{}", project_name)),
)
.map(safe_name)
.unwrap()
});

Ok(Create {
Expand Down
19 changes: 12 additions & 7 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
use anyhow::{Context, Result};
use which::which;

pub fn prompt_for_input(prompt: &str) -> Result<String> {
Ok(
dialoguer::Input::<String>::with_theme(&dialoguer::theme::ColorfulTheme::default())
.with_prompt(prompt)
.interact_text()
.context("failed to prompt user")?,
)
pub fn prompt_for_input(prompt: &str, default: Option<String>) -> Result<String> {
let theme = dialoguer::theme::ColorfulTheme::default();
let mut builder = dialoguer::Input::<String>::with_theme(&theme);

if let Some(default_value) = default {
builder.default(default_value);
}

Ok(builder
.with_prompt(prompt)
.interact_text()
.context("failed to prompt user")?)
}

pub fn find_gradle() -> Option<String> {
Expand Down

0 comments on commit fc4652e

Please sign in to comment.