Skip to content

Commit

Permalink
feat: support filesystem features
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanSpeakEasy committed Jan 13, 2023
1 parent 1507ac7 commit 57fa164
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 26 deletions.
63 changes: 46 additions & 17 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package easytemplate

import (
"fmt"
"io/fs"
"os"
"path"
"text/template"
Expand All @@ -10,17 +11,25 @@ import (
_ "github.com/robertkrimen/otto/underscore"
)

type WriteFunc func(string, []byte) error

type Opt func(*Engine)

func WithTemplateDir(templateDir string) Opt {
func WithBaseDir(baseDir string) Opt {
return func(e *Engine) {
e.baseDir = baseDir
}
}

func WithReadFileSystem(fs fs.FS) Opt {
return func(e *Engine) {
e.templateDir = templateDir
e.readFS = fs
}
}

func WithScriptDir(scriptDir string) Opt {
func WithWriteFunc(writeFunc WriteFunc) Opt {
return func(e *Engine) {
e.scriptDir = scriptDir
e.writeFunc = writeFunc
}
}

Expand Down Expand Up @@ -49,8 +58,9 @@ func WithJSFuncs(funcs map[string]func(call otto.FunctionCall) otto.Value) Opt {
}

type Engine struct {
templateDir string
scriptDir string
baseDir string
readFS fs.FS
writeFunc WriteFunc
ran bool
tmplFuncs template.FuncMap
jsFuncs map[string]func(call otto.FunctionCall) otto.Value
Expand Down Expand Up @@ -88,7 +98,12 @@ func (e *Engine) RunScript(scriptFile string, data any) error {
return err
}

s, err := vm.Compile(scriptFile, nil)
script, err := e.ReadFile(scriptFile)
if err != nil {
return fmt.Errorf("failed to read script file: %w", err)
}

s, err := vm.Compile("", script)
if err != nil {
return fmt.Errorf("failed to compile script: %w", err)
}
Expand All @@ -106,16 +121,16 @@ func (e *Engine) RunTemplate(templateFile string, outFile string, data any) erro
return err
}

templated, err := e.tmpl(vm, templateFile, data)
if err != nil {
return fmt.Errorf("failed to template file: %w", err)
}
return e.templateFile(vm, templateFile, outFile, data)
}

if err := os.WriteFile(outFile, []byte(templated), 0o644); err != nil {
return fmt.Errorf("failed to write file: %w", err)
func (e *Engine) RunTemplateString(templateString string, data any) (string, error) {
vm, err := e.init(data)
if err != nil {
return "", err
}

return nil
return e.tmpl(vm, templateString, data)
}

func (e *Engine) init(data any) (*otto.Otto, error) {
Expand Down Expand Up @@ -170,11 +185,12 @@ func (e *Engine) require(call otto.FunctionCall) otto.Value {

scriptPath := call.Argument(0).String()

if e.scriptDir != "" {
scriptPath = path.Join(e.scriptDir, scriptPath)
script, err := e.ReadFile(scriptPath)
if err != nil {
panic(vm.MakeCustomError("requireScript", err.Error()))
}

s, err := vm.Compile(scriptPath, nil)
s, err := vm.Compile("", script)
if err != nil {
panic(vm.MakeCustomError("requireScript", err.Error()))
}
Expand Down Expand Up @@ -212,3 +228,16 @@ func (e *Engine) registerTemplateFunc(call otto.FunctionCall) otto.Value {

return otto.Value{}
}

func (e *Engine) ReadFile(file string) ([]byte, error) {
filePath := file
if e.baseDir != "" {
filePath = path.Join(e.baseDir, filePath)
}

if e.readFS != nil {
return fs.ReadFile(e.readFS, filePath)
} else {
return os.ReadFile(filePath)
}
}
18 changes: 9 additions & 9 deletions templating.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"os"
"path"
"regexp"
"strings"
"text/template"
Expand Down Expand Up @@ -38,8 +37,14 @@ func (e *Engine) templateFile(vm *otto.Otto, templateFile, outFile string, input
return err
}

if err := os.WriteFile(outFile, []byte(output), 0o644); err != nil {
return err
if e.writeFunc != nil {
err = e.writeFunc(outFile, []byte(output))
} else {
err = os.WriteFile(outFile, []byte(output), 0o644)
}

if err != nil {
return fmt.Errorf("failed to write file %s: %w", outFile, err)
}

return nil
Expand Down Expand Up @@ -101,12 +106,7 @@ func (e *Engine) tmpl(vm *otto.Otto, templatePath string, inputData any) (out st
return "", fmt.Errorf("failed to set context: %w", err)
}

tp := templatePath
if e.templateDir != "" {
tp = path.Join(e.templateDir, templatePath)
}

data, err := os.ReadFile(tp)
data, err := e.ReadFile(templatePath)
if err != nil {
return "", fmt.Errorf("failed to read template file: %w", err)
}
Expand Down

0 comments on commit 57fa164

Please sign in to comment.