From 9a044b2883d75916648b4f98808208053ff548cd Mon Sep 17 00:00:00 2001 From: SliverHorn Date: Sat, 7 Nov 2020 15:09:11 +0800 Subject: [PATCH 1/6] =?UTF-8?q?add=20autoMoveFilePath=20=E5=AD=97=E6=AE=B5?= =?UTF-8?q?,=E4=BF=AE=E6=94=B9utils=E7=9A=84fileoperations.go->=20file=5Fo?= =?UTF-8?q?perations.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/service/sys_auto_code.go | 7 ++++--- server/utils/{fileoperations.go => file_operations.go} | 2 -- 2 files changed, 4 insertions(+), 5 deletions(-) rename server/utils/{fileoperations.go => file_operations.go} (96%) diff --git a/server/service/sys_auto_code.go b/server/service/sys_auto_code.go index 4b863d41..f56619b6 100644 --- a/server/service/sys_auto_code.go +++ b/server/service/sys_auto_code.go @@ -12,9 +12,10 @@ import ( ) type tplData struct { - template *template.Template - locationPath string - autoCodePath string + template *template.Template + locationPath string + autoCodePath string + autoMoveFilePath string } // @title CreateTemp diff --git a/server/utils/fileoperations.go b/server/utils/file_operations.go similarity index 96% rename from server/utils/fileoperations.go rename to server/utils/file_operations.go index 311cbeee..a2ec56c4 100644 --- a/server/utils/fileoperations.go +++ b/server/utils/file_operations.go @@ -8,8 +8,6 @@ import ( "path/filepath" ) -// package fileOperations use File Operations - // fileMove: 文件移动 // src: 源位置 需要传入绝对路径 // dst: 目标位置 需要传入绝对路径 From 53733962ef6d52cc4bea44e1dbf357027f75f67d Mon Sep 17 00:00:00 2001 From: v_zhibsong Date: Sat, 7 Nov 2020 15:35:12 +0800 Subject: [PATCH 2/6] change filemove --- server/service/sys_auto_code.go | 9 ++++----- server/utils/file_operations.go | 5 +++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/server/service/sys_auto_code.go b/server/service/sys_auto_code.go index f56619b6..ed17c898 100644 --- a/server/service/sys_auto_code.go +++ b/server/service/sys_auto_code.go @@ -12,10 +12,9 @@ import ( ) type tplData struct { - template *template.Template - locationPath string - autoCodePath string - autoMoveFilePath string + template *template.Template + locationPath string + autoCodePath string } // @title CreateTemp @@ -99,7 +98,7 @@ func CreateTemp(autoCode model.AutoCodeStruct) (err error) { // 判断是否需要自动转移 for _, value := range dataList { // 转移 - err := utils.FileMove(value.locationPath, value.autoCodePath) + err := utils.FileMove(value.autoCodePath, utils.GetAutoPath(value.autoCodePath)) if err != nil { return err } diff --git a/server/utils/file_operations.go b/server/utils/file_operations.go index a2ec56c4..0c6d2001 100644 --- a/server/utils/file_operations.go +++ b/server/utils/file_operations.go @@ -59,3 +59,8 @@ func FileMove(src string, dst string) error { } return err } + +// GetAutoPath 根据生成路径生成移动路径 +func GetAutoPath(path string) string { + return filepath.Base(filepath.Dir(path)) +} From 0bf09204866b5d124e0471030a4f869931ca0c41 Mon Sep 17 00:00:00 2001 From: v_zhibsong Date: Sat, 7 Nov 2020 16:00:09 +0800 Subject: [PATCH 3/6] change filepath --- server/utils/file_operations.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/server/utils/file_operations.go b/server/utils/file_operations.go index 0c6d2001..d1b19cb7 100644 --- a/server/utils/file_operations.go +++ b/server/utils/file_operations.go @@ -3,7 +3,6 @@ package utils import ( "errors" "fmt" - "io/ioutil" "os" "path/filepath" ) @@ -37,8 +36,8 @@ func FileMove(src string, dst string) error { if err != nil { return err } - if !oSrc.IsDir() { - return errors.New(fmt.Sprintf("%s is not Dir", src)) + if oSrc.IsDir() { + return errors.New(fmt.Sprintf("%s is Dir", src)) } oDst, err := os.Stat(dst) if err != nil { @@ -47,15 +46,19 @@ func FileMove(src string, dst string) error { if !oDst.IsDir() { return errors.New(fmt.Sprintf("%s is not Dir", dst)) } - // 遍历指定目录下所有文件 - f, err := ioutil.ReadDir(src) - for _, file := range f { - nDst := filepath.Join(dst, file.Name()) - nSrc := filepath.Join(src, file.Name()) - err = fileMove(nSrc, nDst) - if err != nil { - return err - } + //// 遍历指定目录下所有文件 + //f, err := ioutil.ReadDir(src) + //for _, file := range f { + // nDst := filepath.Join(dst, file.Name()) + // nSrc := filepath.Join(src, file.Name()) + // err = fileMove(nSrc, nDst) + // if err != nil { + // return err + // } + //} + err = fileMove(src, filepath.Join(dst, filepath.Base(dst))) + if err != nil { + return err } return err } From 15c47ddeb9965be21e216068fe385249346c750c Mon Sep 17 00:00:00 2001 From: v_zhibsong Date: Sat, 7 Nov 2020 16:00:55 +0800 Subject: [PATCH 4/6] Update file_operations.go --- server/utils/file_operations.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/server/utils/file_operations.go b/server/utils/file_operations.go index d1b19cb7..0f83d58e 100644 --- a/server/utils/file_operations.go +++ b/server/utils/file_operations.go @@ -56,7 +56,11 @@ func FileMove(src string, dst string) error { // return err // } //} - err = fileMove(src, filepath.Join(dst, filepath.Base(dst))) + nDst := filepath.Join(dst, filepath.Base(dst)) + if src == nDst { + return nil + } + err = fileMove(src, nDst) if err != nil { return err } From 9c8c690afbe066fb420b0c269c3d639320f8cde2 Mon Sep 17 00:00:00 2001 From: SliverHorn Date: Sat, 7 Nov 2020 17:32:58 +0800 Subject: [PATCH 5/6] add addAutoMoveFile --- server/service/sys_auto_code.go | 60 ++++++++++++++++++++++++------- server/utils/file_operations.go | 62 +++++---------------------------- 2 files changed, 57 insertions(+), 65 deletions(-) diff --git a/server/service/sys_auto_code.go b/server/service/sys_auto_code.go index ed17c898..62fe21e8 100644 --- a/server/service/sys_auto_code.go +++ b/server/service/sys_auto_code.go @@ -1,20 +1,23 @@ package service import ( + "fmt" "gin-vue-admin/global" "gin-vue-admin/model" "gin-vue-admin/model/request" "gin-vue-admin/utils" "io/ioutil" "os" + "path/filepath" "strings" "text/template" ) type tplData struct { - template *template.Template - locationPath string - autoCodePath string + template *template.Template + locationPath string + autoCodePath string + autoMoveFilePath string } // @title CreateTemp @@ -88,23 +91,24 @@ func CreateTemp(autoCode model.AutoCodeStruct) (err error) { _ = f.Close() } - defer func() { - // 移除中间文件 + defer func() { // 移除中间文件 if err := os.RemoveAll(autoPath); err != nil { return } }() - if autoCode.AutoMoveFile { - // 判断是否需要自动转移 - for _, value := range dataList { - // 转移 - err := utils.FileMove(value.autoCodePath, utils.GetAutoPath(value.autoCodePath)) + if autoCode.AutoMoveFile { // 判断是否需要自动转移 + for index, _ := range dataList { + addAutoMoveFile(&dataList[index]) + } + for _, value := range dataList { // 移动文件 + err := utils.FileMove(value.autoCodePath, value.autoMoveFilePath) if err != nil { + fmt.Println(err) return err } } - } else { - // 打包 + return + } else { // 打包 if err := utils.ZipFiles("./ginvueadmin.zip", fileList, ".", "."); err != nil { return err } @@ -144,3 +148,35 @@ func GetColumn(tableName string, dbName string) (err error, Columns []request.Co 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 return err, Columns } + +func addAutoMoveFile(data *tplData) { + if strings.Contains(data.autoCodePath, "server") { + if strings.Contains(data.autoCodePath, "router") { + apiList := strings.Split(data.autoCodePath, "/") + data.autoMoveFilePath = filepath.Join(apiList[len(apiList)-2], apiList[len(apiList)-1]) + } else if strings.Contains(data.autoCodePath, "api") { + apiList := strings.Split(data.autoCodePath, "/") + data.autoMoveFilePath = filepath.Join(apiList[len(apiList)-2], "v1", apiList[len(apiList)-1]) + } else if strings.Contains(data.autoCodePath, "service") { + serviceList := strings.Split(data.autoCodePath, "/") + data.autoMoveFilePath = filepath.Join(serviceList[len(serviceList)-2], serviceList[len(serviceList)-1]) + } else if strings.Contains(data.autoCodePath, "model") { + modelList := strings.Split(data.autoCodePath, "/") + data.autoMoveFilePath = filepath.Join(modelList[len(modelList)-2], modelList[len(modelList)-1]) + } else if strings.Contains(data.autoCodePath, "request") { + requestList := strings.Split(data.autoCodePath, "/") + data.autoMoveFilePath = filepath.Join("model", requestList[len(requestList)-2], requestList[len(requestList)-1]) + } + } else if strings.Contains(data.autoCodePath, "web") { + if strings.Contains(data.autoCodePath, "js") { + jsList := strings.Split(data.autoCodePath, "/") + data.autoMoveFilePath = filepath.Join("../", "web", "src", jsList[len(jsList)-2], jsList[len(jsList)-1]) + } else if strings.Contains(data.autoCodePath, "form") { + formList := strings.Split(data.autoCodePath, "/") + data.autoMoveFilePath = filepath.Join("../", "web", "view", formList[len(formList)-3], strings.Split(formList[len(formList)-1], ".")[0]+"From.vue") + } else if strings.Contains(data.autoCodePath, "form") { + vueList := strings.Split(data.autoCodePath, "/") + data.autoMoveFilePath = filepath.Join("../", "web", "view", vueList[len(vueList)-3], vueList[len(vueList)-1]) + } + } +} diff --git a/server/utils/file_operations.go b/server/utils/file_operations.go index 0f83d58e..66d57c74 100644 --- a/server/utils/file_operations.go +++ b/server/utils/file_operations.go @@ -2,27 +2,17 @@ package utils import ( "errors" - "fmt" "os" "path/filepath" ) -// fileMove: 文件移动 -// src: 源位置 需要传入绝对路径 -// dst: 目标位置 需要传入绝对路径 -func fileMove(src string, dst string) error { - if !filepath.IsAbs(dst) && !filepath.IsAbs(src) { - return errors.New(fmt.Sprintf("%s or %s path is not abs", dst, src)) - } - return os.Rename(src, dst) -} - // FileMove: 文件移动供外部调用 // src: 源位置 绝对路径相对路径都可以 // dst: 目标位置 绝对路径相对路径都可以 dst 必须为文件夹 -func FileMove(src string, dst string) error { - var err error - // 转化为绝对路径 +func FileMove(src string, dst string) (err error) { + if dst == "" { + return nil + } src, err = filepath.Abs(src) if err != nil { return err @@ -31,43 +21,9 @@ func FileMove(src string, dst string) error { if err != nil { return err } - // 判断传入的是否是目录 - oSrc, err := os.Stat(src) - if err != nil { - return err - } - if oSrc.IsDir() { - return errors.New(fmt.Sprintf("%s is Dir", src)) - } - oDst, err := os.Stat(dst) - if err != nil { - return err - } - if !oDst.IsDir() { - return errors.New(fmt.Sprintf("%s is not Dir", dst)) - } - //// 遍历指定目录下所有文件 - //f, err := ioutil.ReadDir(src) - //for _, file := range f { - // nDst := filepath.Join(dst, file.Name()) - // nSrc := filepath.Join(src, file.Name()) - // err = fileMove(nSrc, nDst) - // if err != nil { - // return err - // } - //} - nDst := filepath.Join(dst, filepath.Base(dst)) - if src == nDst { - return nil - } - err = fileMove(src, nDst) - if err != nil { - return err + if !filepath.IsAbs(dst) && !filepath.IsAbs(src) { + return errors.New(dst + " or " + src + " path is not abs") } - return err -} - -// GetAutoPath 根据生成路径生成移动路径 -func GetAutoPath(path string) string { - return filepath.Base(filepath.Dir(path)) -} + // TODO 判断文件夹是否存在,不存在mkdir + return os.Rename(src, dst) +} \ No newline at end of file From a6efedf84d8c8efaeb8ffb92d53da62bb63a6ea7 Mon Sep 17 00:00:00 2001 From: v_zhibsong Date: Sat, 7 Nov 2020 18:03:23 +0800 Subject: [PATCH 6/6] change2 --- server/utils/file_operations.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/server/utils/file_operations.go b/server/utils/file_operations.go index 66d57c74..9eb99232 100644 --- a/server/utils/file_operations.go +++ b/server/utils/file_operations.go @@ -1,7 +1,6 @@ package utils import ( - "errors" "os" "path/filepath" ) @@ -21,9 +20,18 @@ func FileMove(src string, dst string) (err error) { if err != nil { return err } - if !filepath.IsAbs(dst) && !filepath.IsAbs(src) { - return errors.New(dst + " or " + src + " path is not abs") + var revoke = false +Redirect: + _, err = os.Stat(filepath.Dir(dst)) + if err != nil { + err = os.MkdirAll(filepath.Dir(dst), 0755) + if err != nil { + return err + } + if !revoke { + revoke = true + goto Redirect + } } - // TODO 判断文件夹是否存在,不存在mkdir return os.Rename(src, dst) -} \ No newline at end of file +}