Skip to content
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

Add force flag to allow user defined project name #69

Merged
merged 1 commit into from
Aug 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ pub struct Args {
git: String,
#[structopt(long = "name", short = "n")]
name: Option<String>,
/// Enforce to create a new project without case conversion of project name
#[structopt(long = "force", short = "f")]
force: bool,
}

main!(|_cli: Cli| {
Expand All @@ -73,6 +76,7 @@ main!(|_cli: Cli| {
Some(ref n) => ProjectName::new(n),
None => ProjectName::new(&interactive::name()?),
};
let force = args.force;

println!(
"{} {} `{}`{}",
Expand All @@ -82,9 +86,10 @@ main!(|_cli: Cli| {
style("...").bold()
);

let dir_name = if force { name.raw() } else { name.kebab_case() };
let project_dir = env::current_dir()
.unwrap_or_else(|_e| ".".into())
.join(&name.kebab_case());
.join(dir_name);

ensure!(
!project_dir.exists(),
Expand All @@ -95,7 +100,7 @@ main!(|_cli: Cli| {
git::create(&project_dir, args)?;
git::remove_history(&project_dir)?;

let template = template::substitute(&name)?;
let template = template::substitute(&name, force)?;

let pbar = progressbar::new();
pbar.tick();
Expand Down
4 changes: 4 additions & 0 deletions src/projectname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ impl ProjectName {
}
}

pub fn raw(&self) -> String {
self.user_input.to_owned()
}

pub fn kebab_case(&self) -> String {
self.user_input.to_kebab_case()
}
Expand Down
6 changes: 4 additions & 2 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ fn engine() -> liquid::Parser {
liquid::ParserBuilder::new().build()
}

pub fn substitute(name: &ProjectName) -> Result<liquid::Object> {
pub fn substitute(name: &ProjectName, force: bool) -> Result<liquid::Object> {
let project_name = if force { name.raw() } else { name.kebab_case() };

let mut template = liquid::Object::new();
template.insert(
String::from("project-name"),
liquid::Value::scalar(&name.kebab_case()),
liquid::Value::scalar(project_name),
);
template.insert(
String::from("crate_name"),
Expand Down
46 changes: 38 additions & 8 deletions tests/integration/basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ name = "{{project-name}}"
description = "A wonderful project"
version = "0.1.0"
"#,
)
.init_git()
).init_git()
.build();

let dir = dir("main").build();
Expand Down Expand Up @@ -50,8 +49,7 @@ name = "{{project-name}}"
description = "A wonderful project"
version = "0.1.0"
"#,
)
.init_git()
).init_git()
.build();

let dir = dir("main").build();
Expand Down Expand Up @@ -82,8 +80,7 @@ fn it_substitutes_cratename_in_a_rust_file() {
r#"
extern crate {{crate_name}};
"#,
)
.init_git()
).init_git()
.build();

let dir = dir("main").build();
Expand Down Expand Up @@ -115,8 +112,7 @@ name = "{{project-name}}"
description = "A wonderful project"
version = "0.1.0"
"#,
)
.init_git()
).init_git()
.build();

let dir = dir("main").build();
Expand All @@ -138,3 +134,37 @@ version = "0.1.0"
.contains("foobar-project")
);
}

#[test]
fn it_allows_user_defined_projectname_when_passing_force_flag() {
let template = dir("template")
.file(
"Cargo.toml",
r#"[package]
name = "{{project-name}}"
description = "A wonderful project"
version = "0.1.0"
"#,
).init_git()
.build();

let dir = dir("main").build();

Command::main_binary()
.unwrap()
.arg("generate")
.arg("--git")
.arg(template.path())
.arg("--name")
.arg("foobar_project")
.arg("--force")
.current_dir(&dir.path())
.assert()
.success()
.stdout(predicates::str::contains("Done!").from_utf8());

assert!(
dir.read("foobar_project/Cargo.toml")
.contains("foobar_project")
);
}