Skip to content

Commit

Permalink
Merge pull request #22 from onflow/fix-intern-temp-files
Browse files Browse the repository at this point in the history
convert template files to go files and use strings instead of files
  • Loading branch information
bthaile authored Nov 20, 2023
2 parents 9bc8076 + 440a9a3 commit 0e81dc8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 35 deletions.
44 changes: 17 additions & 27 deletions bindings/fcl-js.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
19 changes: 16 additions & 3 deletions bindings/fcl-js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -193,15 +194,23 @@ 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)
}

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)
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
}
Original file line number Diff line number Diff line change
@@ -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 -}}
Expand All @@ -19,4 +22,9 @@ export async function {{.Title}}(
});
return info
}{{end}}
`

return template

}
Original file line number Diff line number Diff line change
@@ -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}}
Expand All @@ -17,4 +20,9 @@ export async function {{.Title}}({
});
return transactionId
}{{end}}
`

return template

}

0 comments on commit 0e81dc8

Please sign in to comment.