Skip to content

Commit

Permalink
feat: add muxt version comment
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Oct 31, 2024
1 parent b3bb1c0 commit 0e97047
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
11 changes: 10 additions & 1 deletion cmd/muxt/generate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"flag"
"fmt"
"go/token"
Expand Down Expand Up @@ -112,5 +113,13 @@ func generateCommand(args []string, workingDirectory string, getEnv func(string)
if err != nil {
return err
}
return os.WriteFile(filepath.Join(workingDirectory, g.outputFilename), []byte(CodeGenerationComment+"\n\n"+s), 0o644)
var sb bytes.Buffer
sb.WriteString(CodeGenerationComment)
if v, ok := cliVersion(); ok {
sb.WriteString("\n// muxt version: ")
sb.WriteString(v)
sb.WriteString("\n\n")
}
sb.WriteString(s)
return os.WriteFile(filepath.Join(workingDirectory, g.outputFilename), sb.Bytes(), 0o644)
}
17 changes: 17 additions & 0 deletions cmd/muxt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"runtime/debug"

"rsc.io/script"
)
Expand All @@ -24,6 +25,8 @@ func command(wd string, args []string, getEnv func(string) string, stdout, stder
switch cmd, cmdArgs := args[0], args[1:]; cmd {
case "generate", "gen", "g":
return generateCommand(cmdArgs, wd, getEnv, stdout, stderr)
case "version", "v":
return versionCommand(stdout)
}
}
return fmt.Errorf("unknown command")
Expand Down Expand Up @@ -55,3 +58,17 @@ func scriptCommand() script.Cmd {
}, nil
})
}

func versionCommand(stdout io.Writer) error {
v, _ := cliVersion()
_, err := io.WriteString(stdout, v)
return err
}

func cliVersion() (string, bool) {
bi, ok := debug.ReadBuildInfo()
if !ok || bi.Main.Version == "" {
return "", false
}
return bi.Main.Version, true
}
3 changes: 3 additions & 0 deletions cmd/muxt/testdata/generate/simple_get.txtar
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
muxt generate

cat template_routes.go
stdout '// muxt version: \(devel\)'

exec go test -cover

-- template.gohtml --
Expand Down

0 comments on commit 0e97047

Please sign in to comment.