Skip to content

Commit

Permalink
auto remove .liquid file extensions if present (#639)
Browse files Browse the repository at this point in the history
* auto remove `.liquid` file extensions if present
Fixes #608

Co-authored-by: Sven Assmann <sven@d34dl0ck.me>
  • Loading branch information
taurr and sassman committed Jun 13, 2022
1 parent 27f4dc1 commit bf37bff
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
10 changes: 9 additions & 1 deletion guide/src/README.md
Expand Up @@ -9,10 +9,18 @@
`cargo-generate` is a developer tool to help you get up and running quickly with a new Rust
project by leveraging a pre-existing git repository as a template.

cargo-generate uses [Shopify's Liquid](http://liquidmarkup.org/) template language,
cargo-generate uses [Shopify's Liquid] template language,
[Rhai](https://docs.rs/rhai/latest/rhai/) for hook scripts and [regex](https://docs.rs/regex/latest/regex/) for placeholders.

Due to the use of [Shopify's Liquid], `cargo-generate` special cases files with the file-ending
`.liquid`, by simply removing the file-ending when processing the files. If you, as a template
author, truly wants the `.liquid` file-ending, you need to repeat it twice!

An Example: the file `main.rs.liquid` will be renamed after templating to `main.rs`

Here's an example of using `cargo-generate` with [this template]:

![demo.gif](./demo.gif)

[this template]: https://github.com/ashleygwilliams/wasm-pack-template
[Shopify's Liquid]: http://liquidmarkup.org/
10 changes: 8 additions & 2 deletions src/lib.rs
Expand Up @@ -281,12 +281,15 @@ pub(crate) fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Resu

for src_entry in fs::read_dir(src)? {
let src_entry = src_entry?;
let dst_path = dst.as_ref().join(src_entry.file_name());
let filename = src_entry.file_name().to_string_lossy().to_string();
let entry_type = src_entry.file_type()?;

if entry_type.is_dir() {
let dst_path = dst.as_ref().join(filename);
check_dir_all(src_entry.path(), dst_path)?;
} else if entry_type.is_file() {
let filename = filename.strip_suffix(".liquid").unwrap_or(&filename);
let dst_path = dst.as_ref().join(filename);
if dst_path.exists() {
bail!(
"{} {} {}",
Expand All @@ -310,14 +313,17 @@ pub(crate) fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Resu
let git_file_name: OsString = ".git".into();
for src_entry in fs::read_dir(src)? {
let src_entry = src_entry?;
let dst_path = dst.as_ref().join(src_entry.file_name());
let filename = src_entry.file_name().to_string_lossy().to_string();
let entry_type = src_entry.file_type()?;
if entry_type.is_dir() {
let dst_path = dst.as_ref().join(filename);
if git_file_name == src_entry.file_name() {
continue;
}
copy_dir_all(src_entry.path(), dst_path)?;
} else if entry_type.is_file() {
let filename = filename.strip_suffix(".liquid").unwrap_or(&filename);
let dst_path = dst.as_ref().join(filename);
fs::copy(src_entry.path(), dst_path)?;
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/filenames.rs
Expand Up @@ -13,7 +13,7 @@ fn it_substitutes_filename() {
r#"println!("Welcome in {{project-name}}");"#,
)
.file(
"src/{{project-name}}/lib.rs",
"src/{{project-name}}/lib.rs.liquid",
r#"println!("Welcome in {{project-name}}-lib");"#,
)
.init_git()
Expand Down Expand Up @@ -46,6 +46,6 @@ fn it_substitutes_filename() {
);
assert!(
!dir.exists("foobar-project/src/{{project-name}}/lib.rs"),
"project should not contain foobar-project/src/foobar-project/lib.rs"
"project should not contain foobar-project/src/foobar-project/lib.rs.liquid"
);
}

0 comments on commit bf37bff

Please sign in to comment.