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

template: Process Directories and Files #397

Merged
merged 1 commit into from Aug 6, 2021

Conversation

dave-tucker
Copy link
Contributor

@dave-tucker dave-tucker commented Aug 4, 2021

In order to properly allow for templates in directory names, we must
adjust the WalkDir to include both files and directories AND to yield
contents first.

Given that the project name is bar, and the directory tree:

| {{project-name}}/foo.rs
| {{projcet-name}}

  • {{project-name}}/foo.rs is read and the contents rendered
  • fs:create_dir_all will create the bar directory
  • fs::write will write bar/foo.rs
  • the {{project-name}} directory is then deleted with fs::remove_dir_all

Which will provide:

| bar/foo.rs

Fixes: #396

Command output from this commit:

🔧   Creating project called `foo`...
[ 1/12]   Done: .appveyor.yml
[ 2/12]   Done: .gitignore
[ 3/12]   Done: .travis.yml
[ 4/12]   Done: Cargo.toml
[ 5/12]   Done: LICENSE_APACHE
[ 6/12]   Done: LICENSE_MIT
[ 7/12]   Done: README.md
[ 8/12]   Done: src/lib.rs
[ 9/12]   Done: src/utils.rs
[10/12]   Done: src
[11/12]   Done: tests/web.rs
[12/12]   Done: tests
✨   Done! New project created /home/dave/dev/cargo-generate/foo

.into_iter()
.filter_map(Result::ok)
.filter(|e| !e.file_type().is_dir())
.filter(|e| !is_git_metadata(e))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.filter(|e| !is_git_metadata(e))
.filter(|e| !is_git_metadata(e))
.filter(|e| e.path() != project_dir)

Copy link
Member

Choose a reason for hiding this comment

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

this is the change for the last unnecessary substitution on the root level

@sassman
Copy link
Member

sassman commented Aug 5, 2021

thanks for your fix!

One odd side effect is that I can't figure out how to prune the root directory from the WalkDir:

Above you'll find a solution for the issue

In order to properly allow for templates in directory names, we must
adjust the WalkDir to include both files and directories AND to yield
contents first.

Given that the project name is bar, and the directory tree:

| {{project-name}}/foo.rs
| {{project-name}}

- {{project-name}}/foo.rs is read and the contents rendered
- fs:create_dir_all will create the bar directory
- fs::write will write bar/foo.rs
- the {{project-name}} directory is then deleted with fs::remove_dir_all

Which will provide:

| bar/foo.rs

Fixes: cargo-generate#396

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
@dave-tucker
Copy link
Contributor Author

Thanks @sassman 🙏 I've incorporated your suggested fix and it works as expected!

@sassman sassman merged commit ef81aef into cargo-generate:master Aug 6, 2021
@sassman
Copy link
Member

sassman commented Aug 6, 2021

Thanks a lot @dave-tucker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

using pattern in dir doesn't work correctly
2 participants