2.7.0Beta2 (#1829)

* opt: 优化自动化package注册gorm_biz代码,替换为Inspect

* opt: 自动化代码插件gorm改造完成

* opt: 自动化代码插件路由注册改造完成
This commit is contained in:
PiexlMax(奇淼
2024-07-24 15:24:50 +08:00
committed by GitHub
parent 4512aa7ff8
commit 1494bc9db3
4 changed files with 209 additions and 476 deletions
+63 -188
View File
@@ -2,9 +2,7 @@ package ast
import (
"fmt"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"go/ast"
"go/token"
"io"
)
@@ -40,203 +38,80 @@ func (a *PluginInitializeRouter) Parse(filename string, writer io.Writer) (file
}
func (a *PluginInitializeRouter) Rollback(file *ast.File) error {
for i := 0; i < len(file.Decls); i++ {
v1, o1 := file.Decls[i].(*ast.FuncDecl)
if o1 {
for j := 0; j < len(v1.Body.List); j++ {
v2, o2 := v1.Body.List[j].(*ast.ExprStmt)
if o2 {
v3, o3 := v2.X.(*ast.CallExpr)
if o3 {
v4, o4 := v3.Fun.(*ast.SelectorExpr)
if o4 {
v5, o5 := v4.X.(*ast.SelectorExpr)
if o5 {
v6, o6 := v5.X.(*ast.SelectorExpr)
if o6 {
v7, o7 := v6.X.(*ast.Ident)
if o7 {
if v7.Name == a.PackageName && v6.Sel.Name == a.AppName && v5.Sel.Name == a.GroupName && v4.Sel.Name == a.FunctionName {
v1.Body.List = append(v1.Body.List[:j], v1.Body.List[j+1:]...)
if len(v1.Body.List) == 3 {
if a.ImportMiddlewarePath != "" {
_ = NewImport(a.ImportMiddlewarePath).Rollback(file)
}
if a.ImportGlobalPath != "" {
_ = NewImport(a.ImportGlobalPath).Rollback(file)
}
_ = NewImport(a.ImportPath).Rollback(file)
v1.Body.List = nil
}
break
}
}
}
}
}
funcDecl := FindFunction(file, "Router")
delI := 0
routerNum := 0
for i := len(funcDecl.Body.List) - 1; i >= 0; i-- {
stmt, ok := funcDecl.Body.List[i].(*ast.ExprStmt)
if !ok {
continue
}
callExpr, ok := stmt.X.(*ast.CallExpr)
if !ok {
continue
}
selExpr, ok := callExpr.Fun.(*ast.SelectorExpr)
if !ok {
continue
}
ident, ok := selExpr.X.(*ast.SelectorExpr)
if ok {
if iExpr, ieok := ident.X.(*ast.SelectorExpr); ieok {
if iden, idok := iExpr.X.(*ast.Ident); idok {
if iden.Name == "router" {
routerNum++
}
}
}
if ident.Sel.Name == a.GroupName && selExpr.Sel.Name == a.FunctionName {
// 删除语句
delI = i
}
}
}
funcDecl.Body.List = append(funcDecl.Body.List[:delI], funcDecl.Body.List[delI+1:]...)
if routerNum <= 1 {
_ = NewImport(a.ImportPath).Rollback(file)
}
return nil
}
func (a *PluginInitializeRouter) Injection(file *ast.File) error {
_ = NewImport(a.ImportPath).Injection(file)
for i := 0; i < len(file.Decls); i++ {
v1, o1 := file.Decls[i].(*ast.FuncDecl)
if o1 {
var has bool
for j := 0; j < len(v1.Body.List); j++ {
v2, o2 := v1.Body.List[j].(*ast.ExprStmt)
if o2 {
v3, o3 := v2.X.(*ast.CallExpr)
if o3 {
v4, o4 := v3.Fun.(*ast.SelectorExpr)
if o4 {
v5, o5 := v4.X.(*ast.SelectorExpr)
if o5 {
v6, o6 := v5.X.(*ast.SelectorExpr)
if o6 {
v7, o7 := v6.X.(*ast.Ident)
if o7 {
if v7.Name == a.PackageName && v6.Sel.Name == a.AppName && v5.Sel.Name == a.GroupName && v4.Sel.Name == a.FunctionName {
has = true
}
}
}
}
}
}
}
}
if !has {
if v1.Body == nil {
v1.Body = new(ast.BlockStmt)
}
if v1.Body.List == nil {
a.ImportMiddlewarePath = fmt.Sprintf(`"%s/middleware"`, global.GVA_CONFIG.AutoCode.Module)
_ = NewImport(a.ImportMiddlewarePath).Injection(file)
a.ImportGlobalPath = fmt.Sprintf(`"%s/global"`, global.GVA_CONFIG.AutoCode.Module)
_ = NewImport(a.ImportGlobalPath).Injection(file)
v1.Body.List = make([]ast.Stmt, 0, 3)
public := &ast.AssignStmt{
Lhs: []ast.Expr{
&ast.Ident{Name: a.LeftRouterGroupName, Obj: ast.NewObj(ast.Var, a.LeftRouterGroupName)},
},
Tok: token.DEFINE,
Rhs: []ast.Expr{
&ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{Name: "engine"},
Sel: &ast.Ident{Name: "Group"},
},
Args: []ast.Expr{
&ast.BasicLit{
Kind: token.STRING,
Value: `global.GVA_CONFIG.System.RouterPrefix`,
},
},
},
Sel: &ast.Ident{Name: "Group"},
},
Args: []ast.Expr{
&ast.BasicLit{
Kind: token.STRING,
Value: `""`,
},
},
},
},
}
private := &ast.AssignStmt{
Lhs: []ast.Expr{
&ast.Ident{Name: a.RightRouterGroupName, Obj: ast.NewObj(ast.Var, a.RightRouterGroupName)},
},
Tok: token.DEFINE,
Rhs: []ast.Expr{
&ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{Name: "engine"},
Sel: &ast.Ident{Name: "Group"},
},
Args: []ast.Expr{
&ast.BasicLit{
Kind: token.STRING,
Value: `global.GVA_CONFIG.System.RouterPrefix`,
},
},
},
Sel: &ast.Ident{Name: "Group"},
},
Args: []ast.Expr{
&ast.BasicLit{
Kind: token.STRING,
Value: `""`,
},
},
},
},
}
useMiddleware := &ast.ExprStmt{
X: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{Name: a.RightRouterGroupName},
Sel: &ast.Ident{Name: "Use"},
},
Args: []ast.Expr{
&ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{Name: "middleware"},
Sel: &ast.Ident{Name: "JWTAuth"},
},
},
},
},
Sel: &ast.Ident{Name: "Use"},
},
Args: []ast.Expr{
&ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{Name: "middleware"},
Sel: &ast.Ident{Name: "CasbinHandler"},
},
},
},
},
}
v1.Body.List = append(v1.Body.List, public)
v1.Body.List = append(v1.Body.List, private)
v1.Body.List = append(v1.Body.List, useMiddleware)
}
body := &ast.ExprStmt{
X: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.SelectorExpr{
X: &ast.SelectorExpr{
X: &ast.Ident{Name: a.PackageName},
Sel: &ast.Ident{Name: a.AppName},
},
Sel: &ast.Ident{Name: a.GroupName},
},
Sel: &ast.Ident{Name: a.FunctionName},
},
Args: []ast.Expr{
&ast.Ident{Name: a.LeftRouterGroupName},
&ast.Ident{Name: a.RightRouterGroupName},
},
},
}
v1.Body.List = append(v1.Body.List, body)
}
funcDecl := FindFunction(file, "Router")
var exists bool
ast.Inspect(funcDecl, func(n ast.Node) bool {
callExpr, ok := n.(*ast.CallExpr)
if !ok {
return true
}
selExpr, ok := callExpr.Fun.(*ast.SelectorExpr)
if !ok {
return true
}
ident, ok := selExpr.X.(*ast.SelectorExpr)
if ok && ident.Sel.Name == a.GroupName && selExpr.Sel.Name == a.FunctionName {
exists = true
return false
}
return true
})
if !exists {
stmtStr := fmt.Sprintf("%s.%s.%s.%s(%s, %s)", a.PackageName, a.AppName, a.GroupName, a.FunctionName, a.LeftRouterGroupName, a.RightRouterGroupName)
stmt := CreateStmt(stmtStr)
funcDecl.Body.List = append(funcDecl.Body.List, stmt)
}
return nil
}