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
This commit is contained in:
Max
2020-05-24 13:40:11 +03:00
committed by GitHub
parent 0d574ab354
commit 8ba3c7bdce
3 changed files with 28 additions and 4 deletions
+7 -1
View File
@@ -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:
+14 -3
View File
@@ -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)
+7
View File
@@ -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,