Skip to content

Commit 4147660

Browse files
author
奇淼(piexlmax
authored
Merge pull request #374 from songzhibin97/gva_gormv2_dev
提供自动化代码预览功能
2 parents 6ad13c5 + e05e16d commit 4147660

File tree

3 files changed

+155
-45
lines changed

3 files changed

+155
-45
lines changed

server/api/v1/sys_auto_code.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,54 @@
11
package v1
22

33
import (
4+
"errors"
45
"fmt"
56
"gin-vue-admin/global"
67
"gin-vue-admin/model"
78
"gin-vue-admin/model/response"
89
"gin-vue-admin/service"
910
"gin-vue-admin/utils"
10-
"github.com/gin-gonic/gin"
11-
"github.com/pkg/errors"
12-
"go.uber.org/zap"
1311
"net/url"
1412
"os"
13+
14+
"github.com/gin-gonic/gin"
15+
"go.uber.org/zap"
1516
)
1617

18+
// @Tags AutoCode
19+
// @Summary 预览创建后的代码
20+
// @Security ApiKeyAuth
21+
// @accept application/json
22+
// @Produce application/json
23+
// @Param data body model.AutoCodeStruct true "预览创建代码"
24+
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
25+
// @Router /autoCode/preview [post]
26+
func PreviewTemp(c *gin.Context) {
27+
var a model.AutoCodeStruct
28+
_ = c.ShouldBindJSON(&a)
29+
if err := utils.Verify(a, utils.AutoCodeVerify); err != nil {
30+
response.FailWithMessage(err.Error(), c)
31+
return
32+
}
33+
if a.AutoCreateApiToSql {
34+
if err := service.AutoCreateApi(&a); err != nil {
35+
global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Any("err", err))
36+
c.Writer.Header().Add("success", "false")
37+
c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
38+
return
39+
}
40+
}
41+
m, err := service.PreviewTemp(a)
42+
if err != nil {
43+
c.Writer.Header().Add("success", "false")
44+
c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
45+
} else {
46+
c.Writer.Header().Add("Content-Type", "application/json")
47+
c.Writer.Header().Add("success", "true")
48+
c.JSON(200, m)
49+
}
50+
}
51+
1752
// @Tags AutoCode
1853
// @Summary 自动代码模板
1954
// @Security ApiKeyAuth

server/router/sys_auto_code.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
func InitAutoCodeRouter(Router *gin.RouterGroup) {
99
AutoCodeRouter := Router.Group("autoCode")
1010
{
11+
AutoCodeRouter.POST("preview", v1.PreviewTemp) // 获取自动创建代码预览
1112
AutoCodeRouter.POST("createTemp", v1.CreateTemp) // 创建自动化代码
1213
AutoCodeRouter.GET("getTables", v1.GetTables) // 获取对应数据库的表
1314
AutoCodeRouter.GET("getDB", v1.GetDB) // 获取数据库

server/service/sys_auto_code.go

Lines changed: 116 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,74 +16,98 @@ import (
1616
"gorm.io/gorm"
1717
)
1818

19+
const (
20+
autoPath = "autoCode/"
21+
basePath = "resource/template"
22+
)
23+
1924
type tplData struct {
2025
template *template.Template
2126
locationPath string
2227
autoCodePath string
2328
autoMoveFilePath string
2429
}
2530

26-
//@author: [piexlmax](https://github.com/piexlmax)
27-
//@function: CreateTemp
28-
//@description: 创建代码
31+
//@author: [songzhibin97](https://github.com/songzhibin97)
32+
//@function: PreviewTemp
33+
//@description: 预览创建代码
2934
//@param: model.AutoCodeStruct
30-
//@return: error
35+
//@return: map[string]string, error
3136

32-
func CreateTemp(autoCode model.AutoCodeStruct) (err error) {
33-
basePath := "resource/template"
34-
// 获取 basePath 文件夹下所有tpl文件
35-
tplFileList, err := GetAllTplFile(basePath, nil)
37+
func PreviewTemp(autoCode model.AutoCodeStruct) (map[string]string, error) {
38+
dataList, _, needMkdir, err := getNeedList(&autoCode)
3639
if err != nil {
37-
return err
38-
}
39-
dataList := make([]tplData, 0, len(tplFileList))
40-
fileList := make([]string, 0, len(tplFileList))
41-
needMkdir := make([]string, 0, len(tplFileList)) // 当文件夹下存在多个tpl文件时,改为map更合理
42-
// 根据文件路径生成 tplData 结构体,待填充数据
43-
for _, value := range tplFileList {
44-
dataList = append(dataList, tplData{locationPath: value})
40+
return nil, err
4541
}
46-
// 生成 *Template, 填充 template 字段
47-
for index, value := range dataList {
48-
dataList[index].template, err = template.ParseFiles(value.locationPath)
49-
if err != nil {
50-
return err
51-
}
42+
43+
// 写入文件前,先创建文件夹
44+
if err = utils.CreateDir(needMkdir...); err != nil {
45+
return nil, err
5246
}
5347

54-
// 生成文件路径,填充 autoCodePath 字段,readme.txt.tpl不符合规则,需要特殊处理
55-
// resource/template/web/api.js.tpl -> autoCode/web/autoCode.PackageName/api/autoCode.PackageName.js
56-
// resource/template/readme.txt.tpl -> autoCode/readme.txt
57-
autoPath := "autoCode/"
58-
for index, value := range dataList {
59-
trimBase := strings.TrimPrefix(value.locationPath, basePath+"/")
60-
if trimBase == "readme.txt.tpl" {
61-
dataList[index].autoCodePath = autoPath + "readme.txt"
48+
// 创建map
49+
ret := make(map[string]string)
50+
51+
// 生成map
52+
for _, value := range dataList {
53+
ext := ""
54+
if ext = filepath.Ext(value.autoCodePath); ext == ".txt" {
6255
continue
6356
}
64-
65-
if lastSeparator := strings.LastIndex(trimBase, "/"); lastSeparator != -1 {
66-
origFileName := strings.TrimSuffix(trimBase[lastSeparator+1:], ".tpl")
67-
firstDot := strings.Index(origFileName, ".")
68-
if firstDot != -1 {
69-
dataList[index].autoCodePath = filepath.Join(autoPath, trimBase[:lastSeparator], autoCode.PackageName,
70-
origFileName[:firstDot], autoCode.PackageName+origFileName[firstDot:])
71-
}
57+
f, err := os.OpenFile(value.autoCodePath, os.O_CREATE|os.O_WRONLY, 0755)
58+
if err != nil {
59+
return nil, err
7260
}
73-
74-
if lastSeparator := strings.LastIndex(dataList[index].autoCodePath, string(os.PathSeparator)); lastSeparator != -1 {
75-
needMkdir = append(needMkdir, dataList[index].autoCodePath[:lastSeparator])
61+
if err = value.template.Execute(f, autoCode); err != nil {
62+
return nil, err
63+
}
64+
_ = f.Close()
65+
f, err = os.OpenFile(value.autoCodePath, os.O_CREATE|os.O_RDONLY, 0755)
66+
if err != nil {
67+
return nil, err
68+
}
69+
builder := strings.Builder{}
70+
builder.WriteString("```\n")
71+
data, err := ioutil.ReadAll(f)
72+
if err != nil {
73+
return nil, err
74+
}
75+
builder.Write(data)
76+
builder.WriteString("\n```")
77+
if ext != "" && strings.Contains(ext, ".") {
78+
builder.WriteString(strings.Replace(ext, ".", "", -1))
7679
}
80+
81+
ret[value.autoCodePath] = builder.String()
82+
_ = f.Close()
83+
7784
}
85+
defer func() { // 移除中间文件
86+
if err := os.RemoveAll(autoPath); err != nil {
87+
return
88+
}
89+
}()
90+
return ret, nil
91+
}
7892

93+
//@author: [piexlmax](https://github.com/piexlmax)
94+
//@function: CreateTemp
95+
//@description: 创建代码
96+
//@param: model.AutoCodeStruct
97+
//@return: error
98+
99+
func CreateTemp(autoCode model.AutoCodeStruct) (err error) {
100+
dataList, fileList, needMkdir, err := getNeedList(&autoCode)
101+
if err != nil {
102+
return err
103+
}
79104
// 写入文件前,先创建文件夹
80105
if err = utils.CreateDir(needMkdir...); err != nil {
81106
return err
82107
}
83108

84109
// 生成文件
85110
for _, value := range dataList {
86-
fileList = append(fileList, value.autoCodePath)
87111
f, err := os.OpenFile(value.autoCodePath, os.O_CREATE|os.O_WRONLY, 0755)
88112
if err != nil {
89113
return err
@@ -276,3 +300,53 @@ func AutoCreateApi(a *model.AutoCodeStruct) (err error) {
276300
})
277301
return err
278302
}
303+
304+
func getNeedList(autoCode *model.AutoCodeStruct) (dataList []tplData, fileList []string, needMkdir []string, err error) {
305+
// 获取 basePath 文件夹下所有tpl文件
306+
tplFileList, err := GetAllTplFile(basePath, nil)
307+
if err != nil {
308+
return nil, nil, nil, err
309+
}
310+
dataList = make([]tplData, 0, len(tplFileList))
311+
fileList = make([]string, 0, len(tplFileList))
312+
needMkdir = make([]string, 0, len(tplFileList)) // 当文件夹下存在多个tpl文件时,改为map更合理
313+
// 根据文件路径生成 tplData 结构体,待填充数据
314+
for _, value := range tplFileList {
315+
dataList = append(dataList, tplData{locationPath: value})
316+
}
317+
// 生成 *Template, 填充 template 字段
318+
for index, value := range dataList {
319+
dataList[index].template, err = template.ParseFiles(value.locationPath)
320+
if err != nil {
321+
return nil, nil, nil, err
322+
}
323+
}
324+
// 生成文件路径,填充 autoCodePath 字段,readme.txt.tpl不符合规则,需要特殊处理
325+
// resource/template/web/api.js.tpl -> autoCode/web/autoCode.PackageName/api/autoCode.PackageName.js
326+
// resource/template/readme.txt.tpl -> autoCode/readme.txt
327+
autoPath := "autoCode/"
328+
for index, value := range dataList {
329+
trimBase := strings.TrimPrefix(value.locationPath, basePath+"/")
330+
if trimBase == "readme.txt.tpl" {
331+
dataList[index].autoCodePath = autoPath + "readme.txt"
332+
continue
333+
}
334+
335+
if lastSeparator := strings.LastIndex(trimBase, "/"); lastSeparator != -1 {
336+
origFileName := strings.TrimSuffix(trimBase[lastSeparator+1:], ".tpl")
337+
firstDot := strings.Index(origFileName, ".")
338+
if firstDot != -1 {
339+
dataList[index].autoCodePath = filepath.Join(autoPath, trimBase[:lastSeparator], autoCode.PackageName,
340+
origFileName[:firstDot], autoCode.PackageName+origFileName[firstDot:])
341+
}
342+
}
343+
344+
if lastSeparator := strings.LastIndex(dataList[index].autoCodePath, string(os.PathSeparator)); lastSeparator != -1 {
345+
needMkdir = append(needMkdir, dataList[index].autoCodePath[:lastSeparator])
346+
}
347+
}
348+
for _, value := range dataList {
349+
fileList = append(fileList, value.autoCodePath)
350+
}
351+
return dataList, fileList, needMkdir, err
352+
}

0 commit comments

Comments
 (0)