feat: 初步完成"生成代码"接口

This commit is contained in:
geeknonerd
2023-04-27 08:51:00 +08:00
parent 3974a9e435
commit 7add234970
6 changed files with 106 additions and 3 deletions

View File

@@ -11,7 +11,7 @@ var GenConfig = genConfig{
// 是否去除表前缀
IsRemoveTablePrefix: true,
// 生成代码根路径
GenRootPath: "target",
GenRootPath: "/tmp/target",
}
type genConfig struct {

View File

@@ -28,6 +28,7 @@ func regGen(rg *gin.RouterGroup, group *core.GroupBase) error {
rg.POST("/editTable", handle.editTable)
rg.POST("/delTable", handle.delTable)
rg.GET("/previewCode", handle.previewCode)
rg.GET("/genCode", handle.genCode)
})
}
@@ -122,3 +123,18 @@ func (gh genHandler) previewCode(c *gin.Context) {
res, err := gh.srv.PreviewCode(previewReq.ID)
response.CheckAndRespWithData(c, res, err)
}
//genCode 生成代码
func (gh genHandler) genCode(c *gin.Context) {
var genReq req.GenCodeReq
if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &genReq)) {
return
}
for _, table := range strings.Split(genReq.Tables, ",") {
err := gh.srv.GenCode(table)
if response.IsFailWithResp(c, err) {
return
}
}
response.Ok(c)
}

View File

@@ -76,3 +76,8 @@ type DelTableReq struct {
type PreviewCodeReq struct {
ID uint `form:"id" binding:"required,gt=0"` // 主键
}
//GenCodeReq 生成代码参数
type GenCodeReq struct {
Tables string `form:"tables" binding:"required"` // 生成的表, 用","分隔
}

View File

@@ -22,9 +22,9 @@ type IGenerateService interface {
EditTable(editReq req.EditTableReq) (e error)
DelTable(ids []uint) (e error)
PreviewCode(id uint) (res map[string]string, e error)
//DownloadCode
//GenCode
GenCode(tableName string) (e error)
//GenZipCode
//DownloadCode
}
//NewGenerateService 初始化
@@ -378,3 +378,28 @@ func (genSrv generateService) PreviewCode(id uint) (res map[string]string, e err
}
return
}
//GenCode 生成代码 (自定义路径)
func (genSrv generateService) GenCode(tableName string) (e error) {
var genTable gen.GenTable
err := genSrv.db.Where("table_name = ?", tableName).Order("id desc").Limit(1).First(&genTable).Error
if e = response.CheckErrDBNotRecord(err, "记录丢失!"); e != nil {
return
}
if e = response.CheckErr(err, "GenCode First err"); e != nil {
return
}
//获取模板内容
tplCodeMap, err := genSrv.renderCodeByTable(genTable)
if e = response.CheckErr(err, "GenCode renderCodeByTable err"); e != nil {
return
}
//获取生成根路径
basePath := generator.TemplateUtil.GetGenPath(genTable)
//生成代码文件
err = generator.TemplateUtil.GenCodeFiles(tplCodeMap, genTable.ModuleName, basePath)
if e = response.CheckErr(err, "GenCode GenCodeFiles err"); e != nil {
return
}
return
}

View File

@@ -2,10 +2,13 @@ package generator
import (
"bytes"
"fmt"
"io/ioutil"
"likeadmin/config"
"likeadmin/core/response"
"likeadmin/model/gen"
"likeadmin/util"
"os"
"path"
"text/template"
)
@@ -18,6 +21,7 @@ var TemplateUtil = templateUtil{
}),
}
//sub 模板-减函数
func sub(a, b int) int {
return a - b
}
@@ -163,3 +167,49 @@ func (tu templateUtil) Render(tplPath string, tplVars TplVars) (res string, e er
}
return buf.String(), nil
}
//GetGenPath 获取生成路径
func (tu templateUtil) GetGenPath(table gen.GenTable) string {
if table.GenPath == "/" {
//return path.Join(config.Config.RootPath, config.GenConfig.GenRootPath)
return config.GenConfig.GenRootPath
}
return table.GenPath
}
//GetFilePaths 获取生成文件相对路径
func (tu templateUtil) GetFilePaths(tplCodeMap map[string]string, moduleName string) map[string]string {
//模板文件对应的输出文件
fmtMap := map[string]string{
"vue/api.ts.tpl": "vue/%s/api.ts",
"vue/edit.vue.tpl": "vue/%s/edit.vue",
"vue/index.vue.tpl": "vue/%s/index.vue",
"vue/index-tree.vue.tpl": "vue/%s/index-tree.vue",
}
filePath := make(map[string]string)
for tplPath, tplCode := range tplCodeMap {
file := fmt.Sprintf(fmtMap[tplPath], moduleName)
filePath[file] = tplCode
}
return filePath
}
//GenCodeFiles 生成代码文件
func (tu templateUtil) GenCodeFiles(tplCodeMap map[string]string, moduleName string, basePath string) error {
filePaths := tu.GetFilePaths(tplCodeMap, moduleName)
for file, tplCode := range filePaths {
filePath := path.Join(basePath, file)
dir := path.Dir(filePath)
if !util.ToolsUtil.IsFileExist(dir) {
err := os.MkdirAll(dir, 0755)
if err != nil {
return response.CheckErr(err, "TemplateUtil.GenCodeFiles MkdirAll err")
}
}
err := ioutil.WriteFile(filePath, []byte(tplCode), 0644)
if err != nil {
return response.CheckErr(err, "TemplateUtil.GenCodeFiles WriteFile err")
}
}
return nil
}

View File

@@ -8,6 +8,7 @@ import (
"likeadmin/config"
"math"
"math/rand"
"os"
"reflect"
"strconv"
"strings"
@@ -90,3 +91,9 @@ func (tu toolsUtil) ObjToJson(data interface{}) (res string, err error) {
res = string(b)
return res, nil
}
//IsFileExist 判断文件或目录是否存在
func (tu toolsUtil) IsFileExist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}