forked from mailru/easyjson
/
bootstrap.go
216 lines (191 loc) · 5.26 KB
/
bootstrap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Package bootstrap implements the bootstrapping logic: generation of a .go file to
// launch the actual generator and launching the generator itself.
//
// The package may be preferred to a command-line utility if generating the serializers
// from golang code is required.
package bootstrap
import (
"fmt"
"go/format"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
)
const genPackage = "github.com/betprophet1/easyjson/gen"
const pkgWriter = "github.com/betprophet1/easyjson/jwriter"
const pkgLexer = "github.com/betprophet1/easyjson/jlexer"
var buildFlagsRegexp = regexp.MustCompile("'.+'|\".+\"|\\S+")
type Generator struct {
PkgPath, PkgName string
Types []string
NoStdMarshalers bool
SnakeCase bool
LowerCamelCase bool
OmitEmpty bool
DisallowUnknownFields bool
SkipMemberNameUnescaping bool
OutName string
BuildTags string
GenBuildFlags string
StubsOnly bool
LeaveTemps bool
NoFormat bool
SimpleBytes bool
}
// writeStub outputs an initial stub for marshalers/unmarshalers so that the package
// using marshalers/unmarshales compiles correctly for boostrapping code.
func (g *Generator) writeStub() error {
f, err := os.Create(g.OutName)
if err != nil {
return err
}
defer f.Close()
if g.BuildTags != "" {
fmt.Fprintln(f, "// +build ", g.BuildTags)
fmt.Fprintln(f)
}
fmt.Fprintln(f, "// TEMPORARY AUTOGENERATED FILE: easyjson stub code to make the package")
fmt.Fprintln(f, "// compilable during generation.")
fmt.Fprintln(f)
fmt.Fprintln(f, "package ", g.PkgName)
if len(g.Types) > 0 {
fmt.Fprintln(f)
fmt.Fprintln(f, "import (")
fmt.Fprintln(f, ` "`+pkgWriter+`"`)
fmt.Fprintln(f, ` "`+pkgLexer+`"`)
fmt.Fprintln(f, ")")
}
sort.Strings(g.Types)
for _, t := range g.Types {
fmt.Fprintln(f)
if !g.NoStdMarshalers {
fmt.Fprintln(f, "func (", t, ") MarshalJSON() ([]byte, error) { return nil, nil }")
fmt.Fprintln(f, "func (*", t, ") UnmarshalJSON([]byte) error { return nil }")
}
fmt.Fprintln(f, "func (", t, ") MarshalEasyJSON(w *jwriter.Writer) {}")
fmt.Fprintln(f, "func (*", t, ") UnmarshalEasyJSON(l *jlexer.Lexer) {}")
fmt.Fprintln(f)
fmt.Fprintln(f, "type EasyJSON_exporter_"+t+" *"+t)
}
return nil
}
// writeMain creates a .go file that launches the generator if 'go run'.
func (g *Generator) writeMain() (path string, err error) {
f, err := ioutil.TempFile(filepath.Dir(g.OutName), "easyjson-bootstrap")
if err != nil {
return "", err
}
fmt.Fprintln(f, "// +build ignore")
fmt.Fprintln(f)
fmt.Fprintln(f, "// TEMPORARY AUTOGENERATED FILE: easyjson bootstapping code to launch")
fmt.Fprintln(f, "// the actual generator.")
fmt.Fprintln(f)
fmt.Fprintln(f, "package main")
fmt.Fprintln(f)
fmt.Fprintln(f, "import (")
fmt.Fprintln(f, ` "fmt"`)
fmt.Fprintln(f, ` "os"`)
fmt.Fprintln(f)
fmt.Fprintf(f, " %q\n", genPackage)
if len(g.Types) > 0 {
fmt.Fprintln(f)
fmt.Fprintf(f, " pkg %q\n", g.PkgPath)
}
fmt.Fprintln(f, ")")
fmt.Fprintln(f)
fmt.Fprintln(f, "func main() {")
fmt.Fprintf(f, " g := gen.NewGenerator(%q)\n", filepath.Base(g.OutName))
fmt.Fprintf(f, " g.SetPkg(%q, %q)\n", g.PkgName, g.PkgPath)
if g.BuildTags != "" {
fmt.Fprintf(f, " g.SetBuildTags(%q)\n", g.BuildTags)
}
if g.SnakeCase {
fmt.Fprintln(f, " g.UseSnakeCase()")
}
if g.LowerCamelCase {
fmt.Fprintln(f, " g.UseLowerCamelCase()")
}
if g.OmitEmpty {
fmt.Fprintln(f, " g.OmitEmpty()")
}
if g.NoStdMarshalers {
fmt.Fprintln(f, " g.NoStdMarshalers()")
}
if g.DisallowUnknownFields {
fmt.Fprintln(f, " g.DisallowUnknownFields()")
}
if g.SimpleBytes {
fmt.Fprintln(f, " g.SimpleBytes()")
}
if g.SkipMemberNameUnescaping {
fmt.Fprintln(f, " g.SkipMemberNameUnescaping()")
}
sort.Strings(g.Types)
for _, v := range g.Types {
fmt.Fprintln(f, " g.Add(pkg.EasyJSON_exporter_"+v+"(nil))")
}
fmt.Fprintln(f, " if err := g.Run(os.Stdout); err != nil {")
fmt.Fprintln(f, " fmt.Fprintln(os.Stderr, err)")
fmt.Fprintln(f, " os.Exit(1)")
fmt.Fprintln(f, " }")
fmt.Fprintln(f, "}")
src := f.Name()
if err := f.Close(); err != nil {
return src, err
}
dest := src + ".go"
return dest, os.Rename(src, dest)
}
func (g *Generator) Run() error {
if err := g.writeStub(); err != nil {
return err
}
if g.StubsOnly {
return nil
}
path, err := g.writeMain()
if err != nil {
return err
}
if !g.LeaveTemps {
defer os.Remove(path)
}
f, err := os.Create(g.OutName + ".tmp")
if err != nil {
return err
}
if !g.LeaveTemps {
defer os.Remove(f.Name()) // will not remove after rename
}
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)
if err = cmd.Run(); err != nil {
return err
}
f.Close()
// move unformatted file to out path
if g.NoFormat {
return os.Rename(f.Name(), g.OutName)
}
// format file and write to out path
in, err := ioutil.ReadFile(f.Name())
if err != nil {
return err
}
out, err := format.Source(in)
if err != nil {
return err
}
return ioutil.WriteFile(g.OutName, out, 0644)
}