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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(case): project names are kebabs #64

Merged
merged 6 commits into from Jul 28, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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: 0 additions & 9 deletions src/interactive.rs
@@ -1,7 +1,6 @@
use console::style;
use dialoguer::Input;
use emoji;
use heck::KebabCase;
use quicli::prelude::Error;
use regex;

Expand All @@ -14,14 +13,6 @@ pub fn name() -> Result<String, Error> {
style("Project Name").bold()
)).interact()?;
if valid_ident.is_match(&name) {

println!(
"{} {} `{}`{}",
emoji::WRENCH,
style("Creating project called").bold(),
style(&name.to_kebab_case()).bold().yellow(),
style("...").bold()
);
break name;
} else {
eprintln!(
Expand Down
16 changes: 13 additions & 3 deletions src/main.rs
Expand Up @@ -15,9 +15,11 @@ mod emoji;
mod git;
mod interactive;
mod progressbar;
mod projectname;
mod template;

use console::style;
use projectname::ProjectName;
use quicli::prelude::*;
use std::env;

Expand Down Expand Up @@ -58,13 +60,21 @@ pub struct Args {
main!(|_cli: Cli| {
let Cli::Generate(args) = Cli::from_args();
let name = match &args.name {
Some(ref n) => n.to_string(),
None => interactive::name()?,
Some(ref n) => ProjectName::new(n),
None => ProjectName::new(&interactive::name()?),
};

println!(
"{} {} `{}`{}",
emoji::WRENCH,
style("Creating project called").bold(),
style(&name.kebab_case()).bold().yellow(),
style("...").bold()
);

let project_dir = env::current_dir()
.unwrap_or_else(|_e| ".".into())
.join(&name);
.join(&name.kebab_case());

ensure!(
!project_dir.exists(),
Expand Down
23 changes: 23 additions & 0 deletions src/projectname.rs
@@ -0,0 +1,23 @@
use heck::{KebabCase, SnakeCase};

/// Stores user inputted name and provides convenience methods
/// for handling casing.
pub struct ProjectName {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add a comment to describe the pupose

user_input: String,
}

impl ProjectName {
pub fn new(name: &str) -> ProjectName {
ProjectName {
user_input: name.to_string(),
}
}

pub fn kebab_case(&self) -> String {
self.user_input.to_kebab_case()
}

pub fn snake_case(&self) -> String {
self.user_input.to_snake_case()
}
}
14 changes: 7 additions & 7 deletions src/template.rs
Expand Up @@ -3,6 +3,7 @@ use console::style;
use emoji;
use indicatif::ProgressBar;
use liquid;
use projectname::ProjectName;
use quicli::prelude::*;
use std::fs;
use std::path::PathBuf;
Expand All @@ -12,12 +13,15 @@ fn engine() -> liquid::Parser {
liquid::ParserBuilder::new().build()
}

pub fn substitute(name: &str) -> Result<liquid::Object> {
pub fn substitute(name: &ProjectName) -> Result<liquid::Object> {
let mut template = liquid::Object::new();
template.insert(String::from("project-name"), liquid::Value::scalar(name));
template.insert(
String::from("project-name"),
liquid::Value::scalar(&name.kebab_case()),
);
template.insert(
String::from("crate_name"),
liquid::Value::scalar(&hyphen_to_lodash(name)),
liquid::Value::scalar(&name.snake_case()),
);
template.insert(
String::from("authors"),
Expand All @@ -26,10 +30,6 @@ pub fn substitute(name: &str) -> Result<liquid::Object> {
Ok(template)
}

fn hyphen_to_lodash(string: &str) -> String {
string.to_string().replace("-", "_")
}

pub fn walk_dir(project_dir: &PathBuf, template: liquid::Object, pbar: ProgressBar) -> Result<()> {
let engine = engine();
for entry in WalkDir::new(project_dir) {
Expand Down
34 changes: 34 additions & 0 deletions tests/integration/basics.rs
Expand Up @@ -40,6 +40,40 @@ version = "0.1.0"
);
}

#[test]
fn it_snakecases_projectname_when_passed_to_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")
.current_dir(&dir.path())
.assert()
.success()
.stdout(predicates::str::contains("Done!").from_utf8());

assert!(
dir.read("foobar-project/Cargo.toml")
.contains("foobar-project")
);
}

#[test]
fn it_substitutes_cratename_in_a_rust_file() {
let template = dir("template")
Expand Down