forked from containerd/protobuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protoc.go
116 lines (103 loc) · 3.08 KB
/
protoc.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
package main
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"text/template"
)
var (
tmpl = template.Must(template.New("protoc").Parse(`protoc -I
{{- range $index, $include := .Includes -}}
{{if $index}}` + string(filepath.ListSeparator) + `{{end -}}
{{.}}
{{- end -}}
{{- if .Descriptors}} --include_imports --descriptor_set_out={{.Descriptors}}{{- end }} --
{{- .Name -}}_out={{if .Plugins}}plugins={{- range $index, $plugin := .Plugins -}}
{{- if $index}}+{{end}}
{{- $plugin}}
{{- end -}}
,{{- end -}}import_path={{.ImportPath}}
{{- range $proto, $gopkg := .PackageMap -}},M
{{- $proto}}={{$gopkg -}}
{{- end -}}
:{{- .OutputDir }}
{{- range .Files}} {{.}}{{end -}}
`))
tmplV2 = template.Must(template.New("protoc").Parse(`protoc -I
{{- range $index, $include := .Includes -}}
{{if $index}}` + string(filepath.ListSeparator) + `{{end -}}
{{.}}
{{- end -}}{{$importPath := .ImportPath}}{{$packageMap := .PackageMap}}{{$outputDir := .OutputDir}}
{{- if .Descriptors}} --include_imports --descriptor_set_out={{.Descriptors}}{{- end}}{{- range $gen := .Generator }} --
{{- $gen.Name -}}_out={{if $gen.Options}}{{- range $key, $val := $gen.Options -}}
{{$key}}={{$val}},{{end}}{{end}}{{- if $gen.Plugin}}plugins={{- range $index, $plugin := $gen.Plugin -}}
{{- if $index}}+{{- end -}}
{{- $plugin.Name }}{{- if $plugin.Options -}}={{- range $key, $val := $plugin.Options -}}{{- $key -}}={{- $val -}}{{- if $key}},{{end}}{{end}}
{{end -}}{{- end -}}{{- end -}}
{{- if $gen.Plugin -}},{{- end -}}import_path={{$importPath}}
{{- range $proto, $gopkg := $packageMap -}},M
{{- $proto}}={{$gopkg -}}
{{- end -}}:{{- $outputDir }}{{- end -}}
{{- range .Files}} {{.}}{{end -}}
`))
)
// protocParams defines inputs to a protoc command string. V2
type protocCmdV2 struct {
Includes []string
Generator []Generator
Descriptors string
ImportPath string
PackageMap map[string]string
Files []string
OutputDir string
}
// protocParams defines inputs to a protoc command string.
type protocCmd struct {
Name string // backend name
Includes []string
Plugins []string
Descriptors string
ImportPath string
PackageMap map[string]string
Files []string
OutputDir string
}
func (p *protocCmdV2) mkcmd() (string, error) {
var buf bytes.Buffer
if err := tmplV2.Execute(&buf, p); err != nil {
return "", err
}
return buf.String(), nil
}
func (p *protocCmd) mkcmd() (string, error) {
var buf bytes.Buffer
if err := tmpl.Execute(&buf, p); err != nil {
return "", err
}
return buf.String(), nil
}
func (p *protocCmdV2) run() error {
arg, err := p.mkcmd()
if err != nil {
return err
}
// pass to sh -c so we don't need to re-split here.
args := []string{shArg, arg}
cmd := exec.Command(shCmd, args...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd.Run()
}
func (p *protocCmd) run() error {
arg, err := p.mkcmd()
if err != nil {
return err
}
// pass to sh -c so we don't need to re-split here.
args := []string{shArg, arg}
cmd := exec.Command(shCmd, args...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd.Run()
}