-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_bot_events.go
167 lines (145 loc) · 3.58 KB
/
gen_bot_events.go
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/labstack/gommon/log"
"github.com/mylukin/EchoPilot/helper"
)
// Generate Bot Events
func GenBotEvents(module string, outFile string) error {
events := []string{}
if err := filepath.Walk("./app", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
// ignore not .go
if filepath.Ext(path) != ".go" {
return nil
}
// Don't extract from test files.
if strings.HasSuffix(path, "_test.go") {
return nil
}
buf, err := os.ReadFile(path)
if err != nil {
return err
}
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, path, buf, parser.AllErrors)
if err != nil {
return err
}
// file package name
filePackName := file.Name.Name
currentPackName := getCurrentPackName(file)
ast.Inspect(file, func(n ast.Node) bool {
switch v := n.(type) {
case *ast.CallExpr:
if fn, ok := v.Fun.(*ast.SelectorExpr); ok {
var packName string
if pack, ok := fn.X.(*ast.Ident); ok {
packName = pack.Name
}
if packName != currentPackName {
return true
}
funcName := fn.Sel.Name
if funcName != "SetFSMValue" {
return true
}
var FSMValue *ast.CompositeLit
if FSMValue, ok = v.Args[1].(*ast.CompositeLit); !ok {
return false
}
for _, elt := range FSMValue.Elts {
var keyValue *ast.KeyValueExpr
if keyValue, ok = elt.(*ast.KeyValueExpr); !ok {
continue
}
keyName := keyValue.Key.(*ast.Ident).Name
if keyName != "NextFn" {
continue
}
var nextFn string
if se, ok := keyValue.Value.(*ast.SelectorExpr); ok {
var ppname string
if pack, ok := se.X.(*ast.Ident); ok {
ppname = pack.Name
}
nextFn = fmt.Sprintf(`%s.%s`, ppname, se.Sel.Name)
}
if ident, ok := keyValue.Value.(*ast.Ident); ok {
nextFn = fmt.Sprintf(`%s.%s`, filePackName, ident.Name)
}
if nextFn != "" && !helper.ValueInSlice(nextFn, events) {
events = append(events, nextFn)
}
log.Infof("%s.%s: %s, %v", packName, funcName, keyName, nextFn)
}
}
}
return true
})
return nil
}); err != nil {
return err
}
var tmpl = template.Must(template.New("i18n").Parse(`// Code generated - DO NOT EDIT.
// This file is a generated binding and any manual changes will be lost.
package routers
import (
{{if .Data}}app "{{.Package}}/app"{{end}}
"github.com/labstack/echo/v4"
)
// BotFSMEvents is bot FSM events
var BotFSMEvents = []echo.HandlerFunc{
{{- range $k, $v := .Data }}
{{$v}},
{{- end }}
}
`))
goFile, err := os.Create(outFile)
if err != nil {
log.Fatal(err)
return err
}
return tmpl.Execute(goFile, struct {
Data []string
Package string
}{
events,
module,
})
}
// getCurrentPackName
func getCurrentPackName(file *ast.File) string {
for _, i := range file.Imports {
if i.Path.Kind == token.STRING && i.Path.Value == `"github.com/mylukin/EchoPilot"` {
if i.Name == nil {
return removeQuotesAndExtractLastPart(i.Path.Value)
}
return i.Name.Name
}
}
return ""
}
// removeQuotesAndExtractLastPart 会去除字符串两边的双引号,并返回最后一个斜杠后面的部分。
func removeQuotesAndExtractLastPart(input string) string {
// 去除两边的双引号
trimmed := strings.Trim(input, "\"")
// 分割字符串,获取最后一个部分
parts := strings.Split(trimmed, "/")
if len(parts) == 0 {
return ""
}
return parts[len(parts)-1]
}