From 440a9a32bbdc5a28e24d94ce5ea46a796bb0a618 Mon Sep 17 00:00:00 2001 From: Tom Haile Date: Mon, 20 Nov 2023 13:09:27 -0700 Subject: [PATCH] convert template files to go files and use strings instead of files --- bindings/fcl-js.go | 44 +++++++------------ bindings/fcl-js_test.go | 19 ++++++-- .../{js_fcl_main.tmpl => js_fcl_main.tmpl.go} | 14 ++++-- ..._fcl_script.tmpl => js_fcl_script.tmpl.go} | 10 ++++- .../{js_fcl_tx.tmpl => js_fcl_tx.tmpl.go} | 10 ++++- 5 files changed, 62 insertions(+), 35 deletions(-) rename bindings/templates/{js_fcl_main.tmpl => js_fcl_main.tmpl.go} (85%) rename bindings/templates/{js_fcl_script.tmpl => js_fcl_script.tmpl.go} (73%) rename bindings/templates/{js_fcl_tx.tmpl => js_fcl_tx.tmpl.go} (74%) diff --git a/bindings/fcl-js.go b/bindings/fcl-js.go index 3833dc3..f2b1f94 100644 --- a/bindings/fcl-js.go +++ b/bindings/fcl-js.go @@ -2,13 +2,11 @@ package bindings import ( "bytes" - "os" - "path/filepath" - "runtime" "sort" "text/template" "github.com/onflow/flixkit-go" + bindings "github.com/onflow/flixkit-go/bindings/templates" "github.com/stoewer/go-strcase" ) @@ -31,25 +29,23 @@ type templateData struct { } type FclJSGenerator struct { - TemplateDir string + Templates []string } func NewFclJSGenerator() *FclJSGenerator { - _, currentFilePath, _, _ := runtime.Caller(0) - baseDir := filepath.Dir(currentFilePath) - templateDir := filepath.Join(baseDir, "templates") + templates := []string{ + bindings.GetJsFclMainTemplate(), + bindings.GetJsFclScriptTemplate(), + bindings.GetJsFclTxTemplate(), + } return &FclJSGenerator{ - TemplateDir: templateDir, + Templates: templates, } } func (g FclJSGenerator) Generate(flix *flixkit.FlowInteractionTemplate, templateLocation string, isLocal bool) (string, error) { - templateFiles, err := getAllFiles(g.TemplateDir) - if err != nil { - return "", err - } - tmpl, err := template.ParseFiles(templateFiles...) + tmpl, err := parseTemplates(g.Templates) if err != nil { return "", err } @@ -125,21 +121,15 @@ func convertCadenceTypeToJS(cadenceType string) string { } } -func getAllFiles(dir string) ([]string, error) { - var files []string - err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { +func parseTemplates(templates []string) (*template.Template, error) { + baseTemplate := template.New("base") + + for _, tmplStr := range templates { + _, err := baseTemplate.Parse(tmplStr) if err != nil { - return err + return nil, err } - // If it's a directory, skip it - if info.IsDir() { - return nil - } - files = append(files, path) - return nil - }) - if err != nil { - return nil, err } - return files, nil + + return baseTemplate, nil } diff --git a/bindings/fcl-js_test.go b/bindings/fcl-js_test.go index 9c8c89e..6b6d5d9 100644 --- a/bindings/fcl-js_test.go +++ b/bindings/fcl-js_test.go @@ -5,6 +5,7 @@ import ( "github.com/hexops/autogold/v2" "github.com/onflow/flixkit-go" + bindings "github.com/onflow/flixkit-go/bindings/templates" "github.com/stretchr/testify/assert" ) @@ -193,7 +194,11 @@ var minimumNoParamTemplate = &flixkit.FlowInteractionTemplate{ func TestJSGenTransaction(t *testing.T) { generator := FclJSGenerator{ - TemplateDir: "./templates", + Templates: []string{ + bindings.GetJsFclMainTemplate(), + bindings.GetJsFclScriptTemplate(), + bindings.GetJsFclTxTemplate(), + }, } got, _ := generator.Generate(parsedTemplateTX, "./transfer_token.json", true) autogold.ExpectFile(t, got) @@ -201,7 +206,11 @@ func TestJSGenTransaction(t *testing.T) { func TestJSGenScript(t *testing.T) { generator := FclJSGenerator{ - TemplateDir: "./templates", + Templates: []string{ + bindings.GetJsFclMainTemplate(), + bindings.GetJsFclScriptTemplate(), + bindings.GetJsFclTxTemplate(), + }, } assert := assert.New(t) got, err := generator.Generate(parsedTemplateScript, "./multiply_two_integers.template.json", true) @@ -211,7 +220,11 @@ func TestJSGenScript(t *testing.T) { func TestJSGenArrayScript(t *testing.T) { generator := FclJSGenerator{ - TemplateDir: "./templates", + Templates: []string{ + bindings.GetJsFclMainTemplate(), + bindings.GetJsFclScriptTemplate(), + bindings.GetJsFclTxTemplate(), + }, } assert := assert.New(t) diff --git a/bindings/templates/js_fcl_main.tmpl b/bindings/templates/js_fcl_main.tmpl.go similarity index 85% rename from bindings/templates/js_fcl_main.tmpl rename to bindings/templates/js_fcl_main.tmpl.go index d90c9aa..c862764 100644 --- a/bindings/templates/js_fcl_main.tmpl +++ b/bindings/templates/js_fcl_main.tmpl.go @@ -1,4 +1,7 @@ -/** +package bindings + +func GetJsFclMainTemplate() string { + const template = `/** This binding file was auto generated based on FLIX template v{{.Version}}. Changes to this file might get overwritten. Note fcl version 1.3.0 or higher is required to use templates. @@ -25,10 +28,15 @@ const flixTemplate = "{{.Location}}" {{- end -}} {{- "\n"}}*/ {{if .IsScript}} -{{- template "js_fcl_script.tmpl" .}} +{{- template "script" .}} {{else}} -{{- template "js_fcl_tx.tmpl" .}} +{{- template "tx" .}} {{- end}} + +` + + return template +} diff --git a/bindings/templates/js_fcl_script.tmpl b/bindings/templates/js_fcl_script.tmpl.go similarity index 73% rename from bindings/templates/js_fcl_script.tmpl rename to bindings/templates/js_fcl_script.tmpl.go index 04d90b4..47143e4 100644 --- a/bindings/templates/js_fcl_script.tmpl +++ b/bindings/templates/js_fcl_script.tmpl.go @@ -1,4 +1,7 @@ -export async function {{.Title}}( +package bindings + +func GetJsFclScriptTemplate() string { + const template = `{{define "script"}}export async function {{.Title}}( {{- if len .Parameters -}} { {{- range $index, $ele := .Parameters -}} @@ -19,4 +22,9 @@ export async function {{.Title}}( }); return info +}{{end}} +` + + return template + } diff --git a/bindings/templates/js_fcl_tx.tmpl b/bindings/templates/js_fcl_tx.tmpl.go similarity index 74% rename from bindings/templates/js_fcl_tx.tmpl rename to bindings/templates/js_fcl_tx.tmpl.go index bb31a70..bb180b7 100644 --- a/bindings/templates/js_fcl_tx.tmpl +++ b/bindings/templates/js_fcl_tx.tmpl.go @@ -1,4 +1,7 @@ -export async function {{.Title}}({ +package bindings + +func GetJsFclTxTemplate() string { + const template = `{{define "tx"}}export async function {{.Title}}({ {{- if len .Parameters -}} {{- range $index, $ele := .Parameters -}} {{if $index}}, {{end}}{{.Name}} @@ -17,4 +20,9 @@ export async function {{.Title}}({ }); return transactionId +}{{end}} +` + + return template + }