From ffa0b23ade4e69fc97bcd872e5b120d7418a5b85 Mon Sep 17 00:00:00 2001 From: Vladislav Bulagakov Date: Mon, 15 Sep 2025 11:14:50 +0300 Subject: [PATCH] feat: Add version and commit information to easyjson generator Closes #415 --- Makefile | 6 +++++- easyjson/main.go | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index cc5ebba..44d3434 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,6 @@ +VERSION := $(shell git describe --tags --always --dirty) +COMMIT := $(shell git rev-parse --short HEAD) + all: test clean: @@ -6,7 +9,8 @@ clean: rm -rf benchmark/*_easyjson.go build: - go build -o ./bin/easyjson ./easyjson + go build -ldflags="-s -w -X 'main.Version=$(VERSION)' -X 'main.Commit=$(COMMIT)'" -o ./bin/easyjson ./easyjson + generate: build bin/easyjson -stubs \ diff --git a/easyjson/main.go b/easyjson/main.go index d337c84..55be0ac 100644 --- a/easyjson/main.go +++ b/easyjson/main.go @@ -16,6 +16,11 @@ import ( "github.com/mailru/easyjson/parser" ) +var ( + Version = "dev" // + Commit = "none" +) + var buildTags = flag.String("build_tags", "", "build tags to add to generated file") var genBuildFlags = flag.String("gen_build_flags", "", "build flags when running the generator while bootstrapping") var snakeCase = flag.Bool("snake_case", false, "use snake_case names instead of CamelCase by default") @@ -31,6 +36,7 @@ var specifiedName = flag.String("output_filename", "", "specify the filename of var processPkg = flag.Bool("pkg", false, "process the whole package instead of just the given file") var disallowUnknownFields = flag.Bool("disallow_unknown_fields", false, "return error if any unknown field in json appeared") var skipMemberNameUnescaping = flag.Bool("disable_members_unescape", false, "don't perform unescaping of member names to improve performance") +var showVersion = flag.Bool("version", false, "print version and exit") func generate(fname string) (err error) { fInfo, err := os.Stat(fname) @@ -98,6 +104,11 @@ func main() { files := flag.Args() + if *showVersion { + fmt.Printf("easyjson generator\nversion: %s\ncommit: %s\n", Version, Commit) + os.Exit(0) + } + gofile := os.Getenv("GOFILE") if *processPkg { gofile = filepath.Dir(gofile)