rgst-io / stencil

📒 A modern living-template engine for evolving repositories
https://stencil.rgst.io
Apache License 2.0
4 stars 1 forks source link

Support templates that only render in the initial generation #86

Open erincgucer opened 3 weeks ago

erincgucer commented 3 weeks ago

There are cases when a file (or directory) has to be rendered only on the initial generation of the template, aka when stencil.lock file does not exist.

One example use case of this is when we want to create some conventional directories (for example a "service" directory which would contain business logic classes) for a spring boot microservice. As GitHub does not take empty directories into consideration, in order to create those directories at the very beginning, we need to put at least one file in them. (for example a README.md). After the initial creation though, that placeholder file would eventually be deleted or replaced by other files. (actual business logic classes in our example). So it would not make sense to use file.Static, as the placeholder would already be deleted and we would not want it to be generated all over again and again in the consecutive runs.

erincgucer commented 3 weeks ago

I think this can be done with the following for now;

{{- if stencil.Exists "stencil.lock" }} {{- file.Skip "Should only be generated once as a placeholder" }} {{- end -}}

deregtd commented 3 weeks ago

That'll just do the very first run tho -- if you add a new template that is this one-time behavior to a module and re-run later, it won't make it once. So we still need to figure out a strong notion of how to handle this within stencil.

jaredallard commented 3 weeks ago

I'll need to think about this one for a bit before I take it on, but as a work around, the following should work:

{{- $skip := false }}
{{- if stencil.Exists "stencil.lock" }}
    {{- $stencilLock := stencil.ReadFile "stencil.lock" | fromYaml }}
    {{- range $stencilLock.files }}
        {{- if eq .name "<fileName>" }}
            {{- $skip = true }}
            {{- break }}
        {{- end }}
    {{- end }}
{{- else }}
    {{- $skip = true }}
{{- end }}
{{- if $skip }}
    {{- file.Skip "Already generated once" }}
{{- end }}