/
gen.go
112 lines (93 loc) · 2.46 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
package generator
import (
"path/filepath"
"github.com/zeromicro/go-zero/tools/goctl/rpc/parser"
"github.com/zeromicro/go-zero/tools/goctl/util/console"
"github.com/zeromicro/go-zero/tools/goctl/util/ctx"
"github.com/zeromicro/go-zero/tools/goctl/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
// 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 {
abs, err := filepath.Abs(zctx.Output)
if err != nil {
return err
}
err = pathx.MkdirIfNotExist(abs)
if err != nil {
return err
}
err = g.Prepare()
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)
if err != nil {
return err
}
err = g.GenPb(dirCtx, zctx)
if err != nil {
return err
}
err = g.GenConfig(dirCtx, proto, g.cfg)
if err != nil {
return err
}
err = g.GenSvc(dirCtx, proto, g.cfg)
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)
}
console.NewColorConsole().MarkDone()
return err
}