-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (150 loc) · 3.71 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
package main
import (
"flag"
"fmt"
"io"
"os"
"strings"
_ "embed"
"github.com/cuishu/functools"
"github.com/cuishu/zero-rpc/generator"
"github.com/emicklei/proto"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
var (
protoFile string
genProto bool
spec generator.Spec
//go:embed template/main.go.tpl
mainTmpl string
//go:embed template/svc/session.go.tpl
sessionTmpl string
//go:embed template/svc/svc.go.tpl
svcTmpl string
//go:embed template/server/server.go.tpl
serverTmpl string
//go:embed template/proto/client.go.tpl
clientTmpl string
//go:embed template/logic/logic.go.tpl
logicTmpl string
//go:embed template/config/config.go.tpl
configTmpl string
//go:embed template/config/config.yaml.gtpl
configFileTmpl string
//go:embed template/build.sh.gtpl
buildSHTmpl string
//go:embed template/Makefile.gtpl
makefileTmpl string
//go:embed template/Dockerfile.gtpl
dockerfileTmpl string
//go:embed template/gitignore.gtpl
gitignoreTmpl string
//go:embed template/example.proto.gtpl
exampleProtoTmpl string
//go:embed template/svc/postgres.go.tpl
postgresTmpl string
//go:embed template/svc/redis.go.tpl
redisTmpl string
)
func init() {
flag.StringVar(&protoFile, "f", "", "proto filename")
flag.BoolVar(&genProto, "proto", false, "generate example proto file")
flag.Parse()
if protoFile == "" && !genProto {
os.Exit(0)
}
spec.Template.Main = mainTmpl
spec.Template.Session = sessionTmpl
spec.Template.Svc = svcTmpl
spec.Template.Server = serverTmpl
spec.Template.Client = clientTmpl
spec.Template.Logic = logicTmpl
spec.Template.Config = configTmpl
spec.Template.ConfigFile = configFileTmpl
spec.Template.BuildSH = buildSHTmpl
spec.Template.Makefile = makefileTmpl
spec.Template.Dockerfile = dockerfileTmpl
spec.Template.GitIgnore = gitignoreTmpl
spec.Template.ExampleProtoTmpl = exampleProtoTmpl
spec.Template.Postgres = postgresTmpl
spec.Template.Redis = redisTmpl
}
func handleService(s *proto.Service) {
if s.Comment != nil {
spec.Service.Comment = strings.Join(functools.Map(func(v string) string {
return "//" + v
}, s.Comment.Lines), "\n")
}
spec.Service.Name = s.Name
}
func handlePackage(p *proto.Package) {
spec.Package = p.Name
}
func handleRPC(rpc *proto.RPC) {
var comment []string
if rpc.Comment != nil {
comment = functools.Map(func(v string) string {
return "//" + v
}, rpc.Comment.Lines)
}
spec.Service.Methods = append(spec.Service.Methods, generator.Method{
Comment: strings.Join(comment, "\n"),
Name: rpc.Name,
Param: rpc.RequestType,
Return: rpc.ReturnsType,
})
}
func mkdir() {
os.MkdirAll("config", 0755)
os.MkdirAll("logic", 0755)
os.MkdirAll("server", 0755)
os.MkdirAll("proto", 0755)
os.MkdirAll("svc", 0755)
}
func init() {
file, err := os.Open("go.mod")
if err != nil {
fmt.Println("please run `go mod init` befor do this")
os.Exit(0)
}
defer file.Close()
data, err := io.ReadAll(file)
if err != nil {
panic(err)
}
slice := strings.Split(string(data), "\n")
if len(slice) == 0 {
panic("invalid file go.mod")
}
slice = strings.Split(slice[0], " ")
if len(slice) != 2 {
panic("invalid file go.mod")
}
spec.Module.Set(slice[len(slice)-1])
spec.Service.Name = cases.Title(language.English).String(spec.Module.Short)
if genProto {
generator.GenerateProtoTemplate(&spec)
os.Exit(0)
}
}
func main() {
file, err := os.Open(protoFile)
if err != nil {
fmt.Println(err.Error())
os.Exit(-1)
}
definition, err := proto.NewParser(file).Parse()
if err != nil {
fmt.Println(err.Error())
os.Exit(-2)
}
proto.Walk(definition,
proto.WithService(handleService),
proto.WithPackage(handlePackage),
proto.WithRPC(handleRPC),
)
mkdir()
generator.GenerateProto(protoFile)
generator.GenerateCode(&spec)
}