fermyon / spin

Spin is the open source developer tool for building and running serverless applications powered by WebAssembly.
https://developer.fermyon.com/spin
Apache License 2.0
5.26k stars 249 forks source link

Provide a way for templates to rename files #725

Open itowlson opened 2 years ago

itowlson commented 2 years ago

For example if generating a C# project you would ideally like the .csproj file to be named for the component/application.

itowlson commented 2 years ago

Had an explore of this by allowing a rename field with a Liquid template as its value (e.g. rename = "{{ project-name }}.csproj", and that worked okay.

However, that approach doesn't extend to non-string values, or where something other than simple substitution is needed. Consider a setting to conditionally skip a file. You might hope that {{ foo == "bar" }} would do it, but it doesn't. First the {{}} syntax allows only variables and filters as far as I can tell, not comparisons. Second, template rendering can only result in a string, not a boolean. Which makes sense: it's a templating engine, not a general purpose expression engine.

Now I think you can work around this. E.g. skip_if = "{% if foo == \"bar\" %} true {% else %} false {% endif %}" parses and would produce a true/false string. But eww.

A possible alternative is to use Rhai expressions for metadata expressions and Liquid templates for content bodies. But having two different syntaxes in the same system feels confusing as heck. Another possibility is to provide filters (and recognise strings for true/false/number/etc.), e.g. {{ foo | eq: \"bar\" }}. That doesn't feel very natural either.

I don't want to push ahead on the rename feature until I'm confident we have a framework for other non-substitutive expressions and operations. So for now I will retreat and lick my wounds...

fibonacci1729 commented 2 years ago

Might be worth having a look at cargo generate and how they do templated file names, https://github.com/cargo-generate/cargo-generate/blob/main/src/filenames.rs

itowlson commented 2 years ago

Thanks! Unfortunately, the trouble I'm hitting is not how to do file names in itself; it's extending that to other behaviour overrides. That said, it's cool to see CG doing it this way - I toyed with this idea but then decided to pursue a more extensible system - maybe I should forget trying to unify everything ever and settle for a collection of simple point solutions like this. Great pointer and food for thought - thanks again!