未验证 提交 9f88a43b 编写于 作者: S songzhibin97 提交者: GitHub

规范自动化插件包功能

* 规范自动化插件包功能
Co-authored-by: Nlongzhang83 <38556219+longzhang83@users.noreply.github.com>
上级 b1c024a9
...@@ -15,7 +15,7 @@ type AutoCodeStruct struct { ...@@ -15,7 +15,7 @@ type AutoCodeStruct struct {
Description string `json:"description"` // Struct中文名称 Description string `json:"description"` // Struct中文名称
AutoCreateApiToSql bool `json:"autoCreateApiToSql"` // 是否自动创建api AutoCreateApiToSql bool `json:"autoCreateApiToSql"` // 是否自动创建api
AutoMoveFile bool `json:"autoMoveFile"` // 是否自动移动文件 AutoMoveFile bool `json:"autoMoveFile"` // 是否自动移动文件
Fields []*Field `json:"fields"` Fields []*Field `json:"fields,omitempty"`
DictTypes []string `json:"-"` DictTypes []string `json:"-"`
Package string `json:"package"` Package string `json:"package"`
PackageT string `json:"-"` PackageT string `json:"-"`
...@@ -43,26 +43,40 @@ type SysAutoCode struct { ...@@ -43,26 +43,40 @@ type SysAutoCode struct {
} }
type AutoPlugReq struct { type AutoPlugReq struct {
PlugName string `json:"plugName"` // 必然大写开头 PlugName string `json:"plugName"` // 必然大写开头
Snake string `json:"snake"` // 后端自动转为 snake Snake string `json:"snake"` // 后端自动转为 snake
RouterGroup string `json:"routerGroup"` RouterGroup string `json:"routerGroup"`
HasGlobal bool `json:"hasGlobal"` HasGlobal bool `json:"hasGlobal"`
HasRequest bool `json:"hasRequest"` HasRequest bool `json:"hasRequest"`
HasResponse bool `json:"hasResponse"` HasResponse bool `json:"hasResponse"`
NeedModel bool `json:"needModel"` NeedModel bool `json:"needModel"`
Global []struct { Global []AutoPlugInfo `json:"global,omitempty"`
Key string `json:"key"` Request []AutoPlugInfo `json:"request,omitempty"`
Type string `json:"type"` Response []AutoPlugInfo `json:"response,omitempty"`
Desc string `json:"desc"` }
} `json:"global"`
Request []struct { func (a *AutoPlugReq) CheckList() {
Key string `json:"key"` a.Global = bind(a.Global)
Type string `json:"type"` a.Request = bind(a.Request)
Desc string `json:"desc"` a.Response = bind(a.Response)
} `json:"request"`
Response []struct { }
Key string `json:"key"` func bind(req []AutoPlugInfo) []AutoPlugInfo {
Type string `json:"type"` var r []AutoPlugInfo
Desc string `json:"desc"` for _, info := range req {
} `json:"response"` if info.Effective() {
r = append(r, info)
}
}
return r
}
type AutoPlugInfo struct {
Key string `json:"key"`
Type string `json:"type"`
Desc string `json:"desc"`
}
func (a AutoPlugInfo) Effective() bool {
return a.Key != "" && a.Type != "" && a.Desc != ""
} }
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"go.uber.org/zap"
"go/ast" "go/ast"
"go/format" "go/format"
"go/parser" "go/parser"
...@@ -793,10 +794,15 @@ func ImportReference(filepath, importCode, structName, packageName, groupName st ...@@ -793,10 +794,15 @@ func ImportReference(filepath, importCode, structName, packageName, groupName st
// 自动创建插件模板 // 自动创建插件模板
func (autoCodeService *AutoCodeService) CreatePlug(plug system.AutoPlugReq) error { func (autoCodeService *AutoCodeService) CreatePlug(plug system.AutoPlugReq) error {
// 检查列表参数是否有效
plug.CheckList()
tplFileList, _ := autoCodeService.GetAllTplFile(plugPath, nil) tplFileList, _ := autoCodeService.GetAllTplFile(plugPath, nil)
for _, tpl := range tplFileList { for _, tpl := range tplFileList {
temp, err := template.ParseFiles(tpl) temp, err := template.ParseFiles(tpl)
fmt.Println(err) if err != nil {
zap.L().Error("parse err", zap.String("tpl", tpl), zap.Error(err))
return err
}
pathArr := strings.SplitAfter(tpl, "/") pathArr := strings.SplitAfter(tpl, "/")
if strings.Index(pathArr[2], "tpl") < 0 { if strings.Index(pathArr[2], "tpl") < 0 {
dirPath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, fmt.Sprintf(global.GVA_CONFIG.AutoCode.SPlug, plug.Snake+"/"+pathArr[2])) dirPath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, fmt.Sprintf(global.GVA_CONFIG.AutoCode.SPlug, plug.Snake+"/"+pathArr[2]))
...@@ -804,10 +810,10 @@ func (autoCodeService *AutoCodeService) CreatePlug(plug system.AutoPlugReq) erro ...@@ -804,10 +810,10 @@ func (autoCodeService *AutoCodeService) CreatePlug(plug system.AutoPlugReq) erro
} }
file := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, fmt.Sprintf(global.GVA_CONFIG.AutoCode.SPlug, plug.Snake+"/"+tpl[len(plugPath):len(tpl)-4])) file := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, fmt.Sprintf(global.GVA_CONFIG.AutoCode.SPlug, plug.Snake+"/"+tpl[len(plugPath):len(tpl)-4]))
f, _ := os.OpenFile(file, os.O_WRONLY|os.O_CREATE, 0666) f, _ := os.OpenFile(file, os.O_WRONLY|os.O_CREATE, 0666)
e := temp.Execute(f, plug) err = temp.Execute(f, plug)
if e != nil { if err != nil {
fmt.Println(e) zap.L().Error("exec err", zap.String("tpl", tpl), zap.Error(err), zap.Any("plug", plug))
return e return err
} }
defer f.Close() defer f.Close()
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册