diff --git a/internal/super/generate.go b/internal/super/generate.go index e2a916562..ae077220e 100644 --- a/internal/super/generate.go +++ b/internal/super/generate.go @@ -19,9 +19,12 @@ package super import ( + "bytes" + "embed" "fmt" "path/filepath" "strings" + "text/template" flowsdk "github.com/onflow/flow-go-sdk" @@ -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"` @@ -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, @@ -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) } diff --git a/internal/super/templates/contract_init.cdc.tmpl b/internal/super/templates/contract_init.cdc.tmpl new file mode 100644 index 000000000..ff79993e6 --- /dev/null +++ b/internal/super/templates/contract_init.cdc.tmpl @@ -0,0 +1,4 @@ +access(all) +contract {{ .Name }} { + init() {} +} \ No newline at end of file diff --git a/internal/super/templates/contract_init_test.cdc.tmpl b/internal/super/templates/contract_init_test.cdc.tmpl new file mode 100644 index 000000000..dc8cc25fb --- /dev/null +++ b/internal/super/templates/contract_init_test.cdc.tmpl @@ -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()) +} \ No newline at end of file diff --git a/internal/super/templates/script_init.cdc.tmpl b/internal/super/templates/script_init.cdc.tmpl new file mode 100644 index 000000000..191f77c5d --- /dev/null +++ b/internal/super/templates/script_init.cdc.tmpl @@ -0,0 +1,4 @@ +access(all) +fun main() { + // Script details here +} \ No newline at end of file diff --git a/internal/super/templates/transaction_init.cdc.tmpl b/internal/super/templates/transaction_init.cdc.tmpl new file mode 100644 index 000000000..409f94edf --- /dev/null +++ b/internal/super/templates/transaction_init.cdc.tmpl @@ -0,0 +1,5 @@ +transaction() { + prepare(account:AuthAccount) {} + + execute {} +} \ No newline at end of file