From c0e01cf1d2529d3a3c1783ba57c9f1e171435b04 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Mon, 23 Oct 2023 20:28:22 +0100 Subject: [PATCH] chore: use a pre-commit hook to set a .version file instead of using git This ensures that the flake, go get and goreleaser deployments all have the same version number, since the version number will come from the git repo instead. --- .goreleaser.yaml | 5 ++++- .version | 1 + generator/.version | 1 + generator/generator.go | 24 ++++-------------------- increment-version/main.go | 33 +++++++++++++++++++++++++++++++++ push-tag.sh | 8 ++++---- version.go | 4 +++- 7 files changed, 50 insertions(+), 26 deletions(-) create mode 100644 .version create mode 100644 generator/.version create mode 100644 increment-version/main.go diff --git a/.goreleaser.yaml b/.goreleaser.yaml index e0e86d8c9..1d1a5c630 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -2,8 +2,11 @@ builds: - env: - CGO_ENABLED=0 dir: cmd/templ + mod_timestamp: '{{ .CommitTimestamp }}' + flags: + - -trimpath ldflags: - - -s -w -X github.com/a-h/templ.Version={{.Version}} + - -s -w goos: - linux - windows diff --git a/.version b/.version new file mode 100644 index 000000000..18cae6ffb --- /dev/null +++ b/.version @@ -0,0 +1 @@ +0.2.418-next \ No newline at end of file diff --git a/generator/.version b/generator/.version new file mode 100644 index 000000000..18cae6ffb --- /dev/null +++ b/generator/.version @@ -0,0 +1 @@ +0.2.418-next \ No newline at end of file diff --git a/generator/generator.go b/generator/generator.go index addc77a6a..d57e4a3c2 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -7,9 +7,10 @@ import ( "html" "io" "reflect" - "runtime/debug" "strings" + _ "embed" + "github.com/a-h/templ/parser/v2" ) @@ -48,28 +49,11 @@ func (g *generator) generate() (err error) { return err } -// Binary builds set this version string. goreleaser sets the value using Go build ldflags. +//go:embed .version 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(fmt.Sprintf("// Code generated by templ@%s DO NOT EDIT.\n\n", getVersion())) + _, err := g.w.Write(fmt.Sprintf("// Code generated by templ@%s DO NOT EDIT.\n\n", version)) return err } diff --git a/increment-version/main.go b/increment-version/main.go new file mode 100644 index 000000000..50490fa9c --- /dev/null +++ b/increment-version/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "flag" + "fmt" + "log" + "os/exec" + "strings" +) + +var fileFlag = flag.String("file", ".version", "Set the name of the file to modify") + +func main() { + gitPath, err := exec.LookPath("git") + if err != nil { + log.Fatalf("failed to find git on path: %v", err) + } + + cmd := exec.Command(gitPath, "rev-list", "main", "--count") + output, err := cmd.Output() + if err != nil { + log.Fatalf("failed to run git: %v", err) + } + count := strings.TrimSpace(string(output)) + + var dirty string + cmd = exec.Command(gitPath, "diff", "--quiet") + if cmd.Run() != nil { + dirty = "-next" + } + + fmt.Printf("0.2.%s%s", count, dirty) +} diff --git a/push-tag.sh b/push-tag.sh index f62cefa7a..3d5232315 100755 --- a/push-tag.sh +++ b/push-tag.sh @@ -1,5 +1,5 @@ #!/bin/sh -export VERSION=`git rev-list --count HEAD`; -echo Adding git tag with version v0.2.${VERSION}; -git tag v0.2.${VERSION}; -git push origin v0.2.${VERSION}; +export VERSION=`cat .version` +echo Adding git tag with version v${VERSION}; +git tag v${VERSION}; +git push origin v${VERSION}; diff --git a/version.go b/version.go index 244bc0067..0ad236858 100644 --- a/version.go +++ b/version.go @@ -1,4 +1,6 @@ package templ -// Binary builds set this version string. goreleaser sets the value using Go build ldflags. +import _ "embed" + +//go:embed .version var Version string