forked from hashgraph/hedera-protobufs-go
/
main.go
244 lines (197 loc) · 6.74 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
package main
import (
"errors"
"fmt"
"io/fs"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
)
func main() {
// which module are we building
moduleName := os.Args[1]
// get project directory
// nolint:dogsled
_, buildFilename, _, _ := runtime.Caller(0)
projectDir := path.Join(buildFilename, "../../../..")
// remove all existing files
removeAllWithExt(projectDir, moduleName, ".pb.go")
// invoke the build command for this module
switch moduleName {
case "services":
buildServices(projectDir)
case "mirror":
buildMirror(projectDir)
case "streams":
buildStreams(projectDir)
case "sdk":
buildSdk(projectDir)
}
}
func buildServices(dir string) {
// collect all files from proto/services/_
var servicesProtoFiles []string
var servicesModuleDecls []string
err := filepath.Walk(path.Join(dir, "proto/services"), func(filename string, info fs.FileInfo, err error) error {
if !strings.HasSuffix(filename, ".proto") {
return nil
}
pathFromRoot := strings.TrimPrefix(filename, dir+"/")
pathBase := path.Base(filename)
servicesProtoFiles = append(servicesProtoFiles, pathFromRoot)
// the -M argument generation is what allows us to avoid
// requiring "option go_package"
servicesModuleDecls = append(servicesModuleDecls,
fmt.Sprintf("--go_opt=M%v=github.com/CaioLuColaco/hedera-protobufs-local/services", pathBase),
fmt.Sprintf("--go-grpc_opt=M%v=github.com/CaioLuColaco/hedera-protobufs-local/services", pathBase),
)
return nil
})
if err != nil {
panic(err)
}
// generate proto files for services code
cmdArguments := []string{
"--go_out=services/",
"--go_opt=paths=source_relative",
"--go-grpc_out=services/",
"--go-grpc_opt=paths=source_relative",
"-Iproto/services",
}
cmdArguments = append(cmdArguments, servicesModuleDecls...)
cmdArguments = append(cmdArguments, servicesProtoFiles...)
cmd := exec.Command("protoc", cmdArguments...)
cmd.Dir = dir
mustRunCommand(cmd)
renamePackageDeclGrpcFiles(dir, "proto", "services")
}
func buildMirror(dir string) {
cmd := exec.Command("protoc",
"--go_out=./",
"--go_opt=Mbasic_types.proto=github.com/CaioLuColaco/hedera-protobufs-local/services",
"--go_opt=Mtimestamp.proto=github.com/CaioLuColaco/hedera-protobufs-local/services",
"--go_opt=Mconsensus_submit_message.proto=github.com/CaioLuColaco/hedera-protobufs-local/services",
"--go_opt=Mmirror/consensus_service.proto=github.com/CaioLuColaco/hedera-protobufs-local/mirror",
"--go_opt=Mmirror/mirror_network_service.proto=github.com/CaioLuColaco/hedera-protobufs-local/mirror",
"--go_opt=paths=source_relative",
"--go-grpc_out=./",
"--go-grpc_opt=Mbasic_types.proto=github.com/CaioLuColaco/hedera-protobufs-local/services",
"--go-grpc_opt=Mtimestamp.proto=github.com/CaioLuColaco/hedera-protobufs-local/services",
"--go-grpc_opt=Mconsensus_submit_message.proto=github.com/CaioLuColaco/hedera-protobufs-local/services",
"--go-grpc_opt=Mmirror/consensus_service.proto=github.com/CaioLuColaco/hedera-protobufs-local/mirror",
"--go-grpc_opt=Mmirror/mirror_network_service.proto=github.com/CaioLuColaco/hedera-protobufs-local/mirror",
"--go-grpc_opt=paths=source_relative",
"--proto_path=proto/",
"-Iproto/mirror",
"-Iproto/services",
"proto/mirror/consensus_service.proto",
"proto/mirror/mirror_network_service.proto",
)
cmd.Dir = dir
mustRunCommand(cmd)
renamePackageDeclGrpcFiles(dir, "com_hedera_mirror_api_proto", "mirror")
}
func buildStreams(dir string) {
cmd := exec.Command("protoc",
"--go_out=streams/",
"--go_opt=Mbasic_types.proto=github.com/CaioLuColaco/hedera-protobufs-local/services",
"--go_opt=Mtimestamp.proto=github.com/CaioLuColaco/hedera-protobufs-local/services",
"--go_opt=Maccount_balance_file.proto=github.com/CaioLuColaco/hedera-protobufs-local/streams",
"--go_opt=paths=source_relative",
"--go-grpc_out=streams/",
"--go-grpc_opt=Mbasic_types.proto=github.com/CaioLuColaco/hedera-protobufs-local/services",
"--go-grpc_opt=Mtimestamp.proto=github.com/CaioLuColaco/hedera-protobufs-local/services",
"--go-grpc_opt=Maccount_balance_file.proto=github.com/CaioLuColaco/hedera-protobufs-local/streams",
"--go-grpc_opt=paths=source_relative",
"-Iproto/streams",
"-Iproto/services",
"proto/streams/account_balance_file.proto",
)
cmd.Dir = dir
mustRunCommand(cmd)
}
func buildSdk(dir string) {
var servicesProtoFiles []string
var servicesModuleDecls []string
err := filepath.Walk(path.Join(dir, "proto/services"), func(filename string, info fs.FileInfo, err error) error {
if !strings.HasSuffix(filename, ".proto") {
return nil
}
pathFromRoot := strings.TrimPrefix(filename, dir+"/")
pathBase := path.Base(filename)
servicesProtoFiles = append(servicesProtoFiles, pathFromRoot)
servicesModuleDecls = append(servicesModuleDecls,
fmt.Sprintf("--go_opt=M%v=github.com/CaioLuColaco/hedera-protobufs-local/services", pathBase),
fmt.Sprintf("--go-grpc_opt=M%v=github.com/CaioLuColaco/hedera-protobufs-local/services", pathBase),
)
return nil
})
if err != nil {
panic(err)
}
cmdArguments := []string{
"--go_out=sdk/",
"--go_opt=Mtransaction_list.proto=github.com/CaioLuColaco/hedera-protobufs-local/sdk",
"--go_opt=paths=source_relative",
"--go-grpc_out=sdk/",
"--go-grpc_opt=Mtransaction_list.proto=github.com/CaioLuColaco/hedera-protobufs-local/sdk",
"--go-grpc_opt=paths=source_relative",
"-Iproto/sdk",
"-Iproto/services",
}
cmdArguments = append(cmdArguments, servicesModuleDecls...)
cmdArguments = append(cmdArguments, "proto/sdk/transaction_list.proto")
cmd := exec.Command("protoc", cmdArguments...)
cmd.Dir = dir
mustRunCommand(cmd)
}
func mustRunCommand(cmd *exec.Cmd) {
_, err := cmd.Output()
if err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
fmt.Print(string(exitErr.Stderr))
os.Exit(exitErr.ExitCode())
}
panic(err)
}
}
func removeAllWithExt(dir string, module string, ext string) {
err := filepath.Walk(path.Join(dir, module), func(filename string, info fs.FileInfo, err error) error {
if strings.HasSuffix(filename, ext) {
err := os.Remove(filename)
if err != nil {
return err
}
}
return nil
})
if err != nil {
panic(err)
}
}
func renamePackageDeclGrpcFiles(dir string, oldPackage string, newPackage string) {
err := filepath.Walk(path.Join(dir, newPackage), func(filename string, info fs.FileInfo, err error) error {
if strings.HasSuffix(filename, "_grpc.pb.go") {
data, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
contents := string(data)
contents = strings.Replace(contents,
fmt.Sprintf("package %s", oldPackage),
fmt.Sprintf("package %s", newPackage),
1,
)
return ioutil.WriteFile(filename, []byte(contents), info.Mode())
}
return nil
})
if err != nil {
panic(err)
}
}