-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
220 lines (208 loc) · 8.1 KB
/
generator.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
package main
import (
"fmt"
"github.com/ericnts/code-generator/constant"
"github.com/ericnts/code-generator/db"
"github.com/ericnts/tool-box/stringx"
"io/ioutil"
"os"
"path"
"strings"
)
type Generator struct {
Project string //项目名称
ObjectName string //对象名称
TableName string //表名
Comment string //描述信息
Columns []db.ColumnInfo //数据列
fileName string //文件名
lowerObjectName string //变量名称
columnSize int //字段长度
record EntityRecord
}
func MkDir() error {
if err := os.RemoveAll(constant.DistDir); err != nil {
return err
}
dirs := []string{constant.EntityDir, constant.DaoDir, constant.ServiceDir, constant.ControllerDir, constant.VODir, constant.RouterDir}
for _, dir := range dirs {
err := os.MkdirAll(fmt.Sprintf("%s/%s", constant.DistDir, dir), os.ModePerm)
if err != nil {
return err
}
}
return nil
}
func NewGenerator(project, tableName string) (p *Generator, err error) {
p = &Generator{
Project: project,
TableName: tableName,
}
p.ObjectName = stringx.CamelName(p.TableName)
p.fileName = stringx.UnderscoreName(p.ObjectName)
p.lowerObjectName = stringx.LowerCamelName(p.fileName)
if comment, err := db.GetTableComment(p.TableName); err != nil {
return p, err
} else {
p.Comment = comment
}
if columns, err := db.FindColumn(p.TableName); err != nil {
return p, err
} else {
p.Columns = columns
p.columnSize = len(columns)
}
for i, column := range p.Columns {
p.record.CheckField(i, column.Name)
}
return p, nil
}
// 生成Entity
func (p *Generator) GenerateEntity() error {
fields := make([]string, p.columnSize+1)
var importStr string
if p.record.HasCommon() {
fields[0] = "\tcommon.Entity\n\n"
}
for i, column := range p.Columns {
if p.record.IsCommonField(i) {
continue
}
var notNull, defaultValue, comment string
if column.Nullable == "NO" {
notNull = "not null;"
}
if stringx.IsNotBlank(column.DefaultValue) {
defaultValue = "default:" + column.DefaultValue + ";"
}
if stringx.IsNotBlank(column.Comment) {
comment = "comment:" + column.Comment
}
fields[i+1] = fmt.Sprintf("\t%s\t\t%v\t`gorm:\"column:%s;%s%s%s\"`\n",
stringx.CamelName(column.Name), column.GetType(), column.Name, notNull, defaultValue, comment)
}
fieldStr := strings.Join(fields, "")
if strings.Contains(fieldStr, "time.") {
importStr += "\n\t\"time\""
}
file, err := ioutil.ReadFile(constant.EntityTmp)
if err != nil {
return err
}
str := string(file)
str = strings.Replace(str, constant.Package, path.Base(constant.EntityDir), 1)
str = strings.ReplaceAll(str, constant.Project, p.Project)
str = strings.Replace(str, constant.Import, importStr, 1)
str = strings.Replace(str, constant.Comment, p.Comment, 1)
str = strings.Replace(str, constant.Object, p.ObjectName, 2)
str = strings.Replace(str, constant.Field, fieldStr, 1)
str = strings.Replace(str, constant.Table, p.TableName, 1)
err = ioutil.WriteFile(fmt.Sprintf("%s/%s/%s_%s.go", constant.DistDir, constant.EntityDir, p.fileName, path.Base(constant.EntityDir)), []byte(str), 0644)
return nil
}
// 生成VO
func (p *Generator) GenerateVO() error {
var importStr string
fields := make([]string, p.columnSize)
queryFields := make([]string, p.columnSize)
modifyFields := make([]string, p.columnSize)
toVOs := make([]string, p.columnSize)
toEntities := make([]string, p.columnSize)
var hasTime bool
for i, column := range p.Columns {
fieldType := column.GetType()
if fieldType == constant.FTTime {
hasTime = true
}
fields[i] = fmt.Sprintf("\t%s\t\t%v\t`json:\"%s,omitempty\"`\t//%s\n", stringx.CamelName(column.Name), fieldType, stringx.LowerCamelName(column.Name), column.Comment)
toVOs[i] = fmt.Sprintf("\tv.%s = e.%s\n", stringx.CamelName(column.Name), stringx.CamelName(column.Name))
if !p.record.IsCommonField(i) {
modifyFields[i] = fmt.Sprintf("\t%s\t\t%v\t`json:\"%s\"`\t//%s\n", stringx.CamelName(column.Name), fieldType, stringx.LowerCamelName(column.Name), column.Comment)
queryFields[i] = fmt.Sprintf("\t%s\t\t%v\t`form:\"%s\"`\t//%s\n", stringx.CamelName(column.Name), fieldType, stringx.LowerCamelName(column.Name), column.Comment)
toEntities[i] = fmt.Sprintf("\te.%s = p.%s\n", stringx.CamelName(column.Name), stringx.CamelName(column.Name))
}
}
if hasTime {
importStr += "\n\t\"time\""
}
file, err := ioutil.ReadFile(constant.VOTmp)
if err != nil {
return err
}
str := string(file)
str = strings.Replace(str, constant.Package, path.Base(constant.VODir), 1)
str = strings.Replace(str, constant.Import, importStr, 1)
str = strings.ReplaceAll(str, constant.Project, p.Project)
str = strings.ReplaceAll(str, constant.Object, p.ObjectName)
str = strings.ReplaceAll(str, constant.Field, strings.Join(fields, ""))
str = strings.ReplaceAll(str, constant.QueryField, strings.Join(queryFields, ""))
str = strings.ReplaceAll(str, constant.ModifyField, strings.Join(modifyFields, ""))
str = strings.ReplaceAll(str, constant.ToVO, strings.Join(toVOs, ""))
str = strings.ReplaceAll(str, constant.ToEntity, strings.Join(toEntities, ""))
err = ioutil.WriteFile(fmt.Sprintf("%s/%s/%s_%s.go", constant.DistDir, constant.VODir, p.fileName, path.Base(constant.VODir)), []byte(str), 0644)
return err
}
// 生成DAO
func (p *Generator) GenerateDAO() error {
file, err := ioutil.ReadFile(constant.DaoTmp)
if err != nil {
return err
}
str := string(file)
str = strings.Replace(str, constant.Package, path.Base(constant.DaoDir), 1)
str = strings.ReplaceAll(str, constant.Project, p.Project)
str = strings.ReplaceAll(str, constant.Object, p.ObjectName)
str = strings.ReplaceAll(str, constant.LowerObject, p.lowerObjectName)
err = ioutil.WriteFile(fmt.Sprintf("%s/%s/%s_%s.go", constant.DistDir, constant.DaoDir, p.fileName, path.Base(constant.DaoDir)), []byte(str), 0644)
return err
}
// 生成Service
func (p *Generator) GenerateService() error {
file, err := ioutil.ReadFile(constant.ServiceTmp)
if err != nil {
return err
}
str := string(file)
str = strings.Replace(str, constant.Package, path.Base(constant.ServiceDir), 1)
str = strings.ReplaceAll(str, constant.Project, p.Project)
str = strings.ReplaceAll(str, constant.Object, p.ObjectName)
str = strings.ReplaceAll(str, constant.LowerObject, p.lowerObjectName)
err = ioutil.WriteFile(fmt.Sprintf("%s/%s/%s_%s.go", constant.DistDir, constant.ServiceDir, p.fileName, path.Base(constant.ServiceDir)), []byte(str), 0644)
return err
}
// 生成Controller
func (p *Generator) GenerateController() error {
file, err := ioutil.ReadFile(constant.ControllerTmp)
if err != nil {
return err
}
str := string(file)
str = strings.Replace(str, constant.Package, path.Base(constant.ControllerDir), 1)
str = strings.ReplaceAll(str, constant.Comment, p.Comment)
str = strings.ReplaceAll(str, constant.Project, p.Project)
str = strings.ReplaceAll(str, constant.Object, p.ObjectName)
items := strings.Split(p.fileName, "_")
if len(items) > 0 {
items[len(items)-1] = stringx.Plural(items[len(items)-1])
}
str = strings.ReplaceAll(str, constant.Router, "/"+path.Base(constant.ControllerDir)+"/"+strings.Join(items, "/"))
err = ioutil.WriteFile(fmt.Sprintf("%s/%s/%s_%s.go", constant.DistDir, constant.ControllerDir, p.fileName, path.Base(constant.ControllerDir)), []byte(str), 0644)
return err
}
// 生成Router
func (p *Generator) GenerateRouter() error {
importStr := "\tv1 \"{Project}/controller/v1\"\n\t\"github.com/gin-gonic/gin\""
file, err := ioutil.ReadFile(constant.RouterTmp)
if err != nil {
return err
}
str := string(file)
str = strings.Replace(str, constant.Package, path.Base(constant.RouterDir), 1)
str = strings.Replace(str, constant.Import, importStr, 1)
str = strings.ReplaceAll(str, constant.Comment, p.Comment)
str = strings.ReplaceAll(str, constant.Project, p.Project)
str = strings.ReplaceAll(str, constant.Object, p.ObjectName)
str = strings.ReplaceAll(str, constant.Permission, strings.ReplaceAll(p.fileName, "_", ":"))
err = ioutil.WriteFile(fmt.Sprintf("%s/%s/%s_%s.go", constant.DistDir, constant.RouterDir, p.fileName, path.Base(constant.RouterDir)), []byte(str), 0644)
return err
}