Exclude files or directories based on answers
jonas-hagen opened this issue · comments
Actual Situation
To supply a modular template to the user, currently there are several options:
- Use partial templates and copy multiple templates into one target folder (manually setting a name for the answer file)
- Having if-clauses in files and manually delete empty files
- Use an additional script to be run after template creation to delete unused files
This is about the same situation with cookiecutter as well.
This is not ideal, as supporting modular templates involves quite some manual work and cognitive overhead.
Desired Situation
- A user can enable or disable certain features in the questionnaire
- Certain files are copied or not based on these answers
- Content of the files is adjusted to the features as usual
- Thus, a single template can contain multiple components, of which only a subset can be installed (and updated)
Proposed solution
A special variable or function could be used, like:
{{% if not enabled_feature1 %}} {{ skip_this_file() }} {{% endif %}}
def skip_this_file():
raise SkipThisFileError
For directories, a configuration variable in the sense of _skip_empty_dirs
can be introduced. If empty dirs shall be kept, a .gitkeep
or similar file must be introduced anyways to track it in git.
IIUC, what you'd like to do is already possible with Copier.
- Questions in the questionnaire can define features to be enabled or disabled. You can even have conditional questions via
when
to ask some questions only when a condition (typically based on a previous question's answer) is met. See https://copier.readthedocs.io/en/latest/configuring/#advanced-prompt-formatting. - Files (and directories) can be "skipped" (i.e. omitted) by using a templated filename, typically using a Jinja
if
clause, such that the filename may become empty (except an optional template suffix), causing the file to be omitted. See https://copier.readthedocs.io/en/latest/configuring/#conditional-files-and-directories.
WDYT, @jonas-hagen?
Thanks! Indeed it is possible, no idea how this could slip my attention and search. It also never occurred to me that filenames could be conditional.
To be honest, and sorry for such a harsh word, I think this is ugly. I still think, the proposed solution is more clear and prettier, but I understand that multiple ways of achieving the same is maybe not desirable. Feel free to close.