Skip to content

Commit

Permalink
chore: use a pre-commit hook to set a .version file instead of using git
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
a-h committed Oct 23, 2023
1 parent faa36c1 commit c0e01cf
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 26 deletions.
5 changes: 4 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.2.418-next
1 change: 1 addition & 0 deletions generator/.version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.2.418-next
24 changes: 4 additions & 20 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"html"
"io"
"reflect"
"runtime/debug"
"strings"

_ "embed"

"github.com/a-h/templ/parser/v2"
)

Expand Down Expand Up @@ -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
}

Expand Down
33 changes: 33 additions & 0 deletions increment-version/main.go
Original file line number Diff line number Diff line change
@@ -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")

Check failure on line 11 in increment-version/main.go

View workflow job for this annotation

GitHub Actions / lint

var `fileFlag` is unused (unused)

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)
}
8 changes: 4 additions & 4 deletions push-tag.sh
Original file line number Diff line number Diff line change
@@ -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};
4 changes: 3 additions & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c0e01cf

Please sign in to comment.