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

using files in template parent directories to create files in generated project directories #1054

Open
wt opened this issue Nov 8, 2023 · 3 comments

Comments

@wt
Copy link

wt commented Nov 8, 2023

I have a git repo with multiple generation templates.

<root>/examples/
-> aaa
-> bbb

Some of the templates have files with the same contents.

<root>examples/aaa
-> Makefile.toml (uses ../cargo-make/main.toml)
<root>examples/bbb
-> Makefile.toml (uses ../cargo-make/main.toml)

I can't find a way to use the common files to create files in the generated template.

I would like to be able to do something like.

[template]
include_common = [
  {path="../cargo-make", dest_path="cargo-make"},
]

I want this to have the effect of using the files in the ../cargo-make/ as templates for corresponding files that are created in the cargo-make subfolder of the generated project root.

Even rhai scripts only appear to be able to rename files in the generated folder or generate files from a string or array into the generated folder. The rhai scripts don't appear to allow reading a file within a parent directory of the template directory.

Is there a way to do what I want with cargo-generate right now that I am not seeing?

Thanks a bunch!

@wt wt added the Type: Bug label Nov 8, 2023
@taurr
Copy link
Collaborator

taurr commented Nov 8, 2023

Nope, cargo-generate restricts access to files residing within the template directory for security reasons.

It could be argued that the rule should be relaxed to allow access within the repository, but that would be difficult to ensure since templates can also come from raw folders. Therefore, the template folder represents the broadest scope that is allowed.

@wt
Copy link
Author

wt commented Nov 14, 2023

How would you recommend handling multiple templates in a single repo that all refer to files that contain the same contents?

Here's an example of a bunch of examples that could also be templates if not for the shared files: https://github.com/leptos-rs/leptos/tree/main/examples

@MabezDev
Copy link

I am also looking for something like this, in esp-rs, we will have many templates that share many of the same files, only some parts in main.rs and Cargo.toml changing. I really don't want to be duplicating a bunch of these files, across the repo.

It could be argued that the rule should be relaxed to allow access within the repository, but that would be difficult to ensure since templates can also come from raw folders

Could you expand on this a little? I don't quite understand the connection from repo root to raw folders, are they not exclusive? I.e when calling cargo generate its either a repo via --git, or a raw folder via --path. Could we at least relax the rules in the --git case?

If that isn't possible/suitable, do you have any other suggestions to achieve this? I'm happy to work on a PR.

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

No branches or pull requests

3 participants