feat: screw the recompile, push it to the user.
This commit is contained in:
parent
0e0950172d
commit
ea31b462d8
3 changed files with 31 additions and 21 deletions
16
main.go
16
main.go
|
@ -26,10 +26,9 @@ func New(tengo *Tengo ) *Preprocessor {
|
|||
|
||||
func (pp *Preprocessor) Process(
|
||||
ctx context.Context,
|
||||
recompile bool,
|
||||
filePath string,
|
||||
data []byte,
|
||||
) ([]byte, error) {
|
||||
) (*Compiled, []byte, error) {
|
||||
//var b bytes.Buffer
|
||||
preCode := []byte(nil)
|
||||
texts := [][]byte{}
|
||||
|
@ -38,7 +37,7 @@ func (pp *Preprocessor) Process(
|
|||
if bytes.HasPrefix(data, pref) {
|
||||
idxEnd := bytes.Index(data, pp.tags[1])
|
||||
if idxEnd < 0 {
|
||||
return nil, UnexpectedError{
|
||||
return nil, nil, UnexpectedError{
|
||||
What: "pre-code start tag",
|
||||
}
|
||||
}
|
||||
|
@ -52,14 +51,14 @@ func (pp *Preprocessor) Process(
|
|||
//fmt.Printf("cock %d %d %d\n", last, idxStart, idxEnd)
|
||||
if idxStart < 0 {
|
||||
if idxEnd >= 0 {
|
||||
return nil, UnexpectedError{
|
||||
return nil, nil, UnexpectedError{
|
||||
What: "end tag",
|
||||
}
|
||||
}
|
||||
texts = append(texts, data)
|
||||
break
|
||||
} else if idxEnd < 0 {
|
||||
return nil, UnexpectedError{
|
||||
return nil, nil, UnexpectedError{
|
||||
What: "start tag",
|
||||
}
|
||||
}
|
||||
|
@ -73,18 +72,17 @@ func (pp *Preprocessor) Process(
|
|||
data = data[idxEnd + len(pp.tags[1]):]
|
||||
}
|
||||
|
||||
ret, err := pp.tengo.Eval(
|
||||
compiled, ret, err := pp.tengo.Eval(
|
||||
ctx,
|
||||
recompile,
|
||||
filePath,
|
||||
texts,
|
||||
preCode,
|
||||
codes,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
return compiled, ret, nil
|
||||
}
|
||||
|
||||
|
|
33
tengo.go
33
tengo.go
|
@ -8,12 +8,15 @@ import (
|
|||
"strconv"
|
||||
)
|
||||
|
||||
type CompiledMap map[string] *Compiled
|
||||
|
||||
type Script = tengo.Script
|
||||
type Compiled = tengo.Compiled
|
||||
|
||||
// The type describes
|
||||
// customizable way to evaluate
|
||||
// files.
|
||||
type Tengo struct {
|
||||
head string
|
||||
compiledScripts map[string] *Compiled
|
||||
// Functions to modify
|
||||
// preprocessor for
|
||||
// more specific purposes.
|
||||
|
@ -44,15 +47,18 @@ func (tengo *Tengo) SetPreCompile(fn func(context.Context, *Script)) *Tengo {
|
|||
return tengo
|
||||
}
|
||||
|
||||
// Simple Evaler implementation for the Tengo language
|
||||
func (pp *Tengo) Eval(
|
||||
ctx context.Context,
|
||||
recompile bool,
|
||||
// File path to give it.
|
||||
filePath string,
|
||||
// Static text pieces.
|
||||
texts [][]byte,
|
||||
// The code that comes before any
|
||||
// dynamic code.
|
||||
preCode []byte,
|
||||
// General code.
|
||||
codes [][]byte,
|
||||
) ([]byte, error) {
|
||||
) (*Compiled, []byte, error) {
|
||||
var fullCodeBuf, retBuf bytes.Buffer
|
||||
const retHead = `
|
||||
context := {}
|
||||
|
@ -68,8 +74,6 @@ func (pp *Tengo) Eval(
|
|||
const retSeparator = `
|
||||
__separate__()
|
||||
`
|
||||
//rets := [][]byte{}
|
||||
|
||||
|
||||
fmt.Fprint(&fullCodeBuf, retHead)
|
||||
|
||||
|
@ -193,10 +197,19 @@ func (pp *Tengo) Eval(
|
|||
},
|
||||
})
|
||||
|
||||
_, err := script.RunContext(ctx)
|
||||
compiled, err := script.Compile()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
// To keep everything of the changes.
|
||||
compiled = compiled.Clone()
|
||||
|
||||
_, err = script.RunContext(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return retBuf.Bytes(), nil
|
||||
return compiled, retBuf.Bytes(), nil
|
||||
}
|
||||
|
||||
|
||||
|
|
3
tool.go
3
tool.go
|
@ -42,9 +42,8 @@ var Tool = mtool.T("tpp").Func(func(flags *mtool.Flags){
|
|||
log.Println("read error:", err)
|
||||
continue
|
||||
}
|
||||
out, err := pp.Process(
|
||||
_, out, err := pp.Process(
|
||||
context.Background(),
|
||||
true,
|
||||
pth,
|
||||
bts,
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue