Skip to content

Commit

Permalink
自动化回滚可以回滚gorm.go 和 router.go
Browse files Browse the repository at this point in the history
  • Loading branch information
piexlmax committed Mar 11, 2023
1 parent d51bd0f commit 419c677
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
4 changes: 4 additions & 0 deletions server/service/system/sys_autocode_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
"github.com/flipped-aurora/gin-vue-admin/server/utils/ast"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -120,6 +121,9 @@ func (autoCodeHistoryService *AutoCodeHistoryService) RollBack(info *systemReq.R
_ = utils.AutoClearCode(meta[0], meta[2])
}
}

ast.RollBackAst(md.Package, md.StructName)

md.Flag = 1
return global.GVA_DB.Save(&md).Error
}
Expand Down
157 changes: 157 additions & 0 deletions server/utils/ast/ast_rollback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package ast

import (
"bytes"
"fmt"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"os"
"path/filepath"
)

func RollBackAst(pk, model string) {
RollGormBack(pk, model)
RollRouterBack(pk, model)
}

func RollGormBack(pk, model string) {

// 首先分析存在多少个ttt作为调用方的node块
// 如果多个 仅仅删除对应块即可
// 如果单个 那么还需要剔除import
path := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "gorm.go")
src, err := os.ReadFile(path)
if err != nil {
fmt.Println(err)
}
fileSet := token.NewFileSet()
astFile, err := parser.ParseFile(fileSet, "", src, 0)
if err != nil {
fmt.Println(err)
}
var n *ast.CallExpr
var k int = -1
var pkNum = 0
ast.Inspect(astFile, func(node ast.Node) bool {
if node, ok := node.(*ast.CallExpr); ok {
for i := range node.Args {
pkOK := false
modelOK := false
ast.Inspect(node.Args[i], func(item ast.Node) bool {
if ii, ok := item.(*ast.Ident); ok {
if ii.Name == pk {
pkOK = true
pkNum++
}
if ii.Name == model {
modelOK = true
}
}
if pkOK && modelOK {
n = node
k = i
}
return true
})
}
}
return true
})
if k > 0 {
n.Args = append(append([]ast.Expr{}, n.Args[:k]...), n.Args[k+1:]...)
}
if pkNum == 1 {
var imI int = -1
var gp *ast.GenDecl
ast.Inspect(astFile, func(node ast.Node) bool {
if gen, ok := node.(*ast.GenDecl); ok {
for i := range gen.Specs {
if imspec, ok := gen.Specs[i].(*ast.ImportSpec); ok {
if imspec.Path.Value == "\"github.com/flipped-aurora/gin-vue-admin/server/model/"+pk+"\"" {
gp = gen
imI = i
return false
}
}
}
}
return true
})

if imI > -1 {
gp.Specs = append(append([]ast.Spec{}, gp.Specs[:imI]...), gp.Specs[imI+1:]...)
}
}

var out []byte
bf := bytes.NewBuffer(out)
printer.Fprint(bf, fileSet, astFile)
os.Remove(path)
os.WriteFile(path, bf.Bytes(), 0666)

}

func RollRouterBack(pk, model string) {

// 首先抓到所有的代码块结构 {}
// 分析结构中是否存在一个变量叫做 pk+Router
// 然后获取到代码块指针 对内部需要回滚的代码进行剔除
path := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "router.go")
src, err := os.ReadFile(path)
if err != nil {
fmt.Println(err)
}
fileSet := token.NewFileSet()
astFile, err := parser.ParseFile(fileSet, "", src, 0)
if err != nil {
fmt.Println(err)
}

var block *ast.BlockStmt
ast.Inspect(astFile, func(node ast.Node) bool {
if n, ok := node.(*ast.BlockStmt); ok {
ast.Inspect(n, func(bNode ast.Node) bool {
if in, ok := bNode.(*ast.Ident); ok {
if in.Name == pk+"Router" {
block = n
return false
}
}
return true
})
return true
}
return true
})
var k int
for i := range block.List {
if stmtNode, ok := block.List[i].(*ast.ExprStmt); ok {
ast.Inspect(stmtNode, func(node ast.Node) bool {
if n, ok := node.(*ast.Ident); ok {
if n.Name == "Init"+model+"Router" {
k = i
return false
}
}
return true
})
}
}

block.List = append(append([]ast.Stmt{}, block.List[:k]...), block.List[k+1:]...)

if len(block.List) == 1 {
// 说明这个块就没任何意义了
block.List = nil
// TODO 删除空的{}
}

var out []byte
bf := bytes.NewBuffer(out)
printer.Fprint(bf, fileSet, astFile)
os.Remove(path)
os.WriteFile(path, bf.Bytes(), 0666)
}
11 changes: 11 additions & 0 deletions server/utils/ast/ast_rollback_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ast

import "testing"

func TestRollRouterBack(t *testing.T) {
RollRouterBack("ttt", "Testttt")
}

func TestRollGormBack(t *testing.T) {
RollGormBack("ttt", "Testttt")
}

0 comments on commit 419c677

Please sign in to comment.