From 5d27f133475c42ea9c49adbf55a448343395d188 Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Thu, 11 Feb 2021 19:33:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E5=87=BA=E9=87=8D=E5=A4=8D=E7=9A=84?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E4=BC=98=E5=8C=96PreviewTemp=E5=92=8CCreateT?= =?UTF-8?q?emp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/service/sys_auto_code.go | 143 +++++++++++++------------------- 1 file changed, 57 insertions(+), 86 deletions(-) diff --git a/server/service/sys_auto_code.go b/server/service/sys_auto_code.go index ac76bdc2..02d1e294 100644 --- a/server/service/sys_auto_code.go +++ b/server/service/sys_auto_code.go @@ -16,6 +16,11 @@ import ( "gorm.io/gorm" ) +const ( + autoPath = "autoCode/" + basePath = "resource/template" +) + type tplData struct { template *template.Template locationPath string @@ -30,50 +35,10 @@ type tplData struct { //@return: map[string]string, error func PreviewTemp(autoCode model.AutoCodeStruct) (map[string]string, error) { - basePath := "resource/template" - // 获取 basePath 文件夹下所有tpl文件 - tplFileList, err := GetAllTplFile(basePath, nil) + dataList, _, needMkdir, err := getNeedList(&autoCode) if err != nil { return nil, 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}) - } - // 生成 *Template, 填充 template 字段 - for index, value := range dataList { - dataList[index].template, err = template.ParseFiles(value.locationPath) - if err != nil { - return nil, err - } - } - // 生成文件路径,填充 autoCodePath 字段,readme.txt.tpl不符合规则,需要特殊处理 - // resource/template/web/api.js.tpl -> autoCode/web/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 - } - - if lastSeparator := strings.LastIndex(trimBase, "/"); lastSeparator != -1 { - origFileName := strings.TrimSuffix(trimBase[lastSeparator+1:], ".tpl") - firstDot := strings.Index(origFileName, ".") - if firstDot != -1 { - dataList[index].autoCodePath = filepath.Join(autoPath, trimBase[:lastSeparator], autoCode.PackageName, - origFileName[:firstDot], autoCode.PackageName+origFileName[firstDot:]) - } - } - - if lastSeparator := strings.LastIndex(dataList[index].autoCodePath, string(os.PathSeparator)); lastSeparator != -1 { - needMkdir = append(needMkdir, dataList[index].autoCodePath[:lastSeparator]) - } - } // 写入文件前,先创建文件夹 if err = utils.CreateDir(needMkdir...); err != nil { @@ -89,7 +54,6 @@ func PreviewTemp(autoCode model.AutoCodeStruct) (map[string]string, error) { if ext = filepath.Ext(value.autoCodePath); ext == ".txt" { continue } - fileList = append(fileList, value.autoCodePath) f, err := os.OpenFile(value.autoCodePath, os.O_CREATE|os.O_WRONLY, 0755) if err != nil { return nil, err @@ -133,52 +97,10 @@ func PreviewTemp(autoCode model.AutoCodeStruct) (map[string]string, error) { //@return: error func CreateTemp(autoCode model.AutoCodeStruct) (err error) { - basePath := "resource/template" - // 获取 basePath 文件夹下所有tpl文件 - tplFileList, err := GetAllTplFile(basePath, nil) + dataList, fileList, needMkdir, err := getNeedList(&autoCode) 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}) - } - // 生成 *Template, 填充 template 字段 - for index, value := range dataList { - dataList[index].template, err = template.ParseFiles(value.locationPath) - if err != nil { - return err - } - } - - // 生成文件路径,填充 autoCodePath 字段,readme.txt.tpl不符合规则,需要特殊处理 - // resource/template/web/api.js.tpl -> autoCode/web/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 - } - - if lastSeparator := strings.LastIndex(trimBase, "/"); lastSeparator != -1 { - origFileName := strings.TrimSuffix(trimBase[lastSeparator+1:], ".tpl") - firstDot := strings.Index(origFileName, ".") - if firstDot != -1 { - dataList[index].autoCodePath = filepath.Join(autoPath, trimBase[:lastSeparator], autoCode.PackageName, - origFileName[:firstDot], autoCode.PackageName+origFileName[firstDot:]) - } - } - - if lastSeparator := strings.LastIndex(dataList[index].autoCodePath, string(os.PathSeparator)); lastSeparator != -1 { - needMkdir = append(needMkdir, dataList[index].autoCodePath[:lastSeparator]) - } - } - // 写入文件前,先创建文件夹 if err = utils.CreateDir(needMkdir...); err != nil { return err @@ -186,7 +108,6 @@ func CreateTemp(autoCode model.AutoCodeStruct) (err error) { // 生成文件 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 { return err @@ -379,3 +300,53 @@ func AutoCreateApi(a *model.AutoCodeStruct) (err error) { }) return err } + +func getNeedList(autoCode *model.AutoCodeStruct) (dataList []tplData, fileList []string, needMkdir []string, err error) { + // 获取 basePath 文件夹下所有tpl文件 + tplFileList, err := GetAllTplFile(basePath, nil) + if err != nil { + return nil, nil, nil, 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}) + } + // 生成 *Template, 填充 template 字段 + for index, value := range dataList { + dataList[index].template, err = template.ParseFiles(value.locationPath) + if err != nil { + return nil, nil, nil, err + } + } + // 生成文件路径,填充 autoCodePath 字段,readme.txt.tpl不符合规则,需要特殊处理 + // resource/template/web/api.js.tpl -> autoCode/web/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 + } + + if lastSeparator := strings.LastIndex(trimBase, "/"); lastSeparator != -1 { + origFileName := strings.TrimSuffix(trimBase[lastSeparator+1:], ".tpl") + firstDot := strings.Index(origFileName, ".") + if firstDot != -1 { + dataList[index].autoCodePath = filepath.Join(autoPath, trimBase[:lastSeparator], autoCode.PackageName, + origFileName[:firstDot], autoCode.PackageName+origFileName[firstDot:]) + } + } + + if lastSeparator := strings.LastIndex(dataList[index].autoCodePath, string(os.PathSeparator)); lastSeparator != -1 { + needMkdir = append(needMkdir, dataList[index].autoCodePath[:lastSeparator]) + } + } + for _, value := range dataList { + fileList = append(fileList, value.autoCodePath) + } + return dataList, fileList, needMkdir, err +} -- GitLab