-
-
Notifications
You must be signed in to change notification settings - Fork 182
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
Allow templating items in _exclude
#1625
Comments
Are you aware that Copier allows to do that thanks to file names? A file named More generally, a file with an empty name will not be created (it would be impossible to create it anyway). Same for directories, although they don't need the |
Not exactly, because the file in question is configuring the template, it cannot have any other name. That file has to exist as is, and excluded on copy conditionally. At the moment I have to overwrite it and make it clean not to copy my template's config over, but it is not used in the destination. Does it make sense? |
Oh, IIUC you have files that are used both for maintaining the template, and as files to copy to destination projects. Interesting. I'd recommend keeping two separate copies indeed, and using the subdirectory feature to separate them. Just my opinion but if you need to change config for your template you might not want this change to apply to projects too? Better explicit than implicit? |
Other than that, no strong opinion :) If other maintainers think this feature is worth it, I won't mind. |
Yeah, let me give you an example. I have a
|
Actually could you post snippets of your file layout? Because it's quite hard to understand how exactly you're using Copier from text alone.
Could you elaborate on that? I'm not sure to understand either. |
Here an example. The Regarding the CI that I mentioned. I have a Hope this makes it all clearer. |
OK I see, thanks a lot 🙂
Yeah that's one of the challenges I'm facing indeed. Tracking my progress here: pawamoy/copier-uv#24. |
I am running into the same issue. I can't edit the file name because it is needed for the template itself to run some checks. I tried to create a duplicate filename and then add that to the exclude but it ends up skipping both directories. |
I'd like an option that doesn't involve messing with the filename too because the conditionals can become pretty unwieldy and it also seems my editor (vim) doesn't really like it either, for some reason. I can open the file, but vim fails to save it because it seems to interpret some things incorrectly. |
I'm not sure I'm understanding the use case properly, but if I do, it seems to me like you can solve it with a combination of subdirectory and symlinks. One live example here:
As a result, the template CI itself uses the same CI primitives as the produced projects. But produced projects can configure that CI at will. Would this strategy solve your problems? Note: To make it even more fun, the template is applied to itself! |
I mean it would but at some point the conditional expressions get really large, and trying to embed all that logic into a file name becomes untenable. |
You can use a macro then, or a computed field |
Thank you @yajo for your input. Yes, using This is btw the solution I currently have. I have many symlinked files to try to not repeat myself a lot. But it is not clear and it is not easy to mantain. I think the solution I am proposing would be cleaner and easier, without any breaking changes. |
There's a subtle difference between using Think of this example template for a Python project with configurable project layout and optional CLI: layout:
type: str
help: Which Python project layout would you like?
choices:
flat: ""
src: null
package_slug:
type: str
help: Your Python package name/slug
cli:
type: bool
help: Do you need a CLI?
Here, the Jinja condition for the CLI-related file is part of its filename, so it works for either project layout. But if we were to use the +_exclude:
+ - "{% if not cli %}/{{ layout }}/{{ package_slug }}/__main__.py{% endif %}"
layout:
type: str
help: Which Python project layout would you like?
choices:
flat: ""
src: null
package_slug:
type: str
help: Your Python package name/slug
cli:
type: bool
help: Do you need a CLI? .
└── {{ layout }}
└── {{ package_slug }}
- ├── {% if cli %}__main__.py{% endif %}.jinja
+ ├── __main__.py.jinja
└── __init__.py TBH, I find the latter neither more intuitive nor more robust. If the conditions become more complex, a computed value may be used as @yajo already suggested. Note that macros may not work at the moment because of #1164, and my upstream PR pallets/jinja#1852 does not seem to get merged for some reason. |
thanks @sisp! I agree my proposal being more intuitive / robust is subjective. But it gives the user the option to choose how they want to encode the logic. I personally prefer to read simpler file names and a use the exlude pattern that is still pretty explicit. Using dynamic names & computed values in the exclude section would be pretty powerful. Maybe it does not become simpler, but it is cleaner to me, your exclude logic is confined clearly. Furthermore, it allows not requiring the |
I agree with @sisp but it's also true that I don't see that as a strong reason to oppose including templating in But if some contributor wants to open the PR, I'd be glad to review it. I'll label the issue as such. |
Actual Situation
I would like to exclude files / folders conditionally in the
copier.yaml
file. Currently, if you add a file/folder to the exclude list, it is always excluded, even if there is a suffixed file that converts. Therefore, the exclusion list applies to the result (this has been discussed in past issues).My real use case is that I have a few configuration files that apply to my template, and may or not apply to the destination depending on a certain variable. If they don't apply to the destination I would like to exclude them and not copy irrelevant files. If they apply, I would not exclude and normal overwrite rules apply.
Desired Situation
Being able to exclude files/folders conditionally would allow having config files in my template that are copied over conditionally on variables. This would be a new powerful feature.
Proposed solution
I would like to have something in the
copier.yaml
that would allow me to exclude files/folders:I think this solution would not impact current behavior of exlusions applying to final names. It just modifies applying the exclusion conditionally.
The text was updated successfully, but these errors were encountered: