Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
Refs #58 - Simplify name generating logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ondrej Prazak committed Jan 12, 2020
1 parent d366f9b commit fe6013f
Showing 1 changed file with 39 additions and 49 deletions.
88 changes: 39 additions & 49 deletions src/commands/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,30 @@ pub fn generate(
) -> Result<(), failure::Error> {
validate_worker_name(name)?;

log::info!("Generating a new worker project with name '{}'", name);
run_generate(name, template)?;
let dirname_exists = match directory_exists(name) {
Ok(val) => val,
Err(_) => true,
};

let new_name: String;

if dirname_exists {
new_name = match generate_name(name) {
Ok(val) => val,
Err(_) => {
log::debug!(
"Failed to auto-increment name for a new worker project, using '{}'",
name
);
String::from(name)
}
};
} else {
new_name = String::from(name);
}

log::info!("Generating a new worker project with name '{}'", new_name);
run_generate(&new_name, template)?;

let config_path = PathBuf::from("./").join(&name);
// TODO: this is tightly coupled to our site template. Need to remove once
Expand All @@ -26,7 +48,7 @@ pub fn generate(
} else {
None
};
Manifest::generate(name.to_string(), target_type, &config_path, generated_site)?;
Manifest::generate(new_name, target_type, &config_path, generated_site)?;

Ok(())
}
Expand All @@ -35,46 +57,21 @@ pub fn run_generate(name: &str, template: &str) -> Result<(), failure::Error> {
let tool_name = "cargo-generate";
let binary_path = install::install(tool_name, "ashleygwilliams")?.binary(tool_name)?;

let new_name = match generate_name(name) {
Ok(val) => val,
Err(_) => {
log::debug!(
"Failed to auto-increment name for a new worker project, using '{}'",
name
);
String::from(name)
}
};

let args = [
"generate", "--git", template, "--name", &new_name, "--force",
];
let args = ["generate", "--git", template, "--name", name, "--force"];

let command = command(binary_path, &args);
let command_name = format!("{:?}", command);
commands::run(command, &command_name)
}

fn generate_name(name: &str) -> Result<String, failure::Error> {
let digits = detect_num(name);
let mut new_name = String::from(name);
let mut chars = new_name.chars().collect::<Vec<char>>();
let split = chars.split_off(new_name.len() - digits);
let bare_name = chars.into_iter().collect::<String>();

let mut num = match split.into_iter().collect::<String>().parse::<usize>() {
Ok(val) => Some(val),
Err(_) => None,
};

let mut num = 1;
let entry_names = read_current_dir()?;
let mut new_name = construct_name(&name, num);

while entry_names.contains(&OsString::from(&new_name)) {
num = num.map(|val| val + 1);
new_name = construct_name(&bare_name, num);
if num.is_none() {
num = Some(1);
}
num = num + 1;
new_name = construct_name(&name, num);
}
Ok(new_name)
}
Expand All @@ -85,28 +82,21 @@ fn read_current_dir() -> Result<Vec<OsString>, failure::Error> {

for entry in fs::read_dir(current_dir)? {
let entry = entry?;
entry_names.push(entry.file_name());
let path = entry.path();
if path.is_dir() {
entry_names.push(entry.file_name());
}
}
Ok(entry_names)
}

fn construct_name(bare_name: &str, num: Option<usize>) -> String {
let new_num = match num {
Some(val) => val.to_string(),
None => String::from("1"),
};
String::from(bare_name) + &new_num
fn directory_exists(dirname: &str) -> Result<bool, failure::Error> {
let entry_names = read_current_dir()?;
Ok(entry_names.contains(&OsString::from(dirname)))
}

fn detect_num(name: &str) -> usize {
let digits = String::from(name).chars().rev().fold(0, |memo, item| {
if item.is_digit(10) {
memo + 1
} else {
return memo;
}
});
digits
fn construct_name(name: &str, num: i32) -> String {
format!("{}-{}", name, num)
}

fn command(binary_path: PathBuf, args: &[&str]) -> Command {
Expand Down

0 comments on commit fe6013f

Please sign in to comment.