-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_event.go
238 lines (198 loc) · 6.39 KB
/
gen_event.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"unicode"
)
func genEvent(ctx *CommandContext) {
eventFile := ctx.EventDir
if eventFile == "" {
eventFile = strings.TrimSuffix(ctx.DeclFile, ".go") + "_code.go"
} else {
eventFile = strings.Join([]string{filepath.Dir(ctx.DeclFile), ctx.EventDir, filepath.Base(strings.TrimSuffix(ctx.DeclFile, ".go")) + "_code.go"}, string(filepath.Separator))
}
code := &bytes.Buffer{}
// 生成注释
{
program := strings.TrimSuffix(filepath.Base(os.Args[0]), filepath.Ext(os.Args[0]))
args := strings.Join(os.Args[1:], " ")
fmt.Fprintf(code, `// Code generated by %s %s; DO NOT EDIT.
package %s
`, program, args, ctx.EventPackage)
}
// 生成import
{
importCode := &bytes.Buffer{}
fmt.Fprintf(importCode, "\nimport (")
fmt.Fprintf(importCode, `
"fmt"
%s "%s"
%s "%s"`, ctx.PackageEventAlias, packageEventPath, ctx.PackageIfaceAlias, packageIfacePath)
for _, imp := range ctx.FileAst.Imports {
begin := ctx.FileSet.Position(imp.Pos())
end := ctx.FileSet.Position(imp.End())
impStr := string(ctx.FileData[begin.Offset:end.Offset])
switch imp.Path.Value {
case fmt.Sprintf(`"%s"`, packageEventPath):
if imp.Name == nil {
if ctx.PackageEventAlias == "event" {
continue
}
} else {
if imp.Name.Name == ctx.PackageEventAlias {
continue
}
}
case fmt.Sprintf(`"%s"`, packageIfacePath):
if imp.Name == nil {
if ctx.PackageIfaceAlias == "iface" {
continue
}
} else {
if imp.Name.Name == ctx.PackageIfaceAlias {
continue
}
}
}
fmt.Fprintf(importCode, "\n\t%s", impStr)
}
fmt.Fprintf(importCode, "\n)\n")
fmt.Fprintf(code, importCode.String())
}
// 解析事件定义
eventDeclTab := EventDeclTab{}
eventDeclTab.Parse(ctx)
// event包前缀
eventPrefix := ""
if ctx.PackageEventAlias != "." {
eventPrefix = ctx.PackageEventAlias + "."
}
// iface包前缀
ifacePrefix := ""
if ctx.PackageIfaceAlias != "." {
ifacePrefix = ctx.PackageIfaceAlias + "."
}
// 生成事件发送代码
for _, eventDecl := range eventDeclTab {
// 是否导出事件发送代码
exportEmitStr := "_Emit"
if ctx.EventDefExport {
exportEmitStr = "Emit"
}
if strings.Contains(eventDecl.Comment, "[EmitExport]") {
exportEmitStr = "Emit"
} else if strings.Contains(eventDecl.Comment, "[EmitUnExport]") {
exportEmitStr = "_Emit"
}
auto := ctx.EventDefAuto
if strings.Contains(eventDecl.Comment, "[EmitAuto]") {
auto = true
} else if strings.Contains(eventDecl.Comment, "[EmitManual]") {
auto = false
}
var visibility string
if unicode.IsLower(rune(eventDecl.Name[0])) {
visibility = "_"
}
// 生成代码
if auto {
if eventDecl.FuncHasRet {
fmt.Fprintf(code, `
type _Auto%[1]s interface {
%[1]s() %[6]sIEvent
}
func Bind%[1]s(auto _Auto%[1]s, subscriber %[2]s%[8]s, priority ...int32) %[6]sHook {
if auto == nil {
panic(fmt.Errorf("%%w: %%w: auto is nil", %[6]sErrEvent, %[6]sErrArgs))
}
return %[6]sBindEvent[%[2]s%[8]s](auto.%[1]s(), subscriber, priority...)
}
func %[9]s%[1]s%[7]s(auto _Auto%[1]s%[4]s) {
if auto == nil {
panic(fmt.Errorf("%%w: %%w: auto is nil", %[6]sErrEvent, %[6]sErrArgs))
}
%[6]sUnsafeEvent(auto.%[1]s()).Emit(func(subscriber %[10]sCache) bool {
return %[10]sCache2Iface[%[2]s%[8]s](subscriber).%[3]s(%[5]s)
})
}
`, strings.Title(eventDecl.Name), eventDecl.Name, eventDecl.FuncName, eventDecl.FuncParamsDecl, eventDecl.FuncParams, eventPrefix, eventDecl.FuncTypeParamsDecl, eventDecl.FuncTypeParams, exportEmitStr, ifacePrefix)
} else {
fmt.Fprintf(code, `
type _Auto%[1]s interface {
%[1]s() %[6]sIEvent
}
func Bind%[1]s(auto _Auto%[1]s, subscriber %[2]s%[8]s, priority ...int32) %[6]sHook {
if auto == nil {
panic(fmt.Errorf("%%w: %%w: auto is nil", %[6]sErrEvent, %[6]sErrArgs))
}
return %[6]sBindEvent[%[2]s%[8]s](auto.%[1]s(), subscriber, priority...)
}
func %[9]s%[1]s%[7]s(auto _Auto%[1]s%[4]s) {
if auto == nil {
panic(fmt.Errorf("%%w: %%w: auto is nil", %[6]sErrEvent, %[6]sErrArgs))
}
%[6]sUnsafeEvent(auto.%[1]s()).Emit(func(subscriber %[10]sCache) bool {
%[10]sCache2Iface[%[2]s%[8]s](subscriber).%[3]s(%[5]s)
return true
})
}
`, strings.Title(eventDecl.Name), eventDecl.Name, eventDecl.FuncName, eventDecl.FuncParamsDecl, eventDecl.FuncParams, eventPrefix, eventDecl.FuncTypeParamsDecl, eventDecl.FuncTypeParams, exportEmitStr, ifacePrefix)
}
} else {
if eventDecl.FuncHasRet {
fmt.Fprintf(code, `
func %[9]s%[1]s%[7]s(evt %[6]sIEvent%[4]s) {
if evt == nil {
panic(fmt.Errorf("%%w: %%w: evt is nil", %[6]sErrEvent, %[6]sErrArgs))
}
%[6]sUnsafeEvent(evt).Emit(func(subscriber %[10]sCache) bool {
return %[10]sCache2Iface[%[2]s%[8]s](subscriber).%[3]s(%[5]s)
})
}
`, strings.Title(eventDecl.Name), eventDecl.Name, eventDecl.FuncName, eventDecl.FuncParamsDecl, eventDecl.FuncParams, eventPrefix, eventDecl.FuncTypeParamsDecl, eventDecl.FuncTypeParams, exportEmitStr, ifacePrefix)
} else {
fmt.Fprintf(code, `
func %[9]s%[1]s%[7]s(evt %[6]sIEvent%[4]s) {
if evt == nil {
panic(fmt.Errorf("%%w: %%w: evt is nil", %[6]sErrEvent, %[6]sErrArgs))
}
%[6]sUnsafeEvent(evt).Emit(func(subscriber %[10]sCache) bool {
%[10]sCache2Iface[%[2]s%[8]s](subscriber).%[3]s(%[5]s)
return true
})
}
`, strings.Title(eventDecl.Name), eventDecl.Name, eventDecl.FuncName, eventDecl.FuncParamsDecl, eventDecl.FuncParams, eventPrefix, eventDecl.FuncTypeParamsDecl, eventDecl.FuncTypeParams, exportEmitStr, ifacePrefix)
}
}
if eventDecl.FuncHasRet {
fmt.Fprintf(code, `
func %[5]sHandle%[1]s(fun func(%[3]s) bool) %[5]s%[1]sHandler {
return %[1]sHandler(fun)
}
type %[5]s%[1]sHandler func(%[3]s) bool
func (h %[5]s%[1]sHandler) %[2]s(%[3]s) bool {
return h(%[4]s)
}
`, strings.Title(eventDecl.Name), eventDecl.FuncName, strings.TrimLeft(eventDecl.FuncParamsDecl, ", "), eventDecl.FuncParams, visibility)
} else {
fmt.Fprintf(code, `
func %[5]sHandle%[1]s(fun func(%[3]s)) %[5]s%[1]sHandler {
return %[5]s%[1]sHandler(fun)
}
type %[5]s%[1]sHandler func(%[3]s)
func (h %[5]s%[1]sHandler) %[2]s(%[3]s) {
h(%[4]s)
}
`, strings.Title(eventDecl.Name), eventDecl.FuncName, strings.TrimLeft(eventDecl.FuncParamsDecl, ", "), eventDecl.FuncParams, visibility)
}
fmt.Printf("Emit: %s\n", eventDecl.Name)
}
os.MkdirAll(filepath.Dir(eventFile), os.ModePerm)
if err := ioutil.WriteFile(eventFile, code.Bytes(), os.ModePerm); err != nil {
panic(err)
}
}