Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Sep 26, 2024
1 parent 79a0997 commit 6b4a410
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 53 deletions.
17 changes: 1 addition & 16 deletions internal/super/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,6 @@ func init() {
GenerateScriptCommand.AddToParent(GenerateCommand)
}

const (
DefaultCadenceDirectory = "cadence"
ContractType = "contract"
TransactionType = "transaction"
ScriptType = "script"
)

func generateContract(
args []string,
_ command.GlobalFlags,
Expand All @@ -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
}

Expand Down
32 changes: 28 additions & 4 deletions internal/super/generator/contract_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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),
}}
}

Expand All @@ -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,
},
},
}
}
27 changes: 24 additions & 3 deletions internal/super/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 7 additions & 12 deletions internal/super/generator/script_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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))
}
15 changes: 9 additions & 6 deletions internal/super/generator/test_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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))
}
15 changes: 7 additions & 8 deletions internal/super/generator/transaction_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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))
}
4 changes: 0 additions & 4 deletions internal/super/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6b4a410

Please sign in to comment.