ソースを参照

抽出重复的内容优化PreviewTemp和CreateTemp

songzhibin97 4 年 前
コミット
5d27f13347
1 ファイル変更57 行追加86 行削除
  1. 57 86
      server/service/sys_auto_code.go

+ 57 - 86
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
+}