Browse Source

重构CreateTemp

main
Leonard Wang 5 years ago
parent
commit
425da3c73f
  1. 39
      server/resource/template/te/api.go.tpl
  2. 8
      server/resource/template/te/model.go.tpl
  3. 14
      server/resource/template/te/router.go.tpl
  4. 12
      server/resource/template/te/service.go.tpl
  5. 189
      server/service/sys_auto_code.go
  6. 2
      server/utils/directory.go

39
server/resource/template/te/api.go.tpl

@ -2,15 +2,14 @@ package v1
import ( import (
"fmt" "fmt"
"gin-vue-admin/global/response"
"gin-vue-admin/model"
"gin-vue-admin/model/request"
resp "gin-vue-admin/model/response"
"gin-vue-admin/service"
"github.com/gin-gonic/gin"
"gin-vue-admin/global/response"
"gin-vue-admin/model"
"gin-vue-admin/model/request"
resp "gin-vue-admin/model/response"
"gin-vue-admin/service"
"github.com/gin-gonic/gin"
) )
// @Tags {{.StructName}} // @Tags {{.StructName}}
// @Summary 创建{{.StructName}} // @Summary 创建{{.StructName}}
// @Security ApiKeyAuth // @Security ApiKeyAuth
@ -30,7 +29,6 @@ func Create{{.StructName}}(c *gin.Context) {
} }
} }
// @Tags {{.StructName}} // @Tags {{.StructName}}
// @Summary 删除{{.StructName}} // @Summary 删除{{.StructName}}
// @Security ApiKeyAuth // @Security ApiKeyAuth
@ -50,7 +48,6 @@ func Delete{{.StructName}}(c *gin.Context) {
} }
} }
// @Tags {{.StructName}} // @Tags {{.StructName}}
// @Summary 更新{{.StructName}} // @Summary 更新{{.StructName}}
// @Security ApiKeyAuth // @Security ApiKeyAuth
@ -70,7 +67,6 @@ func Update{{.StructName}}(c *gin.Context) {
} }
} }
// @Tags {{.StructName}} // @Tags {{.StructName}}
// @Summary 用id查询{{.StructName}} // @Summary 用id查询{{.StructName}}
// @Security ApiKeyAuth // @Security ApiKeyAuth
@ -82,15 +78,14 @@ func Update{{.StructName}}(c *gin.Context) {
func Find{{.StructName}}(c *gin.Context) { func Find{{.StructName}}(c *gin.Context) {
var {{.Abbreviation}} model.{{.StructName}} var {{.Abbreviation}} model.{{.StructName}}
_ = c.ShouldBindQuery(&{{.Abbreviation}}) _ = c.ShouldBindQuery(&{{.Abbreviation}})
err,re{{.Abbreviation}} := service.Get{{.StructName}}({{.Abbreviation}}.ID)
err, re{{.Abbreviation}} := service.Get{{.StructName}}({{.Abbreviation}}.ID)
if err != nil { if err != nil {
response.FailWithMessage(fmt.Sprintf("查询失败,%v", err), c)
response.FailWithMessage(fmt.Sprintf("查询失败,%v", err), c)
} else { } else {
response.OkWithData( gin.H{"re{{.Abbreviation}}":re{{.Abbreviation}},}, c)
response.OkWithData(gin.H{"re{{.Abbreviation}}": re{{.Abbreviation}}}, c)
} }
} }
// @Tags {{.StructName}} // @Tags {{.StructName}}
// @Summary 分页获取{{.StructName}}列表 // @Summary 分页获取{{.StructName}}列表
// @Security ApiKeyAuth // @Security ApiKeyAuth
@ -104,13 +99,13 @@ func Get{{.StructName}}List(c *gin.Context) {
_ = c.ShouldBindQuery(&pageInfo) _ = c.ShouldBindQuery(&pageInfo)
err, list, total := service.Get{{.StructName}}InfoList(pageInfo) err, list, total := service.Get{{.StructName}}InfoList(pageInfo)
if err != nil { if err != nil {
response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
} else { } else {
response.OkWithData(resp.PageResult{
List: list,
Total: total,
Page: pageInfo.Page,
PageSize: pageInfo.PageSize,
}, c)
response.OkWithData(resp.PageResult{
List: list,
Total: total,
Page: pageInfo.Page,
PageSize: pageInfo.PageSize,
}, c)
} }
}
}

8
server/resource/template/te/model.go.tpl

@ -2,10 +2,10 @@
package model package model
import ( import (
"github.com/jinzhu/gorm"
"github.com/jinzhu/gorm"
) )
type {{.StructName}} struct { type {{.StructName}} struct {
gorm.Model {{range .Fields}}
{{.FieldName}} {{.FieldType}} `json:"{{.FieldJson}}"{{if .ColumnName}} gorm:"column:{{.ColumnName}}"{{end}}`{{ end }}
}
gorm.Model{{range .Fields}}
{{.FieldName}} {{.FieldType}} `json:"{{.FieldJson}}"{{if .ColumnName}} gorm:"column:{{.ColumnName}}"{{end}}`{{ end }}
}

14
server/resource/template/te/router.go.tpl

@ -2,17 +2,17 @@ package router
import ( import (
"gin-vue-admin/api/v1" "gin-vue-admin/api/v1"
"gin-vue-admin/middleware"
"github.com/gin-gonic/gin"
"gin-vue-admin/middleware"
"github.com/gin-gonic/gin"
) )
func Init{{.StructName}}Router(Router *gin.RouterGroup) { func Init{{.StructName}}Router(Router *gin.RouterGroup) {
{{.StructName}}Router := Router.Group("{{.Abbreviation}}").Use(middleware.JWTAuth()).Use(middleware.CasbinHandler()) {{.StructName}}Router := Router.Group("{{.Abbreviation}}").Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
{ {
{{.StructName}}Router.POST("create{{.StructName}}", v1.Create{{.StructName}}) // 新建{{.StructName}}
{{.StructName}}Router.DELETE("delete{{.StructName}}", v1.Delete{{.StructName}}) //删除{{.StructName}}
{{.StructName}}Router.PUT("update{{.StructName}}", v1.Update{{.StructName}}) //更新{{.StructName}}
{{.StructName}}Router.GET("find{{.StructName}}", v1.Find{{.StructName}}) // 根据ID获取{{.StructName}}
{{.StructName}}Router.GET("get{{.StructName}}List", v1.Get{{.StructName}}List) //获取{{.StructName}}列表
{{.StructName}}Router.POST("create{{.StructName}}", v1.Create{{.StructName}}) // 新建{{.StructName}}
{{.StructName}}Router.DELETE("delete{{.StructName}}", v1.Delete{{.StructName}}) // 删除{{.StructName}}
{{.StructName}}Router.PUT("update{{.StructName}}", v1.Update{{.StructName}}) // 更新{{.StructName}}
{{.StructName}}Router.GET("find{{.StructName}}", v1.Find{{.StructName}}) // 根据ID获取{{.StructName}}
{{.StructName}}Router.GET("get{{.StructName}}List", v1.Get{{.StructName}}List) // 获取{{.StructName}}列表
} }
} }

12
server/resource/template/te/service.go.tpl

@ -59,10 +59,10 @@ func Get{{.StructName}}(id uint) (err error, {{.Abbreviation}} model.{{.StructNa
func Get{{.StructName}}InfoList(info request.PageInfo) (err error, list interface{}, total int) { func Get{{.StructName}}InfoList(info request.PageInfo) (err error, list interface{}, total int) {
limit := info.PageSize limit := info.PageSize
offset := info.PageSize * (info.Page - 1)
db := global.GVA_DB
var {{.Abbreviation}}s []model.{{.StructName}}
err = db.Find(&{{.Abbreviation}}s).Count(&total).Error
err = db.Limit(limit).Offset(offset).Find(&{{.Abbreviation}}s).Error
return err,{{.Abbreviation}}s, total
offset := info.PageSize * (info.Page - 1)
db := global.GVA_DB
var {{.Abbreviation}}s []model.{{.StructName}}
err = db.Find(&{{.Abbreviation}}s).Count(&total).Error
err = db.Limit(limit).Offset(offset).Find(&{{.Abbreviation}}s).Error
return err, {{.Abbreviation}}s, total
} }

189
server/service/sys_auto_code.go

@ -3,10 +3,18 @@ package service
import ( import (
"gin-vue-admin/model" "gin-vue-admin/model"
"gin-vue-admin/utils" "gin-vue-admin/utils"
"io/ioutil"
"os" "os"
"strings"
"text/template" "text/template"
) )
type tplData struct {
template *template.Template
locationPath string
autoCodePath string
}
// @title CreateTemp // @title CreateTemp
// @description 函数的详细描述 // @description 函数的详细描述
// @auth (2020/04/05 20:22) // @auth (2020/04/05 20:22)
@ -14,137 +22,96 @@ import (
// @return err error // @return err error
func CreateTemp(autoCode model.AutoCodeStruct) (err error) { func CreateTemp(autoCode model.AutoCodeStruct) (err error) {
basePath := "./resource/template"
modelTmpl, err := template.ParseFiles(basePath + "/te/model.go.tpl")
if err != nil {
return err
}
apiTmpl, err := template.ParseFiles(basePath + "/te/api.go.tpl")
if err != nil {
return err
}
routerTmpl, err := template.ParseFiles(basePath + "/te/router.go.tpl")
if err != nil {
return err
}
serviceTmpl, err := template.ParseFiles(basePath + "/te/service.go.tpl")
if err != nil {
return err
}
feapiTmpl, err := template.ParseFiles(basePath + "/fe/api.js.tpl")
if err != nil {
return err
}
feTableTmpl, err := template.ParseFiles(basePath + "/fe/table.vue.tpl")
if err != nil {
return err
}
readmeTmpl, err := template.ParseFiles(basePath + "/readme.txt.tpl")
if err != nil {
return err
}
// 自动化总目录
_autoCode := "./autoCode/"
// 自动化后台代码目录
_te := "./autoCode/te/"
_dir := _te + autoCode.PackageName
_modeldir := _te + autoCode.PackageName + "/model"
_apidir := _te + autoCode.PackageName + "/api"
_routerdir := _te + autoCode.PackageName + "/router"
_servicedir := _te + autoCode.PackageName + "/service"
// 自动化前台代码目录
_fe := "./autoCode/fe/"
_fe_dir := _fe + autoCode.PackageName
_fe_apidir := _fe + autoCode.PackageName + "/api"
_fe_tabledir := _fe + autoCode.PackageName + "/table"
err = utils.CreateDir(_autoCode, _te, _dir, _modeldir, _apidir, _routerdir, _servicedir, _fe, _fe_dir, _fe_apidir, _fe_tabledir)
if err != nil {
return err
}
model, err := os.OpenFile(_te+autoCode.PackageName+"/model/"+autoCode.PackageName+".go", os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
return err
}
api, err := os.OpenFile(_te+autoCode.PackageName+"/api/"+autoCode.PackageName+".go", os.O_CREATE|os.O_WRONLY, 0755)
basePath := "resource/template"
// 获取 basePath 文件夹下所有tpl文件
tplFileList, err := GetAllTplFile(basePath, nil)
if err != nil { if err != nil {
return err return err
} }
router, err := os.OpenFile(_te+autoCode.PackageName+"/router/"+autoCode.PackageName+".go", os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
return err
dataList := make([]tplData, 0, len(tplFileList))
fileList := make([]string, 0, len(tplFileList))
needMkdir := make([]string, 0, len(tplFileList)) // 当文件夹下存在多个tpl文件时,改为map更合理
// 根据文件路径生成 tplData 结构体,待填充数据
for _, value := range tplFileList {
dataList = append(dataList, tplData{locationPath: value})
} }
service, err := os.OpenFile(_te+autoCode.PackageName+"/service/"+autoCode.PackageName+".go", os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
return err
}
feapi, err := os.OpenFile(_fe+autoCode.PackageName+"/api/"+autoCode.PackageName+".js", os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
return err
}
fetable, err := os.OpenFile(_fe+autoCode.PackageName+"/table/"+autoCode.PackageName+".vue", os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
return err
}
readme, err := os.OpenFile(_autoCode+"readme.txt", os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
return err
}
// 生成代码
{
err = modelTmpl.Execute(model, autoCode)
if err != nil {
return err
}
err = apiTmpl.Execute(api, autoCode)
// 生成 *Template, 填充 template 字段
for index, value := range dataList {
dataList[index].template, err = template.ParseFiles(value.locationPath)
if err != nil { if err != nil {
return err return err
} }
err = routerTmpl.Execute(router, autoCode)
if err != nil {
return err
}
// 生成文件路径,填充 autoCodePath 字段,readme.txt.tpl不符合规则,需要特殊处理
// resource/template/fe/api.js.tpl -> autoCode/fe/autoCode.PackageName/api/autoCode.PackageName.js
// resource/template/readme.txt.tpl -> autoCode/readme.txt
autoPath := "autoCode/"
for index, value := range dataList {
trimBase := strings.TrimPrefix(value.locationPath, basePath+"/")
if trimBase == "readme.txt.tpl" {
dataList[index].autoCodePath = autoPath + "readme.txt"
continue
} }
err = serviceTmpl.Execute(service, autoCode)
if err != nil {
return err
if lastSeparator := strings.LastIndex(trimBase, "/"); lastSeparator != -1 {
origFileName := strings.TrimSuffix(trimBase[lastSeparator+1:], ".tpl")
firstDot := strings.Index(origFileName, ".")
if firstDot != -1 {
dataList[index].autoCodePath = autoPath + trimBase[:lastSeparator] + "/" + autoCode.PackageName + "/" +
origFileName[:firstDot] + "/" + autoCode.PackageName + origFileName[firstDot:]
}
} }
err = feapiTmpl.Execute(feapi, autoCode)
if err != nil {
return err
if lastSeparator := strings.LastIndex(dataList[index].autoCodePath, "/"); lastSeparator != -1 {
needMkdir = append(needMkdir, dataList[index].autoCodePath[:lastSeparator])
} }
err = feTableTmpl.Execute(fetable, autoCode)
}
// 写入文件前,先创建文件夹
if err = utils.CreateDir(needMkdir...); err != nil {
return err
}
// 生成文件
for _, value := range dataList {
fileList = append(fileList, value.autoCodePath)
f, err := os.OpenFile(value.autoCodePath, os.O_CREATE|os.O_WRONLY, 0755)
if err != nil { if err != nil {
return err return err
} }
err = readmeTmpl.Execute(readme, autoCode)
if err != nil {
if err = value.template.Execute(f, autoCode); err != nil {
return err return err
} }
_ = f.Close()
} }
_ = model.Close()
_ = api.Close()
_ = router.Close()
_ = service.Close()
_ = feapi.Close()
_ = fetable.Close()
_ = readme.Close()
fileList := []string{
_te + autoCode.PackageName + "/model/" + autoCode.PackageName + ".go",
_te + autoCode.PackageName + "/api/" + autoCode.PackageName + ".go",
_te + autoCode.PackageName + "/router/" + autoCode.PackageName + ".go",
_te + autoCode.PackageName + "/service/" + autoCode.PackageName + ".go",
_fe + autoCode.PackageName + "/api/" + autoCode.PackageName + ".js",
_fe + autoCode.PackageName + "/table/" + autoCode.PackageName + ".vue",
_autoCode + "readme.txt",
}
err = utils.ZipFiles("./ginvueadmin.zip", fileList, ".", ".")
if err != nil {
// 生成压缩包
if err := utils.ZipFiles("./ginvueadmin.zip", fileList, ".", "."); err != nil {
return err return err
} }
err = os.RemoveAll(_autoCode)
if err != nil {
// 移除中间文件
if err := os.RemoveAll(autoPath); err != nil {
return err return err
} }
return nil return nil
} }
// GetAllTplFile 用来获取 pathName 文件夹下所有 tpl 文件
func GetAllTplFile(pathName string, fileList []string) ([]string, error) {
files, err := ioutil.ReadDir(pathName)
for _, fi := range files {
if fi.IsDir() {
fileList, err = GetAllTplFile(pathName+"/"+fi.Name(), fileList)
if err != nil {
return nil, err
}
} else {
if strings.HasSuffix(fi.Name(), ".tpl") {
fileList = append(fileList, pathName+"/"+fi.Name())
}
}
}
return fileList, err
}

2
server/utils/directory.go

@ -40,7 +40,7 @@ func CreateDir(dirs ...string) (err error) {
} else { } else {
// log.L.Info(fmt.Sprintf("no dir![%v]\n"+_dir)) // log.L.Info(fmt.Sprintf("no dir![%v]\n"+_dir))
// 创建文件夹 // 创建文件夹
err = os.Mkdir(v, os.ModePerm)
err = os.MkdirAll(v, os.ModePerm)
if err != nil { if err != nil {
// log.L.Error(fmt.Sprintf("mkdir error![%v]\n",err)) // log.L.Error(fmt.Sprintf("mkdir error![%v]\n",err))
} else { } else {

Loading…
Cancel
Save