Parse prior hooks.go, if present (prep for more changes).
This commit is contained in:
Родитель
5b1d662d5a
Коммит
9e86b749a5
|
@ -2,6 +2,10 @@ package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"go/ast"
|
||||||
|
"go/parser"
|
||||||
|
"go/printer"
|
||||||
|
"go/token"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/Unity-Technologies/truss/gengokit"
|
"github.com/Unity-Technologies/truss/gengokit"
|
||||||
|
@ -12,25 +16,37 @@ const HookPath = "handlers/hooks.gotemplate"
|
||||||
|
|
||||||
// NewHook returns a new HookRender
|
// NewHook returns a new HookRender
|
||||||
func NewHook(prev io.Reader) (gengokit.Renderable, error) {
|
func NewHook(prev io.Reader) (gengokit.Renderable, error) {
|
||||||
return &HookRender{
|
h := new(HookRender)
|
||||||
prev: prev,
|
if prev != nil {
|
||||||
}, nil
|
h.fset = token.NewFileSet()
|
||||||
|
var err error
|
||||||
|
h.ast, err = parser.ParseFile(h.fset, "", prev, parser.ParseComments)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return h, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type HookRender struct {
|
type HookRender struct {
|
||||||
prev io.Reader
|
fset *token.FileSet
|
||||||
|
ast *ast.File
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render will return the existing file if it exists, otherwise it will return
|
// Render will return the existing file if it exists, otherwise it will return
|
||||||
// a brand new copy from the template.
|
// a brand new copy from the template.
|
||||||
func (h *HookRender) Render(_ string, _ *gengokit.Data) (io.Reader, error) {
|
func (h *HookRender) Render(_ string, _ *gengokit.Data) (io.Reader, error) {
|
||||||
if h.prev != nil {
|
|
||||||
return h.prev, nil
|
|
||||||
}
|
|
||||||
code := new(bytes.Buffer)
|
code := new(bytes.Buffer)
|
||||||
code.WriteString(templates.HookHead)
|
if h.ast == nil {
|
||||||
for _, hd := range templates.Hooks {
|
code.WriteString(templates.HookHead)
|
||||||
code.WriteString(hd.Code)
|
for _, hd := range templates.Hooks {
|
||||||
|
code.WriteString(hd.Code)
|
||||||
|
}
|
||||||
|
return code, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := printer.Fprint(code, h.fset, h.ast); nil != err {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return code, nil
|
return code, nil
|
||||||
|
|
Загрузка…
Ссылка в новой задаче