dnaka91 / cargo-hatch

Hatch new projects like a chick coming out of its egg
GNU Affero General Public License v3.0
7 stars 1 forks source link

feat: support ignoring files from templating #15

Open ModProg opened 1 year ago

ModProg commented 1 year ago

This supports excluding files from being executed as a template but keeps them arround to be used by templates.

This allows both files containing invalid templates without wrapping them in {% raw %}, but more importantly it allows macros.

The problem with macros is, that they are not allowed in templates, so this is the only way to implement them.

files with template = false are now added to template_files so they can be referenced, but they are not rendered as templates them selves.

closes #14 somewhat resolves #9

ModProg commented 1 year ago

One thing I noticed is, that we could maybe allow bool for condition or have it be true by default.

Another option would be to have an additional .teraignore file. But not sure about that.

dnaka91 commented 1 year ago

This is a great start, but I'd rather opt to extend the FileIgnore struct with an enum, that gives a bit more fine-grained control over how to ignore (or rather exclude) the file. What I mean is something like an ignore type or "ignore from" value:

Probably "ignore from" would give a better meaning than type. Would read like "ignore from All", "ignore from Template(-ing)", and "ignore from Copy(-ing)".

Eventually, this could be extended into the .hatchignore with special attributes, similar to how Syncthing does it. For example, have a (?t) at the start to set the type to Template and (?c) to set the type to Copy. But that's something we'd better do in a separate PR, if not completely removing .hatchignore after all, as it's quite duplicate with the ignore rules in .hatch.toml.

ModProg commented 1 year ago

I implemented it that way now. But AFAICT there is no reason to ignore from copy but not from template.

Because if something is referenced from another template, it does not need to be templated itself.

So we'd only need these cases:

  1. don't ignore at all
  2. ignore from templating but copy over
  3. don't use other (this will mean it is still added to the context, but it is not compiled as a template nor copied over).

So it should be enough to have:

Unless you think we need a way to not add files to the tera context at all.

dnaka91 commented 1 year ago

I think it might make sense to completely ignore files from templating, but still copy them over. For example, currently I use mime types to detect whether a file is binary or not, but that might not catch all the right files at all times.

By having a special rule for it, we can cope for that case and define simple copy-only file rules. Then we

ModProg commented 1 year ago

So the cases you'd propose would be

  1. Render as template (unless binary)
  2. Copy over and hide from tera
  3. Copy over and make available to tera
  4. Dont copy and hide from tera
  5. Dont copy and make available to terra

IMO there is no need to actually differentiate between 4. and 5. or 2. and 3. as they would not make a difference to a user I think. Or does terra do something with the files added, even when they are not evalutat as template and would fail on e.g. invalid Unicode.

ModProg commented 1 year ago

I just wanted to use this, and remembered we didn't complete it. Was there something left todo?