reduce duplication in hookgen

This commit is contained in:
Azareal 2020-05-24 09:16:17 +10:00
parent ca7c5dc8fe
commit 3ac9036366
3 changed files with 91 additions and 148 deletions

View File

@ -0,0 +1,80 @@
package hookgen
import (
"log"
"os"
"bytes"
"text/template"
)
type HookVars struct {
Imports []string
Hooks []Hook
}
type Hook struct {
Name string
Params string
Params2 string
Ret string
Type string
Any bool
}
func AddHooks(add func(name, params, ret, htype string)) {
vhookskip := func(name, params string) {
add(name,params,"(bool, RouteError)","VhookSkippable_")
}
vhookskip("forum_check_pre_perms","w http.ResponseWriter,r *http.Request,u *User,fid *int,h *Header")
vhookskip("router_after_filters","w http.ResponseWriter,r *http.Request,prefix string")
vhookskip("router_pre_route","w http.ResponseWriter,r *http.Request,u *User,prefix string")
vhookskip("route_forum_list_start","w http.ResponseWriter,r *http.Request,u *User,h *Header")
vhookskip("route_topic_list_start","w http.ResponseWriter,r *http.Request,u *User,h *Header")
vhooknoret := func(name, params string) {
add(name,params,"","Vhooks")
}
vhooknoret("router_end","w http.ResponseWriter,r *http.Request,u *User,prefix string, extraData string")
vhooknoret("topic_reply_row_assign","r *ReplyUser")
}
func Write(hookVars HookVars) {
fileData := `// Code generated by Gosora's Hook Generator. DO NOT EDIT.
/* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */
package common
import ({{range .Imports}}
"{{.}}"{{end}}
)
{{range .Hooks}}
func H_{{.Name}}_hook(t *HookTable, {{.Params}}) {{.Ret}} { {{if .Any}}
hook := t.{{.Type}}["{{.Name}}"]
if hook != nil {
{{if .Ret}}return {{end}}hook({{.Params2}})
} {{end}}
{{if .Ret}}return false, nil{{end}}
}{{end}}
`
tmpl := template.Must(template.New("hooks").Parse(fileData))
var b bytes.Buffer
if e := tmpl.Execute(&b, hookVars); e != nil {
log.Fatal(e)
}
err := writeFile("./common/gen_extend.go", string(b.Bytes()))
if err != nil {
log.Fatal(err)
}
}
func writeFile(name, body string) error {
f, e := os.Create(name)
if e != nil {
return e
}
if _, e = f.WriteString(body); e != nil {
return e
}
if e = f.Sync(); e != nil {
return e
}
return f.Close()
}

View File

@ -5,30 +5,14 @@ package main // import "github.com/Azareal/Gosora/hook_gen"
import (
"fmt"
"log"
"os"
"bytes"
"strings"
"runtime/debug"
"text/template"
_ "github.com/Azareal/Gosora/extend"
c "github.com/Azareal/Gosora/common"
h "github.com/Azareal/Gosora/cmd/common_hook_gen"
)
type HookVars struct {
Imports []string
Hooks []Hook
}
type Hook struct {
Name string
Params string
Params2 string
Ret string
Type string
Any bool
}
// 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
@ -64,7 +48,7 @@ func main() {
log.Printf("hooks: %+v\n", hooks)
imports := []string{"net/http"}
hookVars := HookVars{imports,nil}
hookVars := h.HookVars{imports,nil}
add := func(name, params, ret, htype string) {
var params2 string
first := true
@ -76,63 +60,10 @@ func main() {
params2 += pspl[0]
first = false
}
hookVars.Hooks = append(hookVars.Hooks, Hook{name, params, params2, ret, htype, hooks[name] > 0})
hookVars.Hooks = append(hookVars.Hooks, h.Hook{name, params, params2, ret, htype, hooks[name] > 0})
}
vhookskip := func(name, params string) {
add(name,params,"(bool, RouteError)","VhookSkippable_")
}
vhookskip("forum_check_pre_perms","w http.ResponseWriter,r *http.Request,u *User,fid *int,h *Header")
vhookskip("router_after_filters","w http.ResponseWriter,r *http.Request,prefix string")
vhookskip("router_pre_route","w http.ResponseWriter,r *http.Request,u *User,prefix string")
vhookskip("route_forum_list_start","w http.ResponseWriter,r *http.Request,u *User,h *Header")
vhookskip("route_topic_list_start","w http.ResponseWriter,r *http.Request,u *User,h *Header")
vhooknoret := func(name, params string) {
add(name,params,"","Vhooks")
}
vhooknoret("router_end","w http.ResponseWriter,r *http.Request,u *User,prefix string,extraData string")
vhooknoret("topic_reply_row_assign","r *ReplyUser")
fileData := `// Code generated by Gosora's Hook Generator. DO NOT EDIT.
/* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */
package common
import ({{range .Imports}}
"{{.}}"{{end}}
)
{{range .Hooks}}
func H_{{.Name}}_hook(t *HookTable, {{.Params}}) {{.Ret}} { {{if .Any}}
hook := t.{{.Type}}["{{.Name}}"]
if hook != nil {
{{if .Ret}}return {{end}}hook({{.Params2}})
} {{end}}
{{if .Ret}}return false, nil{{end}}
}{{end}}
`
tmpl := template.Must(template.New("hooks").Parse(fileData))
var b bytes.Buffer
if e := tmpl.Execute(&b, hookVars); e != nil {
log.Fatal(e)
}
writeFile("./common/gen_extend.go", string(b.Bytes()))
h.AddHooks(add)
h.Write(hookVars)
log.Println("Successfully generated the hooks")
}
func writeFile(name, content string) {
f, err := os.Create(name)
if err != nil {
log.Fatal(err)
}
_, err = f.WriteString(content)
if err != nil {
log.Fatal(err)
}
err = f.Sync()
if err != nil {
log.Fatal(err)
}
err = f.Close()
if err != nil {
log.Fatal(err)
}
}

View File

@ -3,27 +3,12 @@ package main // import "github.com/Azareal/Gosora/hook_stub_gen"
import (
"fmt"
"log"
"os"
"bytes"
"strings"
"runtime/debug"
"text/template"
h "github.com/Azareal/Gosora/cmd/common_hook_gen"
)
type HookVars struct {
Imports []string
Hooks []Hook
}
type Hook struct {
Name string
Params string
Params2 string
Ret string
Type string
Any bool
}
// 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
@ -36,7 +21,7 @@ func main() {
}()
imports := []string{"net/http"}
hookVars := HookVars{imports,nil}
hookVars := h.HookVars{imports,nil}
add := func(name, params, ret, htype string) {
var params2 string
first := true
@ -48,63 +33,10 @@ func main() {
params2 += pspl[0]
first = false
}
hookVars.Hooks = append(hookVars.Hooks, Hook{name, params, params2, ret, htype, true})
}
vhookskip := func(name, params string) {
add(name,params,"(bool, RouteError)","VhookSkippable_")
}
vhookskip("forum_check_pre_perms","w http.ResponseWriter,r *http.Request,u *User,fid *int,h *Header")
vhookskip("router_after_filters","w http.ResponseWriter,r *http.Request,prefix string")
vhookskip("router_pre_route","w http.ResponseWriter,r *http.Request,u *User,prefix string")
vhookskip("route_forum_list_start","w http.ResponseWriter,r *http.Request,u *User,h *Header")
vhookskip("route_topic_list_start","w http.ResponseWriter,r *http.Request,u *User,h *Header")
vhooknoret := func(name, params string) {
add(name,params,"","Vhooks")
}
vhooknoret("router_end","w http.ResponseWriter,r *http.Request,u *User,prefix string, extraData string")
vhooknoret("topic_reply_row_assign","r *ReplyUser")
fileData := `// Code generated by Gosora's Hook Generator. DO NOT EDIT.
/* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */
package common
import ({{range .Imports}}
"{{.}}"{{end}}
)
{{range .Hooks}}
func H_{{.Name}}_hook(t *HookTable, {{.Params}}) {{.Ret}} {
{{if .Any}}hook := t.{{.Type}}["{{.Name}}"]
if hook != nil {
{{if .Ret}}return {{end}}hook({{.Params2}})
} {{end}}
{{if .Ret}}return false, nil{{end}}
}{{end}}
`
tmpl := template.Must(template.New("hooks").Parse(fileData))
var b bytes.Buffer
if e := tmpl.Execute(&b, hookVars); e != nil {
log.Fatal(e)
hookVars.Hooks = append(hookVars.Hooks, h.Hook{name, params, params2, ret, htype, true})
}
writeFile("./common/gen_extend.go", string(b.Bytes()))
h.AddHooks(add)
h.Write(hookVars)
log.Println("Successfully generated the hooks")
}
func writeFile(name, content string) {
f, err := os.Create(name)
if err != nil {
log.Fatal(err)
}
_, err = f.WriteString(content)
if err != nil {
log.Fatal(err)
}
err = f.Sync()
if err != nil {
log.Fatal(err)
}
err = f.Close()
if err != nil {
log.Fatal(err)
}
}