Skip to content

Commit ebd515f

Browse files
committed
add addAutoMoveFile
1 parent 636faaf commit ebd515f

File tree

2 files changed

+57
-65
lines changed

2 files changed

+57
-65
lines changed

server/service/sys_auto_code.go

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package service
22

33
import (
4+
"fmt"
45
"gin-vue-admin/global"
56
"gin-vue-admin/model"
67
"gin-vue-admin/model/request"
78
"gin-vue-admin/utils"
89
"io/ioutil"
910
"os"
11+
"path/filepath"
1012
"strings"
1113
"text/template"
1214
)
1315

1416
type tplData struct {
15-
template *template.Template
16-
locationPath string
17-
autoCodePath string
17+
template *template.Template
18+
locationPath string
19+
autoCodePath string
20+
autoMoveFilePath string
1821
}
1922

2023
// @title CreateTemp
@@ -88,23 +91,24 @@ func CreateTemp(autoCode model.AutoCodeStruct) (err error) {
8891
_ = f.Close()
8992
}
9093

91-
defer func() {
92-
// 移除中间文件
94+
defer func() { // 移除中间文件
9395
if err := os.RemoveAll(autoPath); err != nil {
9496
return
9597
}
9698
}()
97-
if autoCode.AutoMoveFile {
98-
// 判断是否需要自动转移
99-
for _, value := range dataList {
100-
// 转移
101-
err := utils.FileMove(value.autoCodePath, utils.GetAutoPath(value.autoCodePath))
99+
if autoCode.AutoMoveFile { // 判断是否需要自动转移
100+
for index, _ := range dataList {
101+
addAutoMoveFile(&dataList[index])
102+
}
103+
for _, value := range dataList { // 移动文件
104+
err := utils.FileMove(value.autoCodePath, value.autoMoveFilePath)
102105
if err != nil {
106+
fmt.Println(err)
103107
return err
104108
}
105109
}
106-
} else {
107-
// 打包
110+
return
111+
} else { // 打包
108112
if err := utils.ZipFiles("./ginvueadmin.zip", fileList, ".", "."); err != nil {
109113
return err
110114
}
@@ -144,3 +148,35 @@ func GetColumn(tableName string, dbName string) (err error, Columns []request.Co
144148
err = global.GVA_DB.Raw("SELECT COLUMN_NAME column_name,DATA_TYPE data_type,CASE DATA_TYPE WHEN 'longtext' THEN c.CHARACTER_MAXIMUM_LENGTH WHEN 'varchar' THEN c.CHARACTER_MAXIMUM_LENGTH WHEN 'double' THEN CONCAT_WS( ',', c.NUMERIC_PRECISION, c.NUMERIC_SCALE ) WHEN 'decimal' THEN CONCAT_WS( ',', c.NUMERIC_PRECISION, c.NUMERIC_SCALE ) WHEN 'int' THEN c.NUMERIC_PRECISION WHEN 'bigint' THEN c.NUMERIC_PRECISION ELSE '' END AS data_type_long,COLUMN_COMMENT column_comment FROM INFORMATION_SCHEMA.COLUMNS c WHERE table_name = ? AND table_schema = ?", tableName, dbName).Scan(&Columns).Error
145149
return err, Columns
146150
}
151+
152+
func addAutoMoveFile(data *tplData) {
153+
if strings.Contains(data.autoCodePath, "server") {
154+
if strings.Contains(data.autoCodePath, "router") {
155+
apiList := strings.Split(data.autoCodePath, "/")
156+
data.autoMoveFilePath = filepath.Join(apiList[len(apiList)-2], apiList[len(apiList)-1])
157+
} else if strings.Contains(data.autoCodePath, "api") {
158+
apiList := strings.Split(data.autoCodePath, "/")
159+
data.autoMoveFilePath = filepath.Join(apiList[len(apiList)-2], "v1", apiList[len(apiList)-1])
160+
} else if strings.Contains(data.autoCodePath, "service") {
161+
serviceList := strings.Split(data.autoCodePath, "/")
162+
data.autoMoveFilePath = filepath.Join(serviceList[len(serviceList)-2], serviceList[len(serviceList)-1])
163+
} else if strings.Contains(data.autoCodePath, "model") {
164+
modelList := strings.Split(data.autoCodePath, "/")
165+
data.autoMoveFilePath = filepath.Join(modelList[len(modelList)-2], modelList[len(modelList)-1])
166+
} else if strings.Contains(data.autoCodePath, "request") {
167+
requestList := strings.Split(data.autoCodePath, "/")
168+
data.autoMoveFilePath = filepath.Join("model", requestList[len(requestList)-2], requestList[len(requestList)-1])
169+
}
170+
} else if strings.Contains(data.autoCodePath, "web") {
171+
if strings.Contains(data.autoCodePath, "js") {
172+
jsList := strings.Split(data.autoCodePath, "/")
173+
data.autoMoveFilePath = filepath.Join("../", "web", "src", jsList[len(jsList)-2], jsList[len(jsList)-1])
174+
} else if strings.Contains(data.autoCodePath, "form") {
175+
formList := strings.Split(data.autoCodePath, "/")
176+
data.autoMoveFilePath = filepath.Join("../", "web", "view", formList[len(formList)-3], strings.Split(formList[len(formList)-1], ".")[0]+"From.vue")
177+
} else if strings.Contains(data.autoCodePath, "form") {
178+
vueList := strings.Split(data.autoCodePath, "/")
179+
data.autoMoveFilePath = filepath.Join("../", "web", "view", vueList[len(vueList)-3], vueList[len(vueList)-1])
180+
}
181+
}
182+
}

server/utils/file_operations.go

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,17 @@ package utils
22

33
import (
44
"errors"
5-
"fmt"
65
"os"
76
"path/filepath"
87
)
98

10-
// fileMove: 文件移动
11-
// src: 源位置 需要传入绝对路径
12-
// dst: 目标位置 需要传入绝对路径
13-
func fileMove(src string, dst string) error {
14-
if !filepath.IsAbs(dst) && !filepath.IsAbs(src) {
15-
return errors.New(fmt.Sprintf("%s or %s path is not abs", dst, src))
16-
}
17-
return os.Rename(src, dst)
18-
}
19-
209
// FileMove: 文件移动供外部调用
2110
// src: 源位置 绝对路径相对路径都可以
2211
// dst: 目标位置 绝对路径相对路径都可以 dst 必须为文件夹
23-
func FileMove(src string, dst string) error {
24-
var err error
25-
// 转化为绝对路径
12+
func FileMove(src string, dst string) (err error) {
13+
if dst == "" {
14+
return nil
15+
}
2616
src, err = filepath.Abs(src)
2717
if err != nil {
2818
return err
@@ -31,43 +21,9 @@ func FileMove(src string, dst string) error {
3121
if err != nil {
3222
return err
3323
}
34-
// 判断传入的是否是目录
35-
oSrc, err := os.Stat(src)
36-
if err != nil {
37-
return err
38-
}
39-
if oSrc.IsDir() {
40-
return errors.New(fmt.Sprintf("%s is Dir", src))
41-
}
42-
oDst, err := os.Stat(dst)
43-
if err != nil {
44-
return err
45-
}
46-
if !oDst.IsDir() {
47-
return errors.New(fmt.Sprintf("%s is not Dir", dst))
48-
}
49-
//// 遍历指定目录下所有文件
50-
//f, err := ioutil.ReadDir(src)
51-
//for _, file := range f {
52-
// nDst := filepath.Join(dst, file.Name())
53-
// nSrc := filepath.Join(src, file.Name())
54-
// err = fileMove(nSrc, nDst)
55-
// if err != nil {
56-
// return err
57-
// }
58-
//}
59-
nDst := filepath.Join(dst, filepath.Base(dst))
60-
if src == nDst {
61-
return nil
62-
}
63-
err = fileMove(src, nDst)
64-
if err != nil {
65-
return err
24+
if !filepath.IsAbs(dst) && !filepath.IsAbs(src) {
25+
return errors.New(dst + " or " + src + " path is not abs")
6626
}
67-
return err
68-
}
69-
70-
// GetAutoPath 根据生成路径生成移动路径
71-
func GetAutoPath(path string) string {
72-
return filepath.Base(filepath.Dir(path))
73-
}
27+
// TODO 判断文件夹是否存在,不存在mkdir
28+
return os.Rename(src, dst)
29+
}

0 commit comments

Comments
 (0)