gopp/tool.go

57 lines
947 B
Go

package tpp
import (
//"github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/stdlib"
"surdeus.su/core/cli/mtool"
//"fmt"
"os"
"log"
"path/filepath"
"context"
)
var Tool = mtool.T("tpp").Func(func(flags *mtool.Flags){
var (
modDir string
)
flags.StringVar(
&modDir,
"mod",
".",
"set the import directory",
)
filePaths := flags.Parse()
t := NewTengo().
SetPreCompile(func(ctx context.Context, s *Script){
s.SetImports(stdlib.GetModuleMap(
stdlib.AllModuleNames()...,
))
s.EnableFileImport(true)
s.SetImportDir(modDir)
})
pp := New(t)
for _, filePath := range filePaths {
pth := filepath.FromSlash(filePath)
bts, err := os.ReadFile(pth)
if err != nil {
log.Println("read error:", err)
continue
}
out, err := pp.Process(
context.Background(),
true,
pth,
bts,
)
if err != nil {
log.Println("pp error:", err)
continue
}
os.Stdout.Write(out)
}
}).Usage("[files]")