-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
260 lines (218 loc) · 5.66 KB
/
gen.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
package generator
import (
"fmt"
"os"
"path/filepath"
"github.com/gookit/color"
"github.com/zhangbao138208/goctls/extra/ent/template"
"github.com/zhangbao138208/goctls/rpc/execx"
proto2 "github.com/zhangbao138208/goctls/rpc/generator/proto"
"github.com/zhangbao138208/goctls/rpc/parser"
"github.com/zhangbao138208/goctls/util/console"
"github.com/zhangbao138208/goctls/util/ctx"
"github.com/zhangbao138208/goctls/util/pathx"
)
type ZRpcContext struct {
// Sre is the source file of the proto.
Src string
// ProtoCmd is the command to generate proto files.
ProtocCmd string
// ProtoGenGrpcDir is the directory to store the generated proto files.
ProtoGenGrpcDir string
// ProtoGenGoDir is the directory to store the generated go files.
ProtoGenGoDir string
// IsGooglePlugin is the flag to indicate whether the proto file is generated by google plugin.
IsGooglePlugin bool
// GoOutput is the output directory of the generated go files.
GoOutput string
// GrpcOutput is the output directory of the generated grpc files.
GrpcOutput string
// Output is the output directory of the generated files.
Output string
// Multiple is the flag to indicate whether the proto file is generated in multiple mode.
Multiple bool
// Schema is the ent schema path
Schema string
// Ent
Ent bool
// ModuleName is the module name in go mod
ModuleName string
// Port describes the service port exposed
Port int
// MakeFile describes whether generate makefile
MakeFile bool
// DockerFile describes whether generate dockerfile
DockerFile bool
// Gitlab describes whether to use gitlab-ci
Gitlab bool
// DescDir describes whether to create desc folder for splitting proto files
UseDescDir bool
// RpcName describes the rpc name when create new project
RpcName string
// I18n describes whether to use i18n
I18n bool
// Whether to generate rpc client
IsGenClient bool
}
// Generate generates a rpc service, through the proto file,
// code storage directory, and proto import parameters to control
// the source file and target location of the rpc service that needs to be generated
func (g *Generator) Generate(zctx *ZRpcContext) error {
color.Green.Println("Generating...")
abs, err := filepath.Abs(zctx.Output)
if err != nil {
return err
}
err = pathx.MkdirIfNotExist(abs)
if err != nil {
return err
}
// merge proto files
protoDir := filepath.Join(abs, "desc")
if pathx.Exists(protoDir) {
protoFileAbsPath, err := filepath.Abs(zctx.Src)
if err != nil {
return err
}
if err = proto2.MergeProto(&proto2.ProtoContext{
ProtoDir: protoDir,
OutputPath: protoFileAbsPath,
}); err != nil {
return err
}
}
err = g.Prepare()
if err != nil {
return err
}
if zctx.ModuleName != "" {
_, err = execx.Run("go mod init "+zctx.ModuleName, abs)
if err != nil {
return err
}
}
projectCtx, err := ctx.Prepare(abs)
if err != nil {
return err
}
p := parser.NewDefaultProtoParser()
proto, err := p.Parse(zctx.Src, zctx.Multiple)
if err != nil {
return err
}
dirCtx, err := mkdir(projectCtx, proto, g.cfg, zctx)
if err != nil {
return err
}
err = g.GenEtc(dirCtx, proto, g.cfg, zctx)
if err != nil {
return err
}
err = g.GenPb(dirCtx, zctx)
if err != nil {
return err
}
err = g.GenConfig(dirCtx, proto, g.cfg, zctx)
if err != nil {
return err
}
err = g.GenSvc(dirCtx, proto, g.cfg, zctx)
if err != nil {
return err
}
err = g.GenLogic(dirCtx, proto, g.cfg, zctx)
if err != nil {
return err
}
err = g.GenServer(dirCtx, proto, g.cfg, zctx)
if err != nil {
return err
}
err = g.GenMain(dirCtx, proto, g.cfg, zctx)
if err != nil {
return err
}
if zctx.IsGenClient {
err = g.GenCall(dirCtx, proto, g.cfg, zctx)
}
if zctx.MakeFile {
makefileCmd := fmt.Sprintf("goctls extra makefile -t %s -s %s -n %s", "rpc", g.cfg.NamingFormat, zctx.RpcName)
if zctx.I18n {
makefileCmd += " -i"
}
if zctx.Ent {
makefileCmd += " -e"
}
_, err = execx.Run(makefileCmd, abs)
if err != nil {
return err
}
}
if zctx.DockerFile {
_, err = execx.Run(fmt.Sprintf("goctls docker -p %d -s %s -t rpc", zctx.Port, zctx.RpcName), abs)
}
if zctx.Gitlab {
err = g.GenGitlab(dirCtx, proto, g.cfg, zctx)
if err != nil {
return err
}
}
if zctx.UseDescDir {
err = g.GenBaseDesc(dirCtx, proto, g.cfg, zctx)
if err != nil {
return err
}
}
// generate ent
if zctx.Ent {
_, err := execx.Run(fmt.Sprintf("go run -mod=mod entgo.io/ent/cmd/ent new %s",
dirCtx.GetServiceName().ToCamel()), abs)
if err != nil {
return err
}
_, err = execx.Run("go mod tidy", abs)
if err != nil {
return err
}
_, err = execx.Run("go run -mod=mod entgo.io/ent/cmd/ent generate ./ent/schema", abs)
if err != nil {
return err
}
err = pathx.MkdirIfNotExist(filepath.Join(abs, "ent", "template"))
if err != nil {
return err
}
paginationTplPath := filepath.Join(abs, "ent", "template", "pagination.tmpl")
notNilTplPath := filepath.Join(abs, "ent", "template", "set_not_nil.tmpl")
if !pathx.FileExists(paginationTplPath) {
err = os.WriteFile(paginationTplPath, []byte(template.PaginationTmpl), os.ModePerm)
if err != nil {
return err
}
err = os.WriteFile(notNilTplPath, []byte(template.NotNilTmpl), os.ModePerm)
if err != nil {
return err
}
}
// gen ent error handler
err = g.GenErrorHandler(dirCtx, proto, g.cfg, zctx)
if err != nil {
return err
}
// gen ent transaction util
err = g.GenEntTx(dirCtx, proto, g.cfg, zctx)
if err != nil {
return err
}
_, err = execx.Run("go mod tidy", abs)
if err != nil {
return err
}
}
_, err = execx.Run("goctls project upgrade", abs)
if err != nil {
return err
}
console.NewColorConsole().MarkDone()
return err
}