2024-08-02 17:00:46 +03:00
|
|
|
package gopp
|
2024-02-26 00:22:09 +03:00
|
|
|
|
|
|
|
import (
|
2024-04-25 22:38:04 +03:00
|
|
|
//"github.com/d5/tengo/v2"
|
2024-08-02 17:00:46 +03:00
|
|
|
"surdeus.su/core/xgo/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-08-02 17:00:46 +03:00
|
|
|
var Tool = mtool.T("gopp").Func(func(flags *mtool.Flags){
|
2024-06-09 15:35:28 +03:00
|
|
|
var (
|
|
|
|
modDir string
|
2024-08-02 17:00:46 +03:00
|
|
|
render bool
|
2024-06-09 15:35:28 +03:00
|
|
|
)
|
|
|
|
|
2024-08-02 17:00:46 +03:00
|
|
|
log.SetFlags(0)
|
2024-06-09 15:35:28 +03:00
|
|
|
flags.StringVar(
|
|
|
|
&modDir,
|
|
|
|
"mod",
|
|
|
|
".",
|
|
|
|
"set the import directory",
|
|
|
|
)
|
|
|
|
|
2024-08-02 17:00:46 +03:00
|
|
|
flags.BoolVar(
|
|
|
|
&render,
|
|
|
|
"render",
|
|
|
|
false,
|
|
|
|
"render to xgo instead of preprocessing",
|
|
|
|
)
|
|
|
|
|
2024-06-09 15:35:28 +03:00
|
|
|
filePaths := flags.Parse()
|
|
|
|
|
2024-08-02 17:00:46 +03:00
|
|
|
x := NewXGo().SetPreCompile(func(
|
|
|
|
ctx context.Context, s *Script,
|
|
|
|
){
|
|
|
|
s.SetImports(stdlib.GetModuleMap(
|
|
|
|
stdlib.AllModuleNames()...,
|
|
|
|
))
|
|
|
|
s.EnableFileImport(true)
|
|
|
|
s.SetImportDir(modDir)
|
|
|
|
})
|
2024-06-09 15:35:28 +03:00
|
|
|
|
2024-08-02 17:00:46 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
pp := New(x)
|
2024-02-26 00:22:09 +03:00
|
|
|
for _, filePath := range filePaths {
|
|
|
|
pth := filepath.FromSlash(filePath)
|
2024-08-02 17:00:46 +03:00
|
|
|
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)
|
2024-02-26 00:22:09 +03:00
|
|
|
if err != nil {
|
2024-08-02 17:00:46 +03:00
|
|
|
log.Printf("pp.Compile(...): %s\n", err)
|
2024-02-26 00:22:09 +03:00
|
|
|
continue
|
|
|
|
}
|
2024-08-02 17:00:46 +03:00
|
|
|
err = pp.XGo().RunContext(
|
2024-04-25 22:38:04 +03:00
|
|
|
context.Background(),
|
2024-08-02 17:00:46 +03:00
|
|
|
compiled.Clone(),
|
|
|
|
pth, os.Stdout,
|
2024-04-25 22:38:04 +03:00
|
|
|
)
|
2024-02-26 00:22:09 +03:00
|
|
|
if err != nil {
|
2024-08-02 17:00:46 +03:00
|
|
|
log.Printf("pp.Xgo().RunContext(...): %s", err)
|
2024-02-26 00:22:09 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2024-06-09 15:35:28 +03:00
|
|
|
}).Usage("[files]")
|