-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotobuf.go
185 lines (150 loc) · 5.56 KB
/
protobuf.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
package mage
import (
"context"
"os"
"strings"
"github.com/dosquad/mage/dyndep"
"github.com/dosquad/mage/helper"
"github.com/magefile/mage/mg"
"github.com/princjef/mageutil/bintool"
)
// Protobuf namespace is defined to group Protocol buffer functions.
type Protobuf mg.Namespace
// installProtoc install protoc command.
func (Protobuf) installProtoc(_ context.Context) error {
return helper.BinProtoc().Ensure()
}
// installProtocGenGo install protoc-gen-go command.
func (Protobuf) installProtocGenGo(_ context.Context) error {
return helper.BinProtocGenGo().Ensure()
}
// installProtocGenGoGRPC install protoc-gen-go-grpc command.
func (Protobuf) installProtocGenGoGRPC(_ context.Context) error {
return helper.BinProtocGenGoGRPC().Ensure()
}
// installProtocGenGoTwirp install protoc-gen-go-twirp command.
func (Protobuf) installProtocGenGoTwirp(_ context.Context) error {
return helper.BinProtocGenGoTwirp().Ensure()
}
func (Protobuf) installProtocGenGoConnect(_ context.Context) error {
return helper.BinProtocGenGoConnect().Ensure()
}
// Generate install and generate golang Protocol Buffer files.
func (Protobuf) Generate(ctx context.Context) {
dyndep.CtxDeps(ctx, dyndep.Protobuf)
mg.CtxDeps(ctx, Protobuf.installProtoc)
mg.CtxDeps(ctx, Protobuf.installProtocGenGo)
mg.CtxDeps(ctx, Protobuf.installProtocGenGoGRPC)
mg.CtxDeps(ctx, Protobuf.GenGo)
mg.CtxDeps(ctx, Protobuf.GenGoGRPC)
}
// GenerateWithTwirp install and generate golang Protocol Buffer files (including Twirp).
func (Protobuf) GenerateWithTwirp(ctx context.Context) {
dyndep.CtxDeps(ctx, dyndep.Protobuf)
mg.CtxDeps(ctx, Protobuf.installProtoc)
mg.CtxDeps(ctx, Protobuf.installProtocGenGo)
mg.CtxDeps(ctx, Protobuf.installProtocGenGoGRPC)
mg.CtxDeps(ctx, Protobuf.installProtocGenGoTwirp)
mg.CtxDeps(ctx, Protobuf.GenGo)
mg.CtxDeps(ctx, Protobuf.GenGoGRPC)
mg.CtxDeps(ctx, Protobuf.GenGoTwirp)
}
// GenerateWithConnect install and generate golang Protocol Buffer files (including ConnectRPC).
func (Protobuf) GenerateWithConnect(ctx context.Context) {
dyndep.CtxDeps(ctx, dyndep.Protobuf)
mg.CtxDeps(ctx, Protobuf.installProtoc)
mg.CtxDeps(ctx, Protobuf.installProtocGenGo)
mg.CtxDeps(ctx, Protobuf.installProtocGenGoGRPC)
mg.CtxDeps(ctx, Protobuf.installProtocGenGoConnect)
mg.CtxDeps(ctx, Protobuf.GenGo)
mg.CtxDeps(ctx, Protobuf.GenGoGRPC)
mg.CtxDeps(ctx, Protobuf.GenGoConnect)
}
func runProtoCommand(cmd *bintool.BinTool, args []string) error {
origPath := os.Getenv("PATH")
defer func() { os.Setenv("PATH", origPath) }()
os.Setenv("PATH", helper.MustGetProtobufPath()+":"+origPath)
// loga.PrintInfo("runProtoCommand: PATH=%s", os.Getenv("PATH"))
return cmd.Command(strings.Join(args, " ")).Run()
}
// ProtobufGenGo run protoc-gen-go to generate code.
func (Protobuf) GenGo(ctx context.Context) error {
dyndep.CtxDeps(ctx, dyndep.Protobuf)
mg.CtxDeps(ctx, Protobuf.installProtoc)
mg.CtxDeps(ctx, Protobuf.installProtocGenGo)
return protobufGen(ctx, []string{
"--proto_path=" + helper.MustGetArtifactPath("protobuf", "include"),
"--go_opt=module=" + helper.Must[string](helper.GetModuleName()),
"--go_out=.",
})
}
// GenGoGRPC run protoc-gen-go-grpc to generate code.
func (Protobuf) GenGoGRPC(ctx context.Context) error {
dyndep.CtxDeps(ctx, dyndep.Protobuf)
mg.CtxDeps(ctx, Protobuf.installProtoc)
mg.CtxDeps(ctx, Protobuf.installProtocGenGoGRPC)
return protobufGen(ctx, []string{
"--proto_path=" + helper.MustGetArtifactPath("protobuf", "include"),
"--go-grpc_opt=module=" + helper.Must[string](helper.GetModuleName()),
"--go-grpc_out=.",
"--go-grpc_opt=require_unimplemented_servers=false",
})
}
// GenGoTwirp run protoc-gen-go-twirp to generate code.
func (Protobuf) GenGoTwirp(ctx context.Context) error {
dyndep.CtxDeps(ctx, dyndep.Protobuf)
mg.CtxDeps(ctx, Protobuf.installProtoc)
mg.CtxDeps(ctx, Protobuf.installProtocGenGo)
mg.CtxDeps(ctx, Protobuf.installProtocGenGoTwirp)
return protobufGen(ctx, []string{
"--proto_path=" + helper.MustGetArtifactPath("protobuf", "include"),
"--go_opt=module=" + helper.Must[string](helper.GetModuleName()),
"--go_out=.",
"--twirp_opt=module=" + helper.Must[string](helper.GetModuleName()),
"--twirp_out=.",
})
}
// GenGoConnect run protoc-gen-connect-go to generate code.
func (Protobuf) GenGoConnect(ctx context.Context) error {
dyndep.CtxDeps(ctx, dyndep.Protobuf)
mg.CtxDeps(ctx, Protobuf.installProtoc)
mg.CtxDeps(ctx, Protobuf.installProtocGenGo)
mg.CtxDeps(ctx, Protobuf.installProtocGenGoConnect)
return protobufGen(ctx, []string{
"--proto_path=" + helper.MustGetArtifactPath("protobuf", "include"),
"--go_opt=module=" + helper.Must[string](helper.GetModuleName()),
"--go_out=.",
"--connect-go_opt=module=" + helper.Must[string](helper.GetModuleName()),
"--connect-go_out=.",
})
}
func protobufGen(_ context.Context, coreArgs []string) error {
// mg.CtxDeps(ctx, Protobuf.installProtoc)
// mg.CtxDeps(ctx, Protobuf.installProtocGenGoGRPC)
// var moduleName string
// {
// var err error
// moduleName, err = helper.GetModuleName()
// if err != nil {
// return err
// }
// }
// coreArgs := []string{
// "--proto_path=" + helper.MustGetArtifactPath("protobuf", "include"),
// "--go-grpc_opt=module=" + moduleName,
// "--go-grpc_out=.",
// "--go-grpc_opt=require_unimplemented_servers=false",
// }
protobufPaths := helper.ProtobufIncludePaths()
for _, protoPathFunc := range helper.ProtobufTargets() {
if err := runProtoCommand(helper.BinProtoc(),
append(
append(coreArgs, protobufPaths...),
" "+strings.Join(protoPathFunc(), " "),
),
); err != nil {
return err
}
}
return nil
}