From 8ba3c7bdceed0f2e8089b8bac6627e18a1392659 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 24 May 2020 13:40:11 +0300 Subject: [PATCH] Added logic to pass build flags when running the generator (#290) * Added logic to pass build flags when running the generator while bootstrapping * fixed parsing build args for differenct cases * cleanup code --- README.md | 8 +++++++- bootstrap/bootstrap.go | 17 ++++++++++++++--- easyjson/main.go | 7 +++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2f4287a..952575b 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,9 @@ Usage of easyjson: -all generate marshaler/unmarshalers for all structs in a file -build_tags string - build tags to add to generated file + build tags to add to generated file + -gen_build_flags string + build flags when running the generator while bootstrapping -byte use simple bytes instead of Base64Bytes for slice of bytes -leave_temps @@ -88,6 +90,10 @@ Additional option notes: * `-build_tags` will add the specified build tags to generated Go sources. +* `-gen_build_flags` will execute the easyjson bootstapping code to launch the + actual generator command with provided flags. Multiple arguments should be + separated by space e.g. `-gen_build_flags="-mod=mod -x"`. + ## Structure json tag options Besides standart json tag options like 'omitempty' the following are supported: diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index 7e984d9..5755bee 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -12,6 +12,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "sort" ) @@ -19,6 +20,8 @@ const genPackage = "github.com/mailru/easyjson/gen" const pkgWriter = "github.com/mailru/easyjson/jwriter" const pkgLexer = "github.com/mailru/easyjson/jlexer" +var buildFlagsRegexp = regexp.MustCompile("'.+'|\".+\"|\\S+") + type Generator struct { PkgPath, PkgName string Types []string @@ -30,8 +33,9 @@ type Generator struct { DisallowUnknownFields bool SkipMemberNameUnescaping bool - OutName string - BuildTags string + OutName string + BuildTags string + GenBuildFlags string StubsOnly bool LeaveTemps bool @@ -178,7 +182,14 @@ func (g *Generator) Run() error { defer os.Remove(f.Name()) // will not remove after rename } - cmd := exec.Command("go", "run", "-tags", g.BuildTags, filepath.Base(path)) + execArgs := []string{"run"} + if g.GenBuildFlags != "" { + buildFlags := buildFlagsRegexp.FindAllString(g.GenBuildFlags, -1) + execArgs = append(execArgs, buildFlags...) + } + execArgs = append(execArgs, "-tags", g.BuildTags, filepath.Base(path)) + cmd := exec.Command("go", execArgs...) + cmd.Stdout = f cmd.Stderr = os.Stderr cmd.Dir = filepath.Dir(path) diff --git a/easyjson/main.go b/easyjson/main.go index 7e935fb..d337c84 100644 --- a/easyjson/main.go +++ b/easyjson/main.go @@ -17,6 +17,7 @@ import ( ) 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") var lowerCamelCase = flag.Bool("lower_camel_case", false, "use lowerCamelCase names instead of CamelCase by default") var noStdMarshalers = flag.Bool("no_std_marshalers", false, "don't generate MarshalJSON/UnmarshalJSON funcs") @@ -62,8 +63,14 @@ func generate(fname string) (err error) { trimmedBuildTags = strings.TrimSpace(*buildTags) } + var trimmedGenBuildFlags string + if *genBuildFlags != "" { + trimmedGenBuildFlags = strings.TrimSpace(*genBuildFlags) + } + g := bootstrap.Generator{ BuildTags: trimmedBuildTags, + GenBuildFlags: trimmedGenBuildFlags, PkgPath: p.PkgPath, PkgName: p.PkgName, Types: p.StructNames,