add
wangpengfei
2023-08-25 9f98932726cb41697fabccbbbd876205e7255c95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package ast
 
import (
    "bytes"
    "fmt"
    "go/ast"
    "go/parser"
    "go/printer"
    "go/token"
    "os"
    "strings"
)
 
func AppendNodeToList(stmts []ast.Stmt, stmt ast.Stmt, index int) []ast.Stmt {
    return append(stmts[:index], append([]ast.Stmt{stmt}, stmts[index:]...)...)
}
 
func AddRouterCode(path, funcName, pk, model string) {
    src, err := os.ReadFile(path)
    if err != nil {
        fmt.Println(err)
    }
    fileSet := token.NewFileSet()
    astFile, err := parser.ParseFile(fileSet, "", src, parser.ParseComments)
 
    if err != nil {
        fmt.Println(err)
    }
 
    FuncNode := FindFunction(astFile, funcName)
 
    pkName := strings.ToUpper(pk[:1]) + pk[1:]
    routerName := fmt.Sprintf("%sRouter", pk)
    modelName := fmt.Sprintf("Init%sRouter", model)
    var bloctPre *ast.BlockStmt
    for i := len(FuncNode.Body.List) - 1; i >= 0; i-- {
        if block, ok := FuncNode.Body.List[i].(*ast.BlockStmt); ok {
            bloctPre = block
        }
    }
    ast.Print(fileSet, FuncNode)
    if ok, b := needAppendRouter(FuncNode, pk); ok {
        routerNode :=
            &ast.BlockStmt{
                List: []ast.Stmt{
                    &ast.AssignStmt{
                        Lhs: []ast.Expr{
                            &ast.Ident{Name: routerName},
                        },
                        Tok: token.DEFINE,
                        Rhs: []ast.Expr{
                            &ast.SelectorExpr{
                                X: &ast.SelectorExpr{
                                    X:   &ast.Ident{Name: "router"},
                                    Sel: &ast.Ident{Name: "RouterGroupApp"},
                                },
                                Sel: &ast.Ident{Name: pkName},
                            },
                        },
                    },
                },
            }
 
        FuncNode.Body.List = AppendNodeToList(FuncNode.Body.List, routerNode, len(FuncNode.Body.List)-2)
        bloctPre = routerNode
    } else {
        bloctPre = b
    }
 
    if needAppendInit(FuncNode, routerName, modelName) {
        bloctPre.List = append(bloctPre.List,
            &ast.ExprStmt{
                X: &ast.CallExpr{
                    Fun: &ast.SelectorExpr{
                        X:   &ast.Ident{Name: routerName},
                        Sel: &ast.Ident{Name: modelName},
                    },
                    Args: []ast.Expr{
                        &ast.Ident{
                            Name: "PrivateGroup",
                        },
                    },
                },
            })
    }
    var out []byte
    bf := bytes.NewBuffer(out)
    printer.Fprint(bf, fileSet, astFile)
    os.WriteFile(path, bf.Bytes(), 0666)
}
 
func needAppendRouter(funcNode ast.Node, pk string) (bool, *ast.BlockStmt) {
    flag := true
    var block *ast.BlockStmt
    ast.Inspect(funcNode, func(node ast.Node) bool {
        switch n := node.(type) {
        case *ast.BlockStmt:
            for i := range n.List {
                if assignNode, ok := n.List[i].(*ast.AssignStmt); ok {
                    if identNode, ok := assignNode.Lhs[0].(*ast.Ident); ok {
                        if identNode.Name == fmt.Sprintf("%sRouter", pk) {
                            flag = false
                            block = n
                            return false
                        }
                    }
                }
            }
 
        }
        return true
    })
    return flag, block
}
 
func needAppendInit(funcNode ast.Node, routerName string, modelName string) bool {
    flag := true
    ast.Inspect(funcNode, func(node ast.Node) bool {
        switch n := funcNode.(type) {
        case *ast.CallExpr:
            if selectNode, ok := n.Fun.(*ast.SelectorExpr); ok {
                x, xok := selectNode.X.(*ast.Ident)
                if xok && x.Name == routerName && selectNode.Sel.Name == modelName {
                    flag = false
                    return false
                }
            }
        }
        return true
    })
    return flag
}