forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
105 lines (84 loc) · 1.83 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
package generator
import (
"path/filepath"
conf "github.com/zjz894251se/go-zero/tools/goctl/config"
"github.com/zjz894251se/go-zero/tools/goctl/rpc/parser"
"github.com/zjz894251se/go-zero/tools/goctl/util"
"github.com/zjz894251se/go-zero/tools/goctl/util/console"
"github.com/zjz894251se/go-zero/tools/goctl/util/ctx"
)
type RpcGenerator struct {
g Generator
cfg *conf.Config
}
func NewDefaultRpcGenerator(style string) (*RpcGenerator, error) {
cfg, err := conf.NewConfig(style)
if err != nil {
return nil, err
}
return NewRpcGenerator(NewDefaultGenerator(), cfg), nil
}
func NewRpcGenerator(g Generator, cfg *conf.Config) *RpcGenerator {
return &RpcGenerator{
g: g,
cfg: cfg,
}
}
func (g *RpcGenerator) Generate(src, target string, protoImportPath []string) error {
abs, err := filepath.Abs(target)
if err != nil {
return err
}
err = util.MkdirIfNotExist(abs)
if err != nil {
return err
}
err = g.g.Prepare()
if err != nil {
return err
}
projectCtx, err := ctx.Prepare(abs)
if err != nil {
return err
}
p := parser.NewDefaultProtoParser()
proto, err := p.Parse(src)
if err != nil {
return err
}
dirCtx, err := mkdir(projectCtx, proto)
if err != nil {
return err
}
err = g.g.GenEtc(dirCtx, proto, g.cfg)
if err != nil {
return err
}
err = g.g.GenPb(dirCtx, protoImportPath, proto, g.cfg)
if err != nil {
return err
}
err = g.g.GenConfig(dirCtx, proto, g.cfg)
if err != nil {
return err
}
err = g.g.GenSvc(dirCtx, proto, g.cfg)
if err != nil {
return err
}
err = g.g.GenLogic(dirCtx, proto, g.cfg)
if err != nil {
return err
}
err = g.g.GenServer(dirCtx, proto, g.cfg)
if err != nil {
return err
}
err = g.g.GenMain(dirCtx, proto, g.cfg)
if err != nil {
return err
}
err = g.g.GenCall(dirCtx, proto, g.cfg)
console.NewColorConsole().MarkDone()
return err
}