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 !filepath.IsAbs(dst) && !filepath.IsAbs(src) { + return errors.New(dst + " or " + src + " path is not abs") } - 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 - } - 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