gosora/cmd/hook_gen/main.go

69 lines
1.6 KiB
Go
Raw Normal View History

2020-05-23 22:24:23 +00:00
// +build hookgen
package main // import "github.com/Azareal/Gosora/hook_gen"
import (
"fmt"
"log"
"strings"
"runtime/debug"
_ "github.com/Azareal/Gosora/extend"
c "github.com/Azareal/Gosora/common"
2020-05-23 23:16:17 +00:00
h "github.com/Azareal/Gosora/cmd/common_hook_gen"
2020-05-23 22:24:23 +00:00
)
// TODO: Make sure all the errors in this file propagate upwards properly
func main() {
// Capture panics instead of closing the window at a superhuman speed before the user can read the message on Windows
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
debug.PrintStack()
return
}
}()
hooks := make(map[string]int)
for _, pl := range c.Plugins {
if len(pl.Meta.Hooks) > 0 {
for _, hook := range pl.Meta.Hooks {
hooks[hook]++
}
continue
}
if pl.Init != nil {
if e := pl.Init(pl); e != nil {
log.Print("early plugin init err: ", e)
return
}
}
if pl.Hooks != nil {
log.Print("Hooks not nil for ", pl.UName)
for hook, _ := range pl.Hooks {
hooks[hook] += 1
}
}
}
log.Printf("hooks: %+v\n", hooks)
imports := []string{"net/http"}
2020-05-23 23:16:17 +00:00
hookVars := h.HookVars{imports,nil}
2020-05-23 22:24:23 +00:00
add := func(name, params, ret, htype string) {
var params2 string
first := true
for _, param := range strings.Split(params,",") {
if !first {
params2 += ","
}
pspl := strings.Split(strings.ReplaceAll(strings.TrimSpace(param)," "," ")," ")
params2 += pspl[0]
first = false
}
2020-05-23 23:16:17 +00:00
hookVars.Hooks = append(hookVars.Hooks, h.Hook{name, params, params2, ret, htype, hooks[name] > 0})
2020-05-23 22:24:23 +00:00
}
2020-05-23 23:16:17 +00:00
h.AddHooks(add)
h.Write(hookVars)
2020-05-23 22:24:23 +00:00
log.Println("Successfully generated the hooks")
}