@@ -31,6 +31,8 @@ import (
31
31
"github.com/cenkalti/backoff/v4"
32
32
"github.com/cli/browser"
33
33
"github.com/fatih/color"
34
+ "golang.org/x/mod/modfile"
35
+ "golang.org/x/mod/semver"
34
36
)
35
37
36
38
type Arguments struct {
@@ -92,7 +94,7 @@ func runCmd(ctx context.Context, w io.Writer, args Arguments) (err error) {
92
94
}
93
95
var opts []generator.GenerateOpt
94
96
if args .IncludeVersion {
95
- opts = append (opts , generator .WithVersion (templ .Version ))
97
+ opts = append (opts , generator .WithVersion (templ .Version () ))
96
98
}
97
99
if args .IncludeTimestamp {
98
100
opts = append (opts , generator .WithTimestamp (time .Now ()))
@@ -176,6 +178,10 @@ func runCmd(ctx context.Context, w io.Writer, args Arguments) (err error) {
176
178
}()
177
179
}
178
180
}
181
+ if err = checkTemplVersion (args .Path ); err != nil {
182
+ logWarning (w , "templ version check failed: %v\n " , err )
183
+ err = nil
184
+ }
179
185
if firstRunComplete {
180
186
if changesFound > 0 {
181
187
bo .Reset ()
@@ -400,3 +406,54 @@ func logWithDecoration(w io.Writer, decoration string, col color.Attribute, form
400
406
color .New (col ).Fprintf (w , "(%s) " , decoration )
401
407
fmt .Fprintf (w , format , a ... )
402
408
}
409
+
410
+ func checkTemplVersion (dir string ) error {
411
+ // Walk up the directory tree, starting at dir, until we find a go.mod file.
412
+ // If it contains a go.mod file, parse it and find the templ version.
413
+ dir , err := filepath .Abs (dir )
414
+ if err != nil {
415
+ return fmt .Errorf ("failed to get absolute path: %w" , err )
416
+ }
417
+ for {
418
+ current := filepath .Join (dir , "go.mod" )
419
+ _ , err := os .Stat (current )
420
+ if err != nil && ! os .IsNotExist (err ) {
421
+ return fmt .Errorf ("failed to stat go.mod file: %w" , err )
422
+ }
423
+ if os .IsNotExist (err ) {
424
+ // Move up.
425
+ prev := dir
426
+ dir = filepath .Dir (dir )
427
+ if dir == prev {
428
+ return fmt .Errorf ("could not find go.mod file" )
429
+ }
430
+ continue
431
+ }
432
+ // Found a go.mod file.
433
+ // Read it and find the templ version.
434
+ m , err := os .ReadFile (current )
435
+ if err != nil {
436
+ return fmt .Errorf ("failed to read go.mod file: %w" , err )
437
+ }
438
+ mf , err := modfile .Parse (current , m , nil )
439
+ if err != nil {
440
+ return fmt .Errorf ("failed to parse go.mod file: %w" , err )
441
+ }
442
+ if mf .Module .Mod .Path == "github.com/a-h/templ" {
443
+ // The go.mod file is for templ itself.
444
+ return nil
445
+ }
446
+ for _ , r := range mf .Require {
447
+ if r .Mod .Path == "github.com/a-h/templ" {
448
+ cmp := semver .Compare (r .Mod .Version , templ .Version ())
449
+ if cmp < 0 {
450
+ 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 )
451
+ }
452
+ if cmp > 0 {
453
+ 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 )
454
+ }
455
+ return nil
456
+ }
457
+ }
458
+ }
459
+ }
0 commit comments