diff --git a/internal/super/generate.go b/internal/super/generate.go index 24cd0e2d3..dd91e52a4 100644 --- a/internal/super/generate.go +++ b/internal/super/generate.go @@ -84,13 +84,6 @@ func init() { GenerateScriptCommand.AddToParent(GenerateCommand) } -const ( - DefaultCadenceDirectory = "cadence" - ContractType = "contract" - TransactionType = "transaction" - ScriptType = "script" -) - func generateContract( args []string, _ command.GlobalFlags, @@ -100,15 +93,7 @@ func generateContract( ) (result command.Result, err error) { g := generator.NewGenerator("", state, logger, false, true) name := util.StripCDCExtension(args[0]) - - templates := []generator.TemplateItem{ - generator.ContractTemplate{Name: name, SkipTests: generateFlags.SkipTests, SaveState: true}, - } - if !generateFlags.SkipTests { - templates = append(templates, generator.TestTemplate{Name: name + "_test", Data: map[string]interface{}{"ContractName": "Counter"}}) - } - - err = g.Create() + err = g.Create(generator.ContractTemplate{Name: name, SkipTests: generateFlags.SkipTests, SaveState: true}) return nil, err } diff --git a/internal/super/generator/contract_template.go b/internal/super/generator/contract_template.go index 783e9952a..383cf44ff 100644 --- a/internal/super/generator/contract_template.go +++ b/internal/super/generator/contract_template.go @@ -10,6 +10,11 @@ import ( "github.com/onflow/flowkit/v2/config" ) +const ( + DefaultContractDirectory = "cadence" + DefaultTestAddress = "0x0000000000000007" +) + // Contract contains properties for contracts type ContractTemplate struct { Name string @@ -21,8 +26,13 @@ type ContractTemplate struct { } var _ TemplateItem = ContractTemplate{} +var _ TemplateItemWithStateUpdate = ContractTemplate{} +var _ TemplateItemWithChildren = ContractTemplate{} + +func (c ContractTemplate) GetType() string { + return "contract" +} -// GetTemplate returns the template of the contract func (c ContractTemplate) GetTemplatePath() string { if c.TemplatePath == "" { return "contract_init.cdc.tmpl" @@ -31,7 +41,6 @@ func (c ContractTemplate) GetTemplatePath() string { return c.TemplatePath } -// GetData returns the data of the contract func (c ContractTemplate) GetData() map[string]interface{} { data := map[string]interface{}{ "Name": c.Name, @@ -44,7 +53,7 @@ func (c ContractTemplate) GetData() map[string]interface{} { } func (c ContractTemplate) GetTargetPath() string { - return filepath.Join(DefaultCadenceDirectory, "contracts", c.Account, util.AddCDCExtension(c.Name)) + return filepath.Join(DefaultCadenceDirectory, DefaultContractDirectory, c.Account, util.AddCDCExtension(c.Name)) } func (c ContractTemplate) UpdateState(state *flowkit.State) error { @@ -53,7 +62,7 @@ func (c ContractTemplate) UpdateState(state *flowkit.State) error { if c.SkipTests != true { aliases = config.Aliases{{ Network: config.TestingNetwork.Name, - Address: flowsdk.HexToAddress("0x0000000000000007"), + Address: flowsdk.HexToAddress(DefaultTestAddress), }} } @@ -74,3 +83,18 @@ func (c ContractTemplate) UpdateState(state *flowkit.State) error { return nil } + +func (c ContractTemplate) GetChildren() []TemplateItem { + if c.SkipTests { + return []TemplateItem{} + } + + return []TemplateItem{ + TestTemplate{ + Name: fmt.Sprintf("%s_test", c.Name), + Data: map[string]interface{}{ + "ContractName": c.Name, + }, + }, + } +} diff --git a/internal/super/generator/generator.go b/internal/super/generator/generator.go index d74bc212f..eb04270ea 100644 --- a/internal/super/generator/generator.go +++ b/internal/super/generator/generator.go @@ -39,12 +39,24 @@ var templatesFS embed.FS // TemplateItem is an interface for different template types type TemplateItem interface { + GetType() string GetTemplatePath() string GetData() map[string]interface{} GetTargetPath() string +} + +// TemplateItemWithStateUpdate is an interface for template items that need to update the Flowkit state/flow.json +type TemplateItemWithStateUpdate interface { + TemplateItem UpdateState(state *flowkit.State) error } +// TemplateItemWithChildren is an interface for template items that have children +type TemplateItemWithChildren interface { + TemplateItem + GetChildren() []TemplateItem +} + type Generator struct { directory string state *flowkit.State @@ -75,6 +87,13 @@ func (g *Generator) Create(items ...TemplateItem) error { if err != nil { return err } + + if itemWithChildren, ok := item.(TemplateItemWithChildren); ok { + err = g.Create(itemWithChildren.GetChildren()...) + if err != nil { + return err + } + } } return nil @@ -123,9 +142,11 @@ func (g *Generator) generate(item TemplateItem) error { } // Call template state update function if it exists - err = item.UpdateState(g.state) - if err != nil { - return err + if itemWithStateUpdate, ok := item.(TemplateItemWithStateUpdate); ok { + err = itemWithStateUpdate.UpdateState(g.state) + if err != nil { + return err + } } return nil diff --git a/internal/super/generator/script_template.go b/internal/super/generator/script_template.go index 7f13b55aa..1f7c25960 100644 --- a/internal/super/generator/script_template.go +++ b/internal/super/generator/script_template.go @@ -4,10 +4,12 @@ import ( "path/filepath" "github.com/onflow/flow-cli/internal/util" - "github.com/onflow/flowkit/v2" ) -// ScriptTemplate contains only a name property for scripts and transactions +const ( + DefaultScriptDirectory = "scripts" +) + type ScriptTemplate struct { Name string TemplatePath string @@ -16,12 +18,10 @@ type ScriptTemplate struct { var _ TemplateItem = ScriptTemplate{} -// GetName returns the name of the script or transaction -func (o ScriptTemplate) GetName() string { - return o.Name +func (o ScriptTemplate) GetType() string { + return "script" } -// GetTemplate returns an empty string for scripts and transactions func (o ScriptTemplate) GetTemplatePath() string { if o.TemplatePath == "" { return "script_init.cdc.tmpl" @@ -30,15 +30,10 @@ func (o ScriptTemplate) GetTemplatePath() string { return o.TemplatePath } -// GetData returns the data of the script or transaction func (o ScriptTemplate) GetData() map[string]interface{} { return o.Data } func (o ScriptTemplate) GetTargetPath() string { - return filepath.Join(DefaultCadenceDirectory, "scripts", util.AddCDCExtension(o.Name)) -} - -func (o ScriptTemplate) UpdateState(state *flowkit.State) error { - return nil + return filepath.Join(DefaultCadenceDirectory, DefaultScriptDirectory, util.AddCDCExtension(o.Name)) } diff --git a/internal/super/generator/test_template.go b/internal/super/generator/test_template.go index a3e32a5c3..b4e5103f3 100644 --- a/internal/super/generator/test_template.go +++ b/internal/super/generator/test_template.go @@ -4,7 +4,10 @@ import ( "path/filepath" "github.com/onflow/flow-cli/internal/util" - "github.com/onflow/flowkit/v2" +) + +const ( + DefaultTestDirectory = "tests" ) type TestTemplate struct { @@ -15,6 +18,10 @@ type TestTemplate struct { var _ TemplateItem = TestTemplate{} +func (o TestTemplate) GetType() string { + return "test" +} + // GetName returns the name of the script or transaction func (o TestTemplate) GetName() string { return o.Name @@ -35,9 +42,5 @@ func (o TestTemplate) GetData() map[string]interface{} { } func (o TestTemplate) GetTargetPath() string { - return filepath.Join(DefaultCadenceDirectory, "tests", util.AddCDCExtension(o.Name)) -} - -func (o TestTemplate) UpdateState(state *flowkit.State) error { - return nil + return filepath.Join(DefaultCadenceDirectory, DefaultTestDirectory, util.AddCDCExtension(o.Name)) } diff --git a/internal/super/generator/transaction_template.go b/internal/super/generator/transaction_template.go index d90ba3cce..9cb28d9ef 100644 --- a/internal/super/generator/transaction_template.go +++ b/internal/super/generator/transaction_template.go @@ -4,7 +4,10 @@ import ( "path/filepath" "github.com/onflow/flow-cli/internal/util" - "github.com/onflow/flowkit/v2" +) + +const ( + DefaultTransactionDirectory = "transactions" ) // TransactionTemplate contains only a name property for scripts and transactions @@ -17,8 +20,8 @@ type TransactionTemplate struct { var _ TemplateItem = TransactionTemplate{} // GetName returns the name of the script or transaction -func (o TransactionTemplate) GetName() string { - return o.Name +func (o TransactionTemplate) GetType() string { + return "transaction" } // GetTemplate returns an empty string for scripts and transactions @@ -36,9 +39,5 @@ func (o TransactionTemplate) GetData() map[string]interface{} { } func (o TransactionTemplate) GetTargetPath() string { - return filepath.Join(DefaultCadenceDirectory, "transactions", util.AddCDCExtension(o.Name)) -} - -func (o TransactionTemplate) UpdateState(state *flowkit.State) error { - return nil + return filepath.Join(DefaultCadenceDirectory, DefaultTransactionDirectory, util.AddCDCExtension(o.Name)) } diff --git a/internal/super/setup.go b/internal/super/setup.go index 18d850340..ea0d5b856 100644 --- a/internal/super/setup.go +++ b/internal/super/setup.go @@ -229,10 +229,6 @@ func startInteractiveSetup( TemplatePath: "transaction_counter.cdc.tmpl", Data: map[string]interface{}{"ContractName": "Counter"}, }, - generator.TestTemplate{ - Name: "Counter_test", - Data: map[string]interface{}{"ContractName": "Counter"}, - }, } g := generator.NewGenerator(tempDir, state, logger, true, false)