-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
79 lines (69 loc) · 1.71 KB
/
plugin.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
package patch
import (
"bytes"
"io"
"os"
"os/exec"
"strings"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"
)
// StripParam strips a named param from req.
func StripParam(req *pluginpb.CodeGeneratorRequest, p string) {
if req.Parameter == nil {
return
}
v := stripParam(*req.Parameter, p)
req.Parameter = &v
}
func stripParam(s, p string) string {
var b strings.Builder
for _, param := range strings.Split(s, ",") {
if strings.SplitN(param, "=", 2)[0] != p {
if b.Len() > 0 {
b.WriteString(",")
}
b.WriteString(param)
}
}
return b.String()
}
// RunPlugin runs a protoc plugin named "protoc-gen-$plugin"
// and returns the generated CodeGeneratorResponse or an error.
// Supply a non-nil stderr to override stderr on the called plugin.
func RunPlugin(plugin string, req *pluginpb.CodeGeneratorRequest, stderr io.Writer) (*pluginpb.CodeGeneratorResponse, error) {
if stderr == nil {
stderr = os.Stderr
}
// Marshal the CodeGeneratorRequest.
b, err := proto.Marshal(req)
if err != nil {
return nil, err
}
// Call the plugin with the modified CodeGeneratorRequest.
var buf bytes.Buffer
cmd := exec.Command("protoc-gen-" + plugin)
cmd.Stdin = bytes.NewReader(b)
cmd.Stdout = &buf
cmd.Stderr = stderr
err = cmd.Run()
if err != nil {
return nil, err
}
// Read the CodeGeneratorResponse.
var res pluginpb.CodeGeneratorResponse
err = proto.Unmarshal(buf.Bytes(), &res)
if err != nil {
return nil, err
}
return &res, nil
}
// Write marshals and writes CodeGeneratorResponse res to w.
func Write(res *pluginpb.CodeGeneratorResponse, w io.Writer) error {
out, err := proto.Marshal(res)
if err != nil {
return err
}
_, err = w.Write(out)
return err
}