From af7fe7171eb29a652f46efd4cbbb1bf155f9825d Mon Sep 17 00:00:00 2001 From: joe-davidson1802 Date: Wed, 19 Jan 2022 11:44:39 +0000 Subject: [PATCH] encode version used to generate template (#34) --- example/posts_templ.go | 2 +- generator/generator.go | 23 ++++++++++++++++++- generator/test-a-href/template_templ.go | 2 +- .../test-attribute-escaping/template_templ.go | 2 +- generator/test-call/template_templ.go | 2 +- .../test-css-expression/template_templ.go | 2 +- generator/test-css-usage/template_templ.go | 2 +- generator/test-doctype/template_templ.go | 2 +- generator/test-for/template_templ.go | 2 +- generator/test-html/template_templ.go | 2 +- generator/test-if/template_templ.go | 2 +- generator/test-ifelse/template_templ.go | 2 +- generator/test-script-usage/template_templ.go | 2 +- generator/test-string/template_templ.go | 2 +- generator/test-switch/template_templ.go | 2 +- .../test-switchdefault/template_templ.go | 2 +- generator/test-text/template_templ.go | 2 +- generator/test-void/template_templ.go | 2 +- storybook/example/templates_templ.go | 2 +- turbo/stream_templ.go | 2 +- 20 files changed, 41 insertions(+), 20 deletions(-) diff --git a/example/posts_templ.go b/example/posts_templ.go index 6c2040f0d..af0c10989 100644 --- a/example/posts_templ.go +++ b/example/posts_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package main diff --git a/generator/generator.go b/generator/generator.go index 409ff5ca2..e02637c25 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -7,6 +7,7 @@ import ( "html" "io" "reflect" + "runtime/debug" "strings" "github.com/a-h/templ" @@ -90,8 +91,28 @@ func (g *generator) generate() (err error) { return err } +// Binary builds set this version string. goreleaser sets the value using Go build ldflags. +var version string + +// Source builds use this value. When installed using `go install github.com/a-h/templ/cmd/templ@latest` the `version` variable is empty, but +// the debug.ReadBuildInfo return value provides the package version number installed by `go install` +func goInstallVersion() string { + info, ok := debug.ReadBuildInfo() + if !ok { + return "unknown" + } + return info.Main.Version +} + +func getVersion() string { + if version != "" { + return version + } + return goInstallVersion() +} + func (g *generator) writeCodeGeneratedComment() error { - _, err := g.w.Write("// Code generated by templ DO NOT EDIT.\n\n") + _, err := g.w.Write(fmt.Sprintf("// Code generated by templ@%s DO NOT EDIT.\n\n", getVersion())) return err } diff --git a/generator/test-a-href/template_templ.go b/generator/test-a-href/template_templ.go index eb7698843..f59fe3e0b 100644 --- a/generator/test-a-href/template_templ.go +++ b/generator/test-a-href/template_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package testahref diff --git a/generator/test-attribute-escaping/template_templ.go b/generator/test-attribute-escaping/template_templ.go index aa9832340..7408190cc 100644 --- a/generator/test-attribute-escaping/template_templ.go +++ b/generator/test-attribute-escaping/template_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package testhtml diff --git a/generator/test-call/template_templ.go b/generator/test-call/template_templ.go index 29b8d0cd6..c8d0e88b4 100644 --- a/generator/test-call/template_templ.go +++ b/generator/test-call/template_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package testcall diff --git a/generator/test-css-expression/template_templ.go b/generator/test-css-expression/template_templ.go index cb6034952..26f19cd7f 100644 --- a/generator/test-css-expression/template_templ.go +++ b/generator/test-css-expression/template_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package testcssexpression diff --git a/generator/test-css-usage/template_templ.go b/generator/test-css-usage/template_templ.go index d4e85bcc2..08d4cf008 100644 --- a/generator/test-css-usage/template_templ.go +++ b/generator/test-css-usage/template_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package testcssusage diff --git a/generator/test-doctype/template_templ.go b/generator/test-doctype/template_templ.go index ae3c81d48..67f6dc7ab 100644 --- a/generator/test-doctype/template_templ.go +++ b/generator/test-doctype/template_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package testdoctype diff --git a/generator/test-for/template_templ.go b/generator/test-for/template_templ.go index f3a22ae20..1a8a0648a 100644 --- a/generator/test-for/template_templ.go +++ b/generator/test-for/template_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package testfor diff --git a/generator/test-html/template_templ.go b/generator/test-html/template_templ.go index e5dce8140..7b246c3b7 100644 --- a/generator/test-html/template_templ.go +++ b/generator/test-html/template_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package testhtml diff --git a/generator/test-if/template_templ.go b/generator/test-if/template_templ.go index 9ece164f4..c516b30f5 100644 --- a/generator/test-if/template_templ.go +++ b/generator/test-if/template_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package testif diff --git a/generator/test-ifelse/template_templ.go b/generator/test-ifelse/template_templ.go index fe5b5c3df..5c9bca2a9 100644 --- a/generator/test-ifelse/template_templ.go +++ b/generator/test-ifelse/template_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package ifelse diff --git a/generator/test-script-usage/template_templ.go b/generator/test-script-usage/template_templ.go index 741e621aa..78bfa173a 100644 --- a/generator/test-script-usage/template_templ.go +++ b/generator/test-script-usage/template_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package testscriptusage diff --git a/generator/test-string/template_templ.go b/generator/test-string/template_templ.go index 9e10c4ac4..b8b0656b1 100644 --- a/generator/test-string/template_templ.go +++ b/generator/test-string/template_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package teststring diff --git a/generator/test-switch/template_templ.go b/generator/test-switch/template_templ.go index 946b128aa..965487948 100644 --- a/generator/test-switch/template_templ.go +++ b/generator/test-switch/template_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package testswitch diff --git a/generator/test-switchdefault/template_templ.go b/generator/test-switchdefault/template_templ.go index b8e72ad88..2cbb6a3a5 100644 --- a/generator/test-switchdefault/template_templ.go +++ b/generator/test-switchdefault/template_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package testswitchdefault diff --git a/generator/test-text/template_templ.go b/generator/test-text/template_templ.go index 407ac9794..147764e67 100644 --- a/generator/test-text/template_templ.go +++ b/generator/test-text/template_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package testtext diff --git a/generator/test-void/template_templ.go b/generator/test-void/template_templ.go index de34147e9..b3f2555e4 100644 --- a/generator/test-void/template_templ.go +++ b/generator/test-void/template_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package testvoid diff --git a/storybook/example/templates_templ.go b/storybook/example/templates_templ.go index 31c7cb357..952dd1d81 100644 --- a/storybook/example/templates_templ.go +++ b/storybook/example/templates_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package example diff --git a/turbo/stream_templ.go b/turbo/stream_templ.go index 4e51c6a6f..da0d56247 100644 --- a/turbo/stream_templ.go +++ b/turbo/stream_templ.go @@ -1,4 +1,4 @@ -// Code generated by templ DO NOT EDIT. +// Code generated by templ@(devel) DO NOT EDIT. package turbo