Skip to content

Commit e05e16d

Browse files
committed
抽出重复的内容优化PreviewTemp和CreateTemp
1 parent 3549f4c commit e05e16d

File tree

1 file changed

+57
-86
lines changed

1 file changed

+57
-86
lines changed

server/service/sys_auto_code.go

Lines changed: 57 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ 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
@@ -30,50 +35,10 @@ type tplData struct {
3035
//@return: map[string]string, error
3136

3237
func PreviewTemp(autoCode model.AutoCodeStruct) (map[string]string, error) {
33-
basePath := "resource/template"
34-
// 获取 basePath 文件夹下所有tpl文件
35-
tplFileList, err := GetAllTplFile(basePath, nil)
38+
dataList, _, needMkdir, err := getNeedList(&autoCode)
3639
if err != nil {
3740
return nil, err
3841
}
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})
45-
}
46-
// 生成 *Template, 填充 template 字段
47-
for index, value := range dataList {
48-
dataList[index].template, err = template.ParseFiles(value.locationPath)
49-
if err != nil {
50-
return nil, err
51-
}
52-
}
53-
// 生成文件路径,填充 autoCodePath 字段,readme.txt.tpl不符合规则,需要特殊处理
54-
// resource/template/web/api.js.tpl -> autoCode/web/autoCode.PackageName/api/autoCode.PackageName.js
55-
// resource/template/readme.txt.tpl -> autoCode/readme.txt
56-
autoPath := "autoCode/"
57-
for index, value := range dataList {
58-
trimBase := strings.TrimPrefix(value.locationPath, basePath+"/")
59-
if trimBase == "readme.txt.tpl" {
60-
dataList[index].autoCodePath = autoPath + "readme.txt"
61-
continue
62-
}
63-
64-
if lastSeparator := strings.LastIndex(trimBase, "/"); lastSeparator != -1 {
65-
origFileName := strings.TrimSuffix(trimBase[lastSeparator+1:], ".tpl")
66-
firstDot := strings.Index(origFileName, ".")
67-
if firstDot != -1 {
68-
dataList[index].autoCodePath = filepath.Join(autoPath, trimBase[:lastSeparator], autoCode.PackageName,
69-
origFileName[:firstDot], autoCode.PackageName+origFileName[firstDot:])
70-
}
71-
}
72-
73-
if lastSeparator := strings.LastIndex(dataList[index].autoCodePath, string(os.PathSeparator)); lastSeparator != -1 {
74-
needMkdir = append(needMkdir, dataList[index].autoCodePath[:lastSeparator])
75-
}
76-
}
7742

7843
// 写入文件前,先创建文件夹
7944
if err = utils.CreateDir(needMkdir...); err != nil {
@@ -89,7 +54,6 @@ func PreviewTemp(autoCode model.AutoCodeStruct) (map[string]string, error) {
8954
if ext = filepath.Ext(value.autoCodePath); ext == ".txt" {
9055
continue
9156
}
92-
fileList = append(fileList, value.autoCodePath)
9357
f, err := os.OpenFile(value.autoCodePath, os.O_CREATE|os.O_WRONLY, 0755)
9458
if err != nil {
9559
return nil, err
@@ -133,60 +97,17 @@ func PreviewTemp(autoCode model.AutoCodeStruct) (map[string]string, error) {
13397
//@return: error
13498

13599
func CreateTemp(autoCode model.AutoCodeStruct) (err error) {
136-
basePath := "resource/template"
137-
// 获取 basePath 文件夹下所有tpl文件
138-
tplFileList, err := GetAllTplFile(basePath, nil)
100+
dataList, fileList, needMkdir, err := getNeedList(&autoCode)
139101
if err != nil {
140102
return err
141103
}
142-
dataList := make([]tplData, 0, len(tplFileList))
143-
fileList := make([]string, 0, len(tplFileList))
144-
needMkdir := make([]string, 0, len(tplFileList)) // 当文件夹下存在多个tpl文件时,改为map更合理
145-
// 根据文件路径生成 tplData 结构体,待填充数据
146-
for _, value := range tplFileList {
147-
dataList = append(dataList, tplData{locationPath: value})
148-
}
149-
// 生成 *Template, 填充 template 字段
150-
for index, value := range dataList {
151-
dataList[index].template, err = template.ParseFiles(value.locationPath)
152-
if err != nil {
153-
return err
154-
}
155-
}
156-
157-
// 生成文件路径,填充 autoCodePath 字段,readme.txt.tpl不符合规则,需要特殊处理
158-
// resource/template/web/api.js.tpl -> autoCode/web/autoCode.PackageName/api/autoCode.PackageName.js
159-
// resource/template/readme.txt.tpl -> autoCode/readme.txt
160-
autoPath := "autoCode/"
161-
for index, value := range dataList {
162-
trimBase := strings.TrimPrefix(value.locationPath, basePath+"/")
163-
if trimBase == "readme.txt.tpl" {
164-
dataList[index].autoCodePath = autoPath + "readme.txt"
165-
continue
166-
}
167-
168-
if lastSeparator := strings.LastIndex(trimBase, "/"); lastSeparator != -1 {
169-
origFileName := strings.TrimSuffix(trimBase[lastSeparator+1:], ".tpl")
170-
firstDot := strings.Index(origFileName, ".")
171-
if firstDot != -1 {
172-
dataList[index].autoCodePath = filepath.Join(autoPath, trimBase[:lastSeparator], autoCode.PackageName,
173-
origFileName[:firstDot], autoCode.PackageName+origFileName[firstDot:])
174-
}
175-
}
176-
177-
if lastSeparator := strings.LastIndex(dataList[index].autoCodePath, string(os.PathSeparator)); lastSeparator != -1 {
178-
needMkdir = append(needMkdir, dataList[index].autoCodePath[:lastSeparator])
179-
}
180-
}
181-
182104
// 写入文件前,先创建文件夹
183105
if err = utils.CreateDir(needMkdir...); err != nil {
184106
return err
185107
}
186108

187109
// 生成文件
188110
for _, value := range dataList {
189-
fileList = append(fileList, value.autoCodePath)
190111
f, err := os.OpenFile(value.autoCodePath, os.O_CREATE|os.O_WRONLY, 0755)
191112
if err != nil {
192113
return err
@@ -379,3 +300,53 @@ func AutoCreateApi(a *model.AutoCodeStruct) (err error) {
379300
})
380301
return err
381302
}
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)