gopp/tool.go

58 lines
947 B
Go
Raw Permalink Normal View History

package tpp
2024-02-26 00:22:09 +03:00
import (
//"github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/stdlib"
2024-05-20 20:34:11 +03:00
"surdeus.su/core/cli/mtool"
//"fmt"
2024-02-26 00:22:09 +03:00
"os"
"log"
"path/filepath"
"context"
2024-02-26 00:22:09 +03:00
)
2024-06-09 15:35:28 +03:00
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)
2024-06-09 15:35:28 +03:00
s.SetImportDir(modDir)
})
2024-06-09 15:35:28 +03:00
pp := New(t)
2024-02-26 00:22:09 +03:00
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,
)
2024-02-26 00:22:09 +03:00
if err != nil {
log.Println("pp error:", err)
continue
}
os.Stdout.Write(out)
2024-02-26 00:22:09 +03:00
}
2024-06-09 15:35:28 +03:00
}).Usage("[files]")