2024-04-25 22:38:04 +03:00
|
|
|
package tpp
|
2024-02-26 00:22:09 +03:00
|
|
|
|
|
|
|
import (
|
2024-04-25 22:38:04 +03:00
|
|
|
//"github.com/d5/tengo/v2"
|
|
|
|
"github.com/d5/tengo/v2/stdlib"
|
2024-05-20 20:34:11 +03:00
|
|
|
"surdeus.su/core/cli/mtool"
|
2024-04-25 22:38:04 +03:00
|
|
|
//"fmt"
|
2024-02-26 00:22:09 +03:00
|
|
|
"os"
|
|
|
|
"log"
|
|
|
|
"path/filepath"
|
2024-04-25 22:38:04 +03:00
|
|
|
"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()
|
|
|
|
|
2024-04-25 22:38:04 +03:00
|
|
|
t := NewTengo().
|
2024-05-06 21:08:56 +03:00
|
|
|
SetPreCompile(func(ctx context.Context, s *Script){
|
2024-04-25 22:38:04 +03:00
|
|
|
s.SetImports(stdlib.GetModuleMap(
|
|
|
|
stdlib.AllModuleNames()...,
|
|
|
|
))
|
|
|
|
s.EnableFileImport(true)
|
2024-06-09 15:35:28 +03:00
|
|
|
s.SetImportDir(modDir)
|
2024-04-25 22:38:04 +03:00
|
|
|
})
|
2024-06-09 15:35:28 +03:00
|
|
|
|
2024-04-25 22:38:04 +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
|
|
|
|
}
|
2024-06-23 13:04:37 +03:00
|
|
|
_, out, err := pp.Process(
|
2024-04-25 22:38:04 +03:00
|
|
|
context.Background(),
|
|
|
|
pth,
|
|
|
|
bts,
|
|
|
|
)
|
2024-02-26 00:22:09 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("pp error:", err)
|
|
|
|
continue
|
|
|
|
}
|
2024-04-25 22:38:04 +03:00
|
|
|
os.Stdout.Write(out)
|
2024-02-26 00:22:09 +03:00
|
|
|
}
|
2024-06-09 15:35:28 +03:00
|
|
|
}).Usage("[files]")
|