Browse Source

Merge pull request #397 from songzhibin97/gva_gormv2_dev

代码自动迁移动态适配
main
奇淼(piexlmax 4 years ago
committed by GitHub
parent
commit
c91dcf2418
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      server/config.yaml
  2. 16
      server/config/auto_code.go
  3. 2
      server/config/config.go
  4. 6
      server/core/viper.go
  5. 30
      server/service/sys_auto_code.go

15
server/config.yaml

@ -64,6 +64,21 @@ mysql:
log-mode: false log-mode: false
log-zap: "" log-zap: ""
autoCode:
root: ''
server: '/server'
server-api: '/api/v1'
server-model: '/model'
server-request: '/model/request/'
server-router: '/router'
server-service: '/service'
web: '/web/src'
web-api: '/api'
web-form: '/view'
web-table: '/view'
web-flow: '/view'
# local configuration # local configuration
local: local:
path: 'uploads/file' path: 'uploads/file'

16
server/config/auto_code.go

@ -0,0 +1,16 @@
package config
type Autocode struct {
Root string `mapstructure:"root" json:"root" yaml:"root"`
Server string `mapstructure:"server" json:"server" yaml:"server"`
SApi string `mapstructure:"server-api" json:"serverApi" yaml:"server-api"`
SModel string `mapstructure:"server-model" json:"serverModel" yaml:"server-model"`
SRequest string `mapstructure:"server-request" json:"serverRequest" yaml:"server-request"`
SRouter string `mapstructure:"server-router" json:"serverRouter" yaml:"server-router"`
SService string `mapstructure:"server-service" json:"serverService" yaml:"server-service"`
Web string `mapstructure:"web" json:"web" yaml:"web"`
WApi string `mapstructure:"web-api" json:"webApi" yaml:"web-api"`
WForm string `mapstructure:"web-form" json:"webForm" yaml:"web-form"`
WTable string `mapstructure:"web-table" json:"webTable" yaml:"web-table"`
WFlow string `mapstructure:"web-flow" json:"webFlow" yaml:"web-flow"`
}

2
server/config/config.go

@ -8,6 +8,8 @@ type Server struct {
Casbin Casbin `mapstructure:"casbin" json:"casbin" yaml:"casbin"` Casbin Casbin `mapstructure:"casbin" json:"casbin" yaml:"casbin"`
System System `mapstructure:"system" json:"system" yaml:"system"` System System `mapstructure:"system" json:"system" yaml:"system"`
Captcha Captcha `mapstructure:"captcha" json:"captcha" yaml:"captcha"` Captcha Captcha `mapstructure:"captcha" json:"captcha" yaml:"captcha"`
// aoto
AutoCode Autocode `mapstructure:"autoCode" json:"autoCode" yaml:"autoCode"`
// gorm // gorm
Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"` Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"`
// oss // oss

6
server/core/viper.go

@ -6,9 +6,11 @@ import (
"gin-vue-admin/global" "gin-vue-admin/global"
_ "gin-vue-admin/packfile" _ "gin-vue-admin/packfile"
"gin-vue-admin/utils" "gin-vue-admin/utils"
"os"
"path/filepath"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/spf13/viper" "github.com/spf13/viper"
"os"
) )
func Viper(path ...string) *viper.Viper { func Viper(path ...string) *viper.Viper {
@ -46,9 +48,9 @@ func Viper(path ...string) *viper.Viper {
fmt.Println(err) fmt.Println(err)
} }
}) })
if err := v.Unmarshal(&global.GVA_CONFIG); err != nil { if err := v.Unmarshal(&global.GVA_CONFIG); err != nil {
fmt.Println(err) fmt.Println(err)
} }
global.GVA_CONFIG.AutoCode.Root, _ = filepath.Abs("..")
return v return v
} }

30
server/service/sys_auto_code.go

@ -2,7 +2,6 @@ package service
import ( import (
"errors" "errors"
"fmt"
"gin-vue-admin/global" "gin-vue-admin/global"
"gin-vue-admin/model" "gin-vue-admin/model"
"gin-vue-admin/model/request" "gin-vue-admin/model/request"
@ -132,7 +131,6 @@ func CreateTemp(autoCode model.AutoCodeStruct) (err error) {
} }
for _, value := range dataList { // 移动文件 for _, value := range dataList { // 移动文件
if err := utils.FileMove(value.autoCodePath, value.autoMoveFilePath); err != nil { if err := utils.FileMove(value.autoCodePath, value.autoMoveFilePath); err != nil {
fmt.Println(err)
return err return err
} }
} }
@ -212,7 +210,6 @@ func GetColumn(tableName string, dbName string) (err error, Columns []request.Co
//@return: null //@return: null
func addAutoMoveFile(data *tplData) { func addAutoMoveFile(data *tplData) {
dir := filepath.Base(filepath.Dir(data.autoCodePath))
base := filepath.Base(data.autoCodePath) base := filepath.Base(data.autoCodePath)
fileSlice := strings.Split(data.autoCodePath, string(os.PathSeparator)) fileSlice := strings.Split(data.autoCodePath, string(os.PathSeparator))
n := len(fileSlice) n := len(fileSlice)
@ -221,25 +218,34 @@ func addAutoMoveFile(data *tplData) {
} }
if strings.Contains(fileSlice[1], "server") { if strings.Contains(fileSlice[1], "server") {
if strings.Contains(fileSlice[n-2], "router") { if strings.Contains(fileSlice[n-2], "router") {
data.autoMoveFilePath = filepath.Join(dir, base)
data.autoMoveFilePath = filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server,
global.GVA_CONFIG.AutoCode.SRouter, base)
} else if strings.Contains(fileSlice[n-2], "api") { } else if strings.Contains(fileSlice[n-2], "api") {
data.autoMoveFilePath = filepath.Join(dir, "v1", base)
data.autoMoveFilePath = filepath.Join(global.GVA_CONFIG.AutoCode.Root,
global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SApi, base)
} else if strings.Contains(fileSlice[n-2], "service") { } else if strings.Contains(fileSlice[n-2], "service") {
data.autoMoveFilePath = filepath.Join(dir, base)
data.autoMoveFilePath = filepath.Join(global.GVA_CONFIG.AutoCode.Root,
global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SService, base)
} else if strings.Contains(fileSlice[n-2], "model") { } else if strings.Contains(fileSlice[n-2], "model") {
data.autoMoveFilePath = filepath.Join(dir, base)
data.autoMoveFilePath = filepath.Join(global.GVA_CONFIG.AutoCode.Root,
global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SModel, base)
} else if strings.Contains(fileSlice[n-2], "request") { } else if strings.Contains(fileSlice[n-2], "request") {
data.autoMoveFilePath = filepath.Join("model", dir, base)
data.autoMoveFilePath = filepath.Join(global.GVA_CONFIG.AutoCode.Root,
global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SRequest, base)
} }
} else if strings.Contains(fileSlice[1], "web") { } else if strings.Contains(fileSlice[1], "web") {
if strings.Contains(fileSlice[n-1], "js") { if strings.Contains(fileSlice[n-1], "js") {
data.autoMoveFilePath = filepath.Join("../", "web", "src", dir, base)
data.autoMoveFilePath = filepath.Join(global.GVA_CONFIG.AutoCode.Root,
global.GVA_CONFIG.AutoCode.Web, global.GVA_CONFIG.AutoCode.WApi, base)
} else if strings.Contains(fileSlice[n-2], "workflowForm") { } else if strings.Contains(fileSlice[n-2], "workflowForm") {
data.autoMoveFilePath = filepath.Join("../", "web", "src", "view", filepath.Base(filepath.Dir(filepath.Dir(data.autoCodePath))), strings.TrimSuffix(base, filepath.Ext(base))+"WorkflowForm.vue")
data.autoMoveFilePath = filepath.Join(global.GVA_CONFIG.AutoCode.Root,
global.GVA_CONFIG.AutoCode.Web, global.GVA_CONFIG.AutoCode.WFlow, filepath.Base(filepath.Dir(filepath.Dir(data.autoCodePath))), strings.TrimSuffix(base, filepath.Ext(base))+"WorkflowForm.vue")
} else if strings.Contains(fileSlice[n-2], "form") { } else if strings.Contains(fileSlice[n-2], "form") {
data.autoMoveFilePath = filepath.Join("../", "web", "src", "view", filepath.Base(filepath.Dir(filepath.Dir(data.autoCodePath))), strings.TrimSuffix(base, filepath.Ext(base))+"Form.vue")
data.autoMoveFilePath = filepath.Join(global.GVA_CONFIG.AutoCode.Root,
global.GVA_CONFIG.AutoCode.Web, global.GVA_CONFIG.AutoCode.WForm, filepath.Base(filepath.Dir(filepath.Dir(data.autoCodePath))), strings.TrimSuffix(base, filepath.Ext(base))+"Form.vue")
} else if strings.Contains(fileSlice[n-2], "table") { } else if strings.Contains(fileSlice[n-2], "table") {
data.autoMoveFilePath = filepath.Join("../", "web", "src", "view", filepath.Base(filepath.Dir(filepath.Dir(data.autoCodePath))), base)
data.autoMoveFilePath = filepath.Join(global.GVA_CONFIG.AutoCode.Root,
global.GVA_CONFIG.AutoCode.Web, global.GVA_CONFIG.AutoCode.WTable, filepath.Base(filepath.Dir(filepath.Dir(data.autoCodePath))), base)
} }
} }
} }

Loading…
Cancel
Save