1
0
mirror of https://github.com/a-h/templ.git synced 2025-02-06 09:45:21 +00:00

feat: show templ CLI and go.mod version mismatch warning (#338)

This commit is contained in:
Adrian Hesketh 2023-12-17 21:02:42 +00:00 committed by GitHub
parent 99fd97f108
commit f217754e07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 70 additions and 8 deletions

View File

@ -31,6 +31,8 @@ import (
"github.com/cenkalti/backoff/v4"
"github.com/cli/browser"
"github.com/fatih/color"
"golang.org/x/mod/modfile"
"golang.org/x/mod/semver"
)
type Arguments struct {
@ -92,7 +94,7 @@ func runCmd(ctx context.Context, w io.Writer, args Arguments) (err error) {
}
var opts []generator.GenerateOpt
if args.IncludeVersion {
opts = append(opts, generator.WithVersion(templ.Version))
opts = append(opts, generator.WithVersion(templ.Version()))
}
if args.IncludeTimestamp {
opts = append(opts, generator.WithTimestamp(time.Now()))
@ -176,6 +178,10 @@ func runCmd(ctx context.Context, w io.Writer, args Arguments) (err error) {
}()
}
}
if err = checkTemplVersion(args.Path); err != nil {
logWarning(w, "templ version check failed: %v\n", err)
err = nil
}
if firstRunComplete {
if changesFound > 0 {
bo.Reset()
@ -400,3 +406,54 @@ func logWithDecoration(w io.Writer, decoration string, col color.Attribute, form
color.New(col).Fprintf(w, "(%s) ", decoration)
fmt.Fprintf(w, format, a...)
}
func checkTemplVersion(dir string) error {
// Walk up the directory tree, starting at dir, until we find a go.mod file.
// If it contains a go.mod file, parse it and find the templ version.
dir, err := filepath.Abs(dir)
if err != nil {
return fmt.Errorf("failed to get absolute path: %w", err)
}
for {
current := filepath.Join(dir, "go.mod")
_, err := os.Stat(current)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to stat go.mod file: %w", err)
}
if os.IsNotExist(err) {
// Move up.
prev := dir
dir = filepath.Dir(dir)
if dir == prev {
return fmt.Errorf("could not find go.mod file")
}
continue
}
// Found a go.mod file.
// Read it and find the templ version.
m, err := os.ReadFile(current)
if err != nil {
return fmt.Errorf("failed to read go.mod file: %w", err)
}
mf, err := modfile.Parse(current, m, nil)
if err != nil {
return fmt.Errorf("failed to parse go.mod file: %w", err)
}
if mf.Module.Mod.Path == "github.com/a-h/templ" {
// The go.mod file is for templ itself.
return nil
}
for _, r := range mf.Require {
if r.Mod.Path == "github.com/a-h/templ" {
cmp := semver.Compare(r.Mod.Version, templ.Version())
if cmp < 0 {
return fmt.Errorf("generator %v is newer than templ version %v found in go.mod file, consider running `go get github.com/a-h/templ`", templ.Version(), r.Mod.Version)
}
if cmp > 0 {
return fmt.Errorf("generator %v is older than templ version %v found in go.mod file, consider upgrading templ CLI", templ.Version(), r.Mod.Version)
}
return nil
}
}
}
}

View File

@ -51,10 +51,10 @@ func run(w io.Writer, args []string) (code int) {
case "lsp":
return lspCmd(w, args[2:])
case "version":
fmt.Fprintln(w, templ.Version)
fmt.Fprintln(w, templ.Version())
return 0
case "--version":
fmt.Fprintln(w, templ.Version)
fmt.Fprintln(w, templ.Version())
return 0
}
fmt.Fprint(w, usageText)

View File

@ -36,13 +36,13 @@ func TestMain(t *testing.T) {
{
name: `"templ version" prints version`,
args: []string{"templ", "version"},
expected: templ.Version + "\n",
expected: templ.Version() + "\n",
expectedCode: 0,
},
{
name: `"templ --version" prints version`,
args: []string{"templ", "--version"},
expected: templ.Version + "\n",
expected: templ.Version() + "\n",
expectedCode: 0,
},
{

View File

@ -34,7 +34,7 @@
name = "templ";
src = gitignore.lib.gitignoreSource ./.;
subPackages = [ "cmd/templ" ];
vendorHash = "sha256-skftApJDp52ZMFf4+jG0sNWK2jIXi3rDQP199suRgNw=";
vendorHash = "sha256-buJArvaaKGRg3yS7BdcVY0ydyi4zah57ABeo+CHkZQU=";
CGO_ENABLED = 0;
flags = [
"-trimpath"
@ -82,4 +82,5 @@
templ-docs = self.packages.${final.stdenv.system}.templ-docs;
};
};
}
}

View File

@ -3,4 +3,8 @@ package templ
import _ "embed"
//go:embed .version
var Version string
var version string
func Version() string {
return "v" + version
}