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

Conditional exclude items #1625

Open
jzazo opened this issue May 3, 2024 · 9 comments
Open

Conditional exclude items #1625

jzazo opened this issue May 3, 2024 · 9 comments

Comments

@jzazo
Copy link

jzazo commented May 3, 2024

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:

_exclude:
  - "{% if variable is defined %}myconfig.yml{% endif %}"

I think this solution would not impact current behavior of exlusions applying to final names. It just modifies applying the exclusion conditionally.

@pawamoy
Copy link
Contributor

pawamoy commented May 3, 2024

Are you aware that Copier allows to do that thanks to file names?

A file named {% if some_condition %}filename.ext{% endif %}.jinja will only be created if the some_condition evaluates to true.

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 .jinja suffix.

@jzazo
Copy link
Author

jzazo commented May 3, 2024

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?

@pawamoy
Copy link
Contributor

pawamoy commented May 3, 2024

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?

@pawamoy
Copy link
Contributor

pawamoy commented May 3, 2024

Other than that, no strong opinion :) If other maintainers think this feature is worth it, I won't mind.

@jzazo
Copy link
Author

jzazo commented May 3, 2024

Yeah, let me give you an example. I have a acl/access.yml file that specifies who has right permissions in the template repo. When I create a copy of the template to apply to that same server, I ask the pertinent question of who should have access, and format a brand new file from acl/access.yml.jinja file, which will replace the template's original. If I am not creating a project for that server, I don't want to have this file at all in the new repo, so I would exclude the folder.

subdirectory feature is not ideal. I like to keep files in the root because I can test everything. I can run CI and builds exactly as they are going to run in the copies (or pretty close).

@pawamoy
Copy link
Contributor

pawamoy commented May 4, 2024

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.

subdirectory feature is not ideal. I like to keep files in the root because I can test everything. I can run CI and builds exactly as they are going to run in the copies (or pretty close).

Could you elaborate on that? I'm not sure to understand either.

@jzazo
Copy link
Author

jzazo commented May 4, 2024

Here an example. The access.yml file is used for compliance of the template repo. Copies of the template will inherit from the processed access.yml.jinja, encoding their own access. Same for the compliance folder. Copies of the template where I could conditionally exclude folders from, would not get the folder.

image

Regarding the CI that I mentioned. I have a .github/workflows/ci.yaml file running pytest and mkdocs builds. Those tests are run on the template repo, so I know that tests are being found and run, and documentation is being built. Because the same modules and CI is being copied to new repos, I can check that tests running for the template repo, will more likely pass in new copies. If I had a subdirectory with all my template files, I would have to have one ci workflow for the template, and another ci workflow for the subdirectory, completely different between themselves, I would not be able to test the subdirectory workflow at all (because it wouldn't be run, only the template's workflow would be run).

Hope this makes it all clearer.

@pawamoy
Copy link
Contributor

pawamoy commented May 5, 2024

OK I see, thanks a lot 🙂

If I had a subdirectory with all my template files, I would have to have one ci workflow for the template, and another ci workflow for the subdirectory, completely different between themselves, I would not be able to test the subdirectory workflow at all (because it wouldn't be run, only the template's workflow would be run).

Yeah that's one of the challenges I'm facing indeed. Tracking my progress here: pawamoy/copier-uv#24.

@michaelrode
Copy link

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.

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