79 lines
1.4 KiB
Go
79 lines
1.4 KiB
Go
package gopp
|
|
|
|
import (
|
|
//"github.com/d5/tengo/v2"
|
|
"surdeus.su/core/xgo/v2/stdlib"
|
|
"surdeus.su/core/cli/mtool"
|
|
//"fmt"
|
|
"os"
|
|
"log"
|
|
"path/filepath"
|
|
"context"
|
|
)
|
|
|
|
var Tool = mtool.T("gopp").Func(func(flags *mtool.Flags){
|
|
var (
|
|
modDir string
|
|
render bool
|
|
)
|
|
|
|
log.SetFlags(0)
|
|
flags.StringVar(
|
|
&modDir,
|
|
"mod",
|
|
".",
|
|
"set the import directory",
|
|
)
|
|
|
|
flags.BoolVar(
|
|
&render,
|
|
"render",
|
|
false,
|
|
"render to xgo instead of preprocessing",
|
|
)
|
|
|
|
filePaths := flags.Parse()
|
|
|
|
x := NewXGo().SetPreCompile(func(
|
|
ctx context.Context, s *Script,
|
|
){
|
|
s.SetImports(stdlib.GetModuleMap(
|
|
stdlib.AllModuleNames()...,
|
|
))
|
|
s.EnableFileImport(true)
|
|
s.SetImportDir(modDir)
|
|
})
|
|
|
|
ctx := context.Background()
|
|
|
|
pp := New(x)
|
|
for _, filePath := range filePaths {
|
|
pth := filepath.FromSlash(filePath)
|
|
file, err := os.Open(pth)
|
|
if err != nil {
|
|
log.Printf("os.Open(%s): %s\n", pth, err)
|
|
continue
|
|
}
|
|
if render {
|
|
err := pp.Render(ctx, file, os.Stdout)
|
|
if err != nil {
|
|
log.Printf("pp.Render(...): %s\n", err)
|
|
}
|
|
continue
|
|
}
|
|
compiled, err := pp.Compile(context.Background(), file)
|
|
if err != nil {
|
|
log.Printf("pp.Compile(...): %s\n", err)
|
|
continue
|
|
}
|
|
err = pp.XGo().RunContext(
|
|
context.Background(),
|
|
compiled.Clone(),
|
|
pth, os.Stdout,
|
|
)
|
|
if err != nil {
|
|
log.Printf("pp.Xgo().RunContext(...): %s", err)
|
|
continue
|
|
}
|
|
}
|
|
}).Usage("[files]")
|