Skip to content

Commit

Permalink
Merge pull request #1455 from onflow/chasefleming/use-tmpl-generate
Browse files Browse the repository at this point in the history
Refactor to use template files to generate files
  • Loading branch information
chasefleming authored Mar 14, 2024
2 parents f611b10 + 1bf1df1 commit 0f21381
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 26 deletions.
72 changes: 46 additions & 26 deletions internal/super/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
package super

import (
"bytes"
"embed"
"fmt"
"path/filepath"
"strings"
"text/template"

flowsdk "github.com/onflow/flow-go-sdk"

Expand All @@ -36,6 +39,9 @@ import (
"github.com/spf13/cobra"
)

//go:embed templates/*.tmpl
var templatesFS embed.FS

type generateFlagsDef struct {
Directory string `default:"" flag:"dir" info:"Directory to generate files in"`
SkipTests bool `default:"false" flag:"skip-tests" info:"Skip generating test files"`
Expand Down Expand Up @@ -130,6 +136,28 @@ func stripCDCExtension(name string) string {
return strings.TrimSuffix(name, filepath.Ext(name))
}

// processTemplate reads a template file from the embedded filesystem and processes it with the provided data
// If you don't need to provide data, pass nil
func processTemplate(templatePath string, data map[string]interface{}) (string, error) {
templateData, err := templatesFS.ReadFile(templatePath)
if err != nil {
return "", fmt.Errorf("failed to read template file: %w", err)
}

tmpl, err := template.New("template").Parse(string(templateData))
if err != nil {
return "", fmt.Errorf("failed to parse template: %w", err)
}

var executedTemplate bytes.Buffer
// Execute the template with the provided data or nil if no data is needed
if err = tmpl.Execute(&executedTemplate, data); err != nil {
return "", fmt.Errorf("failed to execute template: %w", err)
}

return executedTemplate.String(), nil
}

func generateNew(
args []string,
templateType string,
Expand Down Expand Up @@ -164,34 +192,26 @@ func generateNew(

switch templateType {
case "contract":
fileToWrite = fmt.Sprintf(`access(all)
contract %s {
init() {}
}`, name)
testFileToWrite = fmt.Sprintf(`import Test
access(all) let account = Test.createAccount()
access(all) fun testContract() {
let err = Test.deployContract(
name: "%s",
path: "../contracts/%s.cdc",
arguments: [],
)
Test.expect(err, Test.beNil())
}`, name, name)
nameData := map[string]interface{}{"Name": name}
fileToWrite, err = processTemplate("templates/contract_init.cdc.tmpl", nameData)
if err != nil {
return nil, fmt.Errorf("error generating contract template: %w", err)
}

testFileToWrite, err = processTemplate("templates/contract_init_test.cdc.tmpl", nameData)
if err != nil {
return nil, fmt.Errorf("error generating contract test template: %w", err)
}
case "script":
fileToWrite = `access(all)
fun main() {
// Script details here
}`
fileToWrite, err = processTemplate("templates/script_init.cdc.tmpl", nil)
if err != nil {
return nil, fmt.Errorf("error generating script template: %w", err)
}
case "transaction":
fileToWrite = `transaction() {
prepare(account:AuthAccount) {}
execute {}
}`
fileToWrite, err = processTemplate("templates/transaction_init.cdc.tmpl", nil)
if err != nil {
return nil, fmt.Errorf("error generating transaction template: %w", err)
}
default:
return nil, fmt.Errorf("invalid template type: %s", templateType)
}
Expand Down
4 changes: 4 additions & 0 deletions internal/super/templates/contract_init.cdc.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
access(all)
contract {{ .Name }} {
init() {}
}
13 changes: 13 additions & 0 deletions internal/super/templates/contract_init_test.cdc.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Test

access(all) let account = Test.createAccount()

access(all) fun testContract() {
let err = Test.deployContract(
name: "{{ .Name }}",
path: "../contracts/{{ .Name }}.cdc",
arguments: [],
)

Test.expect(err, Test.beNil())
}
4 changes: 4 additions & 0 deletions internal/super/templates/script_init.cdc.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
access(all)
fun main() {
// Script details here
}
5 changes: 5 additions & 0 deletions internal/super/templates/transaction_init.cdc.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
transaction() {
prepare(account:AuthAccount) {}

execute {}
}

0 comments on commit 0f21381

Please sign in to comment.