wrouesnel / p2cli

pongo2 cli - like the j2cli package in python, but compiles to self-contained go executables
GNU General Public License v2.0
87 stars 20 forks source link

Support filename renaming in directory mode #33

Closed tobiade closed 2 years ago

tobiade commented 2 years ago

Hi, I have a scenario where I have a directory with template files which have the pattern: *.tmpl.* e.g config.tmpl.json. I like naming my files this way to indicate that they are template files which will be rendered during a build.

I'm not sure if this a common use-case, but I'd like to be able to rename those files when using p2cli with directory-mode enabled.

For example, I'd like to remove the *.tmpl.* substring in all filenames that have that pattern e.g config.tmpl.json becomes config.json.

In short, I'd like to match a pattern in the filename and replace it with a specified string. If you think this is useful, I'm happy to raise a PR for this feature.

wrouesnel commented 2 years ago

This sounds like a reasonable feature, it's just a question of the best way to do the name transformations. I think to keep things simple maybe just a regex delete mode:

For your use case the executing command would be:

p2cli --directory-mode --directory-mode-filename-subst '\.tmpl(\..*)$' '$1'

Which is a bit ugly, but probably avoids edge cases with a properly specified regex. I suspect this sort of option should have a dry-run mode so you can list what the names will come out as for development.

Probably some helpers as well so you can just specify the regex:

p2cli --directory-mode --directory-mode-filename-del '\.tmpl\..*'

One important question here is whether this sort of replacement should also include a selector mode and handle "non-template" files (by copying them unaltered?)

tobiade commented 2 years ago

Thanks! I was thinking simple as well - just a simple substring match and replace i.e no need for complicated regex.

Some questions/comments: