-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
330 lines (301 loc) · 9.99 KB
/
main.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// The MIT License
//
// Copyright (c) 2022 Temporal Technologies Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
import (
"bytes"
"context"
"errors"
"flag"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"io/fs"
"os"
"os/exec"
"os/signal"
"path/filepath"
"regexp"
"strings"
)
var enumRgx = regexp.MustCompile(`^enum\s+(\w+)`)
type stringArr []string
func (s *stringArr) String() string {
return "[]string"
}
func (s *stringArr) Set(value string) error {
*s = append(*s, value)
return nil
}
type postProcessor interface {
Process(f *ast.File)
}
type genConfig struct {
rootDirs []string
outputDir string
excludeDirs []string
includes []string
descriptors []string
plugins []string
enums map[string]string
// Post-processors
rewriteString bool
rewriteEnums bool
stripVersions bool
disableUtf8 bool
}
// walkExtension walks the directory starting from root and calls the provided callback on all files that
// have the specified extension. `exclusions` specifies a list of paths we won't search
func walkExtension(ctx context.Context, root string, extension string, exclusions []string, fn func(string) error) error {
return filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
select {
case <-ctx.Done():
// Exit early. This error will be ignored because the errgroup will cancel the context only after
// setting the true error
return ctx.Err()
default:
}
if err != nil {
return err
} else if !strings.HasSuffix(path, extension) {
return nil
}
for i := 0; i < len(exclusions); i++ {
if strings.HasPrefix(path, exclusions[i]) {
return fs.SkipDir
}
}
return fn(path)
})
}
// Walk the file tree and collect all enum definitions we find
// so we can post-process the go code we're generating
func findEnums(ctx context.Context, cfg genConfig) ([]string, []string, error) {
seen := map[string]struct{}{}
var enums []string
var dirs []string
walkFunc := func(path string) error {
// Emit unique directories containing proto files
dir := filepath.Dir(path)
if _, ok := seen[dir]; !ok {
seen[dir] = struct{}{}
dirs = append(dirs, dir)
}
// There isn't a good way to iteratively find matches using an io.Reader
// so we need to pull the whole thing into memory. We could do it line by line
// but I doubt this will take enough memory to matter
bs, err := os.ReadFile(path)
if err != nil {
return err
}
matches := enumRgx.FindAllStringSubmatch(string(bs), -1)
for i := 0; i < len(matches); i++ {
enums = append(enums, matches[i][1])
}
return nil
}
for _, rootDir := range cfg.rootDirs {
if err := walkExtension(ctx, rootDir, ".proto", cfg.excludeDirs, walkFunc); err != nil {
return nil, nil, err
}
}
return enums, dirs, nil
}
// Run protoc in parallel on all proto dirs we discover under the root directory
// It returns the list of unique directories contai
func runProtoc(ctx context.Context, cfg genConfig, protoDirs []string) error {
for i := 0; i < len(protoDirs); i++ {
dir := protoDirs[i]
// Run protoc on each directory individually
args := []string{
"--fatal_warnings",
fmt.Sprintf("--go_out=paths=source_relative:%s", cfg.outputDir),
}
for _, include := range cfg.includes {
args = append(args, fmt.Sprintf("-I=%s", include))
}
for _, desc := range cfg.descriptors {
args = append(args, fmt.Sprintf("--descriptor_set_in=%s", desc))
}
// If we need more complex plugin handling, such as per-plugin options, we can add that later.
// For now we use the same args everywhere
for _, plugin := range cfg.plugins {
args = append(args, fmt.Sprintf("--%s", plugin))
}
files, err := filepath.Glob(filepath.Join(dir, "*"))
if err != nil {
return err
}
args = append(args, files...)
var stderr bytes.Buffer
protoc := exec.CommandContext(ctx, "protoc", args...)
protoc.Stderr = &stderr
protoc.Stdout = os.Stdout
if err := protoc.Run(); err != nil {
if errors.Is(err, context.Canceled) {
return err
}
stderrstr := strings.TrimSpace(stderr.String())
return fmt.Errorf("failed to run `protoc %s`: %w\n%s", strings.Join(args, " "), err, stderrstr)
}
}
return nil
}
func rewriteFile(fileName string, postProcessors ...postProcessor) error {
fset := token.NewFileSet()
// we're going to overwrite files in-place so we read it ahead of time
srcf, err := os.Open(fileName)
if err != nil {
return err
}
ff, err := parser.ParseFile(fset, fileName, srcf, parser.ParseComments)
if err != nil {
return err
}
// Close the file so we can overwrite it
srcf.Close()
for _, p := range postProcessors {
p.Process(ff)
}
of, err := os.Create(fileName)
if err != nil {
return err
}
defer of.Close()
return format.Node(of, fset, ff)
}
// Post-process generated protobuf go files for useability
func postProcess(ctx context.Context, cfg genConfig) error {
if !cfg.rewriteEnums && !cfg.rewriteString && !cfg.stripVersions {
// nothing to do
return nil
}
return walkExtension(ctx, cfg.outputDir, ".pb.go", cfg.excludeDirs, func(path string) error {
var postProcessors []postProcessor
if cfg.rewriteEnums {
postProcessors = append(postProcessors, NewConstRewriter(cfg.enums))
}
if cfg.rewriteString {
postProcessors = append(postProcessors, NewStringRewriter())
}
if cfg.stripVersions {
postProcessors = append(postProcessors, NewVersionRemover())
}
if cfg.disableUtf8 {
postProcessors = append(postProcessors, NewDisableUtf8Validation())
}
return rewriteFile(path, postProcessors...)
})
}
func compileProtos(ctx context.Context, cfg genConfig) error {
enums, dirs, err := findEnums(ctx, cfg)
if err != nil {
return err
}
if err := runProtoc(ctx, cfg, dirs); err != nil {
return err
}
// Add Enum:Prefix pairs for all identified pairs that weren't specifically overridden
for i := 0; i < len(enums); i++ {
e := enums[i]
if _, ok := cfg.enums[e]; !ok {
cfg.enums[e] = e
}
}
return postProcess(ctx, cfg)
}
func fail(msg string, args ...any) {
fmt.Fprintf(os.Stderr, msg, args...)
os.Exit(1)
}
// Contains reports whether v is present in s.
func sliceContains[S ~[]E, E comparable](haystack S, needle E) bool {
for i := 0; i < len(haystack); i++ {
if needle == haystack[i] {
return true
}
}
return false
}
func main() {
var outputDir, enumPrefixPairs string
var protoRootDirs, protoPlugins, protoIncludes, descriptorSetIn, excludeDirs stringArr
var noRewriteString, noRewriteEnum, noStripVersion, noDisableUtf8 bool
var concurrency int
flag.Var(&protoRootDirs, "root", "Root directories containing the protos to generate code for")
flag.StringVar(&outputDir, "output", "api", "Base directory in which to output generated proto files")
flag.Var(&protoIncludes, "I", "Directory to include when compiling protos")
flag.Var(&descriptorSetIn, "descriptor_set_in", "Files containing binary FileDescriptorSet messages as inputs")
flag.Var(&protoPlugins, "p", "Plugin=Options pairs of protobuf plugins, like grpc-gateway_out=allow_patch_feature=false")
flag.Var(&excludeDirs, "exclude", "Directory to exclude when compiling post-processing protos")
flag.StringVar(&enumPrefixPairs, "rewrite-enum", "",
"Comma-separated list of additional EnumType:CodePrefix pairs to remove when rewriting golang enums. Example: BuildId_State:BuildId")
flag.IntVar(&concurrency, "concurrency", 0, "Maximum number of goroutines to run. Defaults to 0, meaning unlimited")
flag.BoolVar(&noRewriteEnum, "no-rewrite-enum-const", false, "Don't rewrite enum constants")
flag.BoolVar(&noRewriteString, "no-rewrite-enum-string", false, "Don't rewrite enum String methods")
flag.BoolVar(&noStripVersion, "no-strip-version", false, "Don't remove protoc plugin versions from generated files")
flag.BoolVar(&noDisableUtf8, "no-disable-utf8", false, "Don't disable utf8 validation for strings")
flag.Parse()
if len(protoRootDirs) == 0 {
fail("must specify at least one root directory")
}
if outputDir == "" {
fail("must specify the output dir for proto files")
}
// Always include the root dir
for _, protoRootDir := range protoRootDirs {
if !sliceContains(protoIncludes, protoRootDir) {
protoIncludes = append(protoIncludes, protoRootDir)
}
}
// Cancel everything if we receive SIGINT or SIGSTOP
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer cancel()
enums := make(map[string]string)
if enumPrefixPairs != "" {
pairs := strings.Split(enumPrefixPairs, ",")
for _, pair := range pairs {
parts := strings.Split(pair, ":")
if len(parts) != 2 {
fail("invalid enum:prefix pair %q", pair)
}
enums[parts[0]] = parts[1]
}
}
err := compileProtos(ctx, genConfig{
rootDirs: protoRootDirs,
outputDir: outputDir,
excludeDirs: excludeDirs,
includes: protoIncludes,
descriptors: descriptorSetIn,
plugins: protoPlugins,
enums: enums,
rewriteString: !noRewriteString,
rewriteEnums: !noRewriteEnum,
stripVersions: !noStripVersion,
disableUtf8: !noDisableUtf8,
})
if err != nil {
fail(err.Error())
}
}