Skip to content

Feat: Improve feedback on invalid name in new command and add CLI prompt to recover #3422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions compiler-cli/src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use strum::{Display, EnumIter, EnumString, IntoEnumIterator, VariantNames};
#[cfg(test)]
mod tests;

use crate::{fs::get_current_directory, NewOptions};
use crate::{cli, fs::get_current_directory, NewOptions};

const GLEAM_STDLIB_REQUIREMENT: &str = ">= 0.34.0 and < 2.0.0";
const GLEEUNIT_REQUIREMENT: &str = ">= 1.0.0 and < 2.0.0";
Expand Down Expand Up @@ -203,7 +203,35 @@ impl Creator {
.trim()
.to_string();

validate_name(&project_name)?;
let project_name = match validate_name(&project_name) {
Ok(()) => project_name,
Err(error) => match error.clone() {
Error::InvalidProjectName { name: _, reason: _ } => {
println!("{}", error.pretty_string());
let allowed_characters_regex = regex::Regex::new("[^a-z0-9_]")
.expect("new name regex could not be compiled");
let valid_format_regex = regex::Regex::new("^[a-z][a-z0-9_]*$")
.expect("new name regex could not be compiled");
let sanitized_name = allowed_characters_regex.replace_all(&project_name, "_");
let replacement_name = if valid_format_regex.is_match(&sanitized_name) {
format!("my_{sanitized_name}")
} else {
return Err(error);
};
validate_name(&replacement_name)?;

if cfg!(test) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this please. We never use conditional test code.

replacement_name
} else {
match cli::confirm(&format!("Would you like to create a project named {replacement_name} in a directory named {project_name}?")) {
Ok(true) => replacement_name,
Ok(false) | Err(_) => return Err(error)
}
}
}
_ => return Err(error),
},
};

let root = get_current_directory()?.join(&options.project_root);
let src = root.join("src");
Expand Down
34 changes: 34 additions & 0 deletions compiler-cli/src/new/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,40 @@ fn invalid_name() {
.is_err());
}

#[test]
fn new_with_recoverable_invalid_name() {
let tmp = tempfile::tempdir().unwrap();
let invalid_name = "gleam-demo";
let replacement_name = "my_gleam_demo";
let path = Utf8PathBuf::from_path_buf(tmp.path().join(&invalid_name)).expect("Non Utf8 Path");

let creator = super::Creator::new(
super::NewOptions {
project_root: path.to_string(),
template: super::Template::Lib,
name: None,
skip_git: false,
skip_github: false,
},
"1.0.0-gleam",
)
.unwrap();

creator.run().unwrap();

assert!(path.join(".git").exists());
assert!(path.join("README.md").exists());
assert!(path.join("gleam.toml").exists());
assert!(path.join(format!("src/{replacement_name}.gleam")).exists());
assert!(path
.join(format!("test/{replacement_name}_test.gleam"))
.exists());
assert!(path.join(".github/workflows/test.yml").exists());

let toml = crate::fs::read(path.join("gleam.toml")).unwrap();
assert!(toml.contains(&format!(r#"name = "{replacement_name}""#)));
}

#[test]
fn existing_directory_no_files() {
let tmp = tempfile::tempdir().unwrap();
Expand Down