Skip to content

Commit 419c677

Browse files
author
piexlmax
committed
自动化回滚可以回滚gorm.go 和 router.go
1 parent d51bd0f commit 419c677

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

server/service/system/sys_autocode_history.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
7+
"github.com/flipped-aurora/gin-vue-admin/server/utils/ast"
78
"path/filepath"
89
"strconv"
910
"strings"
@@ -120,6 +121,9 @@ func (autoCodeHistoryService *AutoCodeHistoryService) RollBack(info *systemReq.R
120121
_ = utils.AutoClearCode(meta[0], meta[2])
121122
}
122123
}
124+
125+
ast.RollBackAst(md.Package, md.StructName)
126+
123127
md.Flag = 1
124128
return global.GVA_DB.Save(&md).Error
125129
}

server/utils/ast/ast_rollback.go

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package ast
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"github.com/flipped-aurora/gin-vue-admin/server/global"
7+
"go/ast"
8+
"go/parser"
9+
"go/printer"
10+
"go/token"
11+
"os"
12+
"path/filepath"
13+
)
14+
15+
func RollBackAst(pk, model string) {
16+
RollGormBack(pk, model)
17+
RollRouterBack(pk, model)
18+
}
19+
20+
func RollGormBack(pk, model string) {
21+
22+
// 首先分析存在多少个ttt作为调用方的node块
23+
// 如果多个 仅仅删除对应块即可
24+
// 如果单个 那么还需要剔除import
25+
path := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "gorm.go")
26+
src, err := os.ReadFile(path)
27+
if err != nil {
28+
fmt.Println(err)
29+
}
30+
fileSet := token.NewFileSet()
31+
astFile, err := parser.ParseFile(fileSet, "", src, 0)
32+
if err != nil {
33+
fmt.Println(err)
34+
}
35+
var n *ast.CallExpr
36+
var k int = -1
37+
var pkNum = 0
38+
ast.Inspect(astFile, func(node ast.Node) bool {
39+
if node, ok := node.(*ast.CallExpr); ok {
40+
for i := range node.Args {
41+
pkOK := false
42+
modelOK := false
43+
ast.Inspect(node.Args[i], func(item ast.Node) bool {
44+
if ii, ok := item.(*ast.Ident); ok {
45+
if ii.Name == pk {
46+
pkOK = true
47+
pkNum++
48+
}
49+
if ii.Name == model {
50+
modelOK = true
51+
}
52+
}
53+
if pkOK && modelOK {
54+
n = node
55+
k = i
56+
}
57+
return true
58+
})
59+
}
60+
}
61+
return true
62+
})
63+
if k > 0 {
64+
n.Args = append(append([]ast.Expr{}, n.Args[:k]...), n.Args[k+1:]...)
65+
}
66+
if pkNum == 1 {
67+
var imI int = -1
68+
var gp *ast.GenDecl
69+
ast.Inspect(astFile, func(node ast.Node) bool {
70+
if gen, ok := node.(*ast.GenDecl); ok {
71+
for i := range gen.Specs {
72+
if imspec, ok := gen.Specs[i].(*ast.ImportSpec); ok {
73+
if imspec.Path.Value == "\"github.com/flipped-aurora/gin-vue-admin/server/model/"+pk+"\"" {
74+
gp = gen
75+
imI = i
76+
return false
77+
}
78+
}
79+
}
80+
}
81+
return true
82+
})
83+
84+
if imI > -1 {
85+
gp.Specs = append(append([]ast.Spec{}, gp.Specs[:imI]...), gp.Specs[imI+1:]...)
86+
}
87+
}
88+
89+
var out []byte
90+
bf := bytes.NewBuffer(out)
91+
printer.Fprint(bf, fileSet, astFile)
92+
os.Remove(path)
93+
os.WriteFile(path, bf.Bytes(), 0666)
94+
95+
}
96+
97+
func RollRouterBack(pk, model string) {
98+
99+
// 首先抓到所有的代码块结构 {}
100+
// 分析结构中是否存在一个变量叫做 pk+Router
101+
// 然后获取到代码块指针 对内部需要回滚的代码进行剔除
102+
path := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "router.go")
103+
src, err := os.ReadFile(path)
104+
if err != nil {
105+
fmt.Println(err)
106+
}
107+
fileSet := token.NewFileSet()
108+
astFile, err := parser.ParseFile(fileSet, "", src, 0)
109+
if err != nil {
110+
fmt.Println(err)
111+
}
112+
113+
var block *ast.BlockStmt
114+
ast.Inspect(astFile, func(node ast.Node) bool {
115+
if n, ok := node.(*ast.BlockStmt); ok {
116+
ast.Inspect(n, func(bNode ast.Node) bool {
117+
if in, ok := bNode.(*ast.Ident); ok {
118+
if in.Name == pk+"Router" {
119+
block = n
120+
return false
121+
}
122+
}
123+
return true
124+
})
125+
return true
126+
}
127+
return true
128+
})
129+
var k int
130+
for i := range block.List {
131+
if stmtNode, ok := block.List[i].(*ast.ExprStmt); ok {
132+
ast.Inspect(stmtNode, func(node ast.Node) bool {
133+
if n, ok := node.(*ast.Ident); ok {
134+
if n.Name == "Init"+model+"Router" {
135+
k = i
136+
return false
137+
}
138+
}
139+
return true
140+
})
141+
}
142+
}
143+
144+
block.List = append(append([]ast.Stmt{}, block.List[:k]...), block.List[k+1:]...)
145+
146+
if len(block.List) == 1 {
147+
// 说明这个块就没任何意义了
148+
block.List = nil
149+
// TODO 删除空的{}
150+
}
151+
152+
var out []byte
153+
bf := bytes.NewBuffer(out)
154+
printer.Fprint(bf, fileSet, astFile)
155+
os.Remove(path)
156+
os.WriteFile(path, bf.Bytes(), 0666)
157+
}

server/utils/ast/ast_rollback_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ast
2+
3+
import "testing"
4+
5+
func TestRollRouterBack(t *testing.T) {
6+
RollRouterBack("ttt", "Testttt")
7+
}
8+
9+
func TestRollGormBack(t *testing.T) {
10+
RollGormBack("ttt", "Testttt")
11+
}

0 commit comments

Comments
 (0)