feat(vm/compiler): embed templates to executable

This commit is contained in:
5684185+vsariola@users.noreply.github.com 2023-07-08 16:39:41 +03:00
parent d2ddba3944
commit 8ffe4a70dd
21 changed files with 9 additions and 6 deletions

View File

@ -2,10 +2,9 @@ package compiler
import ( import (
"bytes" "bytes"
"embed"
"fmt" "fmt"
"path"
"path/filepath" "path/filepath"
"runtime"
"text/template" "text/template"
"github.com/Masterminds/sprig" "github.com/Masterminds/sprig"
@ -21,9 +20,11 @@ type Compiler struct {
RowSync bool RowSync bool
} }
//go:embed templates/amd64-386/* templates/wasm/*
var templateFS embed.FS
// New returns a new compiler using the default .asm templates // New returns a new compiler using the default .asm templates
func New(os string, arch string, output16Bit bool, rowsync bool) (*Compiler, error) { func New(os string, arch string, output16Bit bool, rowsync bool) (*Compiler, error) {
_, myname, _, _ := runtime.Caller(0)
var subdir string var subdir string
if arch == "386" || arch == "amd64" { if arch == "386" || arch == "amd64" {
subdir = "amd64-386" subdir = "amd64-386"
@ -32,9 +33,11 @@ func New(os string, arch string, output16Bit bool, rowsync bool) (*Compiler, err
} else { } else {
return nil, fmt.Errorf("compiler.New failed, because only amd64, 386 and wasm archs are supported (targeted architecture was %v)", arch) return nil, fmt.Errorf("compiler.New failed, because only amd64, 386 and wasm archs are supported (targeted architecture was %v)", arch)
} }
templateDir := filepath.Join(path.Dir(myname), "..", "..", "templates", subdir) tmpl, err := template.New("base").Funcs(sprig.TxtFuncMap()).ParseFS(templateFS, "templates/"+subdir+"/*.*")
compiler, err := NewFromTemplates(os, arch, output16Bit, rowsync, templateDir) if err != nil {
return compiler, err return nil, fmt.Errorf(`could not create templates: %v`, err)
}
return &Compiler{Template: tmpl, OS: os, Arch: arch, RowSync: rowsync, Output16Bit: output16Bit}, nil
} }
func NewFromTemplates(os string, arch string, output16Bit bool, rowsync bool, templateDirectory string) (*Compiler, error) { func NewFromTemplates(os string, arch string, output16Bit bool, rowsync bool, templateDirectory string) (*Compiler, error) {