Browse Source

Merge branch 'gva_gormv2_dev' of https://github.com/flipped-aurora/gin-vue-admin into gva_gormv2_dev

main
krank666 4 years ago
parent
commit
d25df4188e
  1. 54
      server/service/sys_auto_code.go
  2. 37
      server/utils/file_operations.go
  3. 63
      server/utils/fileoperations.go

54
server/service/sys_auto_code.go

@ -1,12 +1,14 @@
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"
)
@ -15,6 +17,7 @@ type tplData struct {
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.locationPath, 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])
}
}
}

37
server/utils/file_operations.go

@ -0,0 +1,37 @@
package utils
import (
"os"
"path/filepath"
)
// FileMove: 文件移动供外部调用
// src: 源位置 绝对路径相对路径都可以
// dst: 目标位置 绝对路径相对路径都可以 dst 必须为文件夹
func FileMove(src string, dst string) (err error) {
if dst == "" {
return nil
}
src, err = filepath.Abs(src)
if err != nil {
return err
}
dst, err = filepath.Abs(dst)
if err != nil {
return err
}
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
}
}
return os.Rename(src, dst)
}

63
server/utils/fileoperations.go

@ -1,63 +0,0 @@
package utils
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
// package fileOperations use File Operations
// 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
// 转化为绝对路径
src, err = filepath.Abs(src)
if err != nil {
return err
}
dst, err = filepath.Abs(dst)
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 not 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
}
}
return err
}
Loading…
Cancel
Save