/
build.go
73 lines (68 loc) · 2.03 KB
/
build.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
package gen
import (
"github.com/carlos-yuan/cargen/util/doc"
"strings"
"time"
"github.com/carlos-yuan/cargen/enum"
openapi "github.com/carlos-yuan/cargen/open_api"
"github.com/carlos-yuan/cargen/util/fileUtil"
)
type Config struct {
Gen string //生成类型
Path string //基础项目路径
Name string //服务名
Des string //描述
Version string //版本
DbDsn string //数据库Dsn
Tables string //数据表 ,隔开
DbName string //库别名 避免名称过长或者库名差异
DictTable string //字典表名
DictType string //字典类型字段名
DictName string //字典名称字段名
DictLabel string //字典标签字段名
DictValue string //字典值字段名
Out string //输出目录
}
const (
GenGrpc = "grpc"
GenDB = "database"
GenDoc = "doc"
GenRouter = "router"
GenEnum = "enum"
GenConfig = "config"
)
func (c Config) Build() {
start := time.Now().UnixMilli()
if c.Path != "" {
c.Path = fileUtil.FixPathSeparator(c.Path)
}
if c.Out != "" {
c.Out = fileUtil.FixPathSeparator(c.Out)
}
switch c.Gen {
case GenGrpc:
projectPath := c.Path + "/biz/" + c.Name
if c.Name != "" {
ModelToProtobuf(c.Path+"/biz", c.Name, "/pb"+c.Name, c.Path+"/orm/"+c.DbName+"/model", "model")
KitexGen(c.Name, c.Path)
CarGen(c.Name, c.DbName, projectPath+"/rpc/kitex_gen/pb"+c.Name, "pb"+c.Name, projectPath+"/service/", "service")
}
case GenRouter:
CreateApiRouter(c.Path)
case GenDB:
GormGen(c.Path, c.DbDsn, c.DbName, strings.Split(c.Tables, ","))
if c.DictTable != "" { //检测是否传入字典表
enum.GenEnum(c.Path+"/orm/"+c.DbName, c.DictTable, c.DictType, c.DictName, c.DictLabel, c.DictValue, c.DbDsn)
}
case GenDoc:
openapi.GenFromPath(c.Name, c.Des, c.Version, c.Path, c.Out)
case GenEnum:
enum.GenEnum(c.Path, c.DictTable, c.DictType, c.DictName, c.DictLabel, c.DictValue, c.DbDsn)
case GenConfig:
ConfigGen(c.Path, c.Name, c.Out)
}
if c.Path != "" {
doc.GoFmt(c.Path)
}
println("Generation time:", time.Now().UnixMilli()-start)
}