Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dependencies option for module create #1287

Merged
merged 41 commits into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
1d55c02
expected keeper template
lumtis Jun 21, 2021
34aaa49
add dependenciy options
lumtis Jun 21, 2021
565831c
Template init
lumtis Jun 22, 2021
8b9eafe
Add misc errors to tracer
lumtis Jun 22, 2021
6148c53
app.go modification
lumtis Jun 22, 2021
398aa98
Merge branch 'develop' into feat/module-deps
lumtis Jun 22, 2021
c73e72e
app.go update gov
lumtis Jun 22, 2021
aed8bc3
Add tests
lumtis Jun 22, 2021
7099905
Fix generated tests
lumtis Jun 22, 2021
c4ba699
gov module warning
lumtis Jun 22, 2021
f909d96
Check dependencies
lumtis Jun 22, 2021
dc64f5d
Fix test
lumtis Jun 22, 2021
7f40b9b
bank permissions
lumtis Jun 22, 2021
bff0de3
Merge branch 'develop' into feat/module-deps
lumtis Jun 22, 2021
8661015
Fix ibc test
lumtis Jun 22, 2021
5c4c201
Merge branch 'develop' into feat/module-deps
lumtis Jun 22, 2021
0675a45
Merge branch 'develop' into feat/module-deps
lumtis Jun 23, 2021
a00c3d4
Merge branch 'develop' into feat/module-deps
lumtis Jun 24, 2021
6aef2c3
Update starport/cmd/module-create.go
lumtis Jun 25, 2021
70af24c
Use string slice
lumtis Jun 25, 2021
1492ad8
Conflict
lumtis Jun 25, 2021
be07620
lint
lumtis Jun 25, 2021
cfc3349
Merge branch 'develop' into feat/module-deps
lumtis Jun 28, 2021
cc449ae
Parse app module
lumtis Jun 29, 2021
5aa900d
Update dependency type
lumtis Jun 29, 2021
5c557fe
Fix validation error bug
lumtis Jun 29, 2021
49ee9c0
Add tests for app analysis
lumtis Jun 29, 2021
76399e4
Lint
lumtis Jun 29, 2021
f9693c4
Merge branch 'develop' into feat/module-deps
lumtis Jun 29, 2021
ce86cb6
Merge branch 'develop' into feat/module-deps
lumtis Jul 1, 2021
f345741
Refactor cosmosanalysis
lumtis Jul 1, 2021
6a92b6c
Lint
lumtis Jul 1, 2021
9efd501
Interface
lumtis Jul 1, 2021
cd1cb63
Parse module instead of files
lumtis Jul 1, 2021
f2a8633
Ilker comment
lumtis Jul 1, 2021
08812cb
No non-registered
lumtis Jul 1, 2021
a177ef5
New test
lumtis Jul 1, 2021
38e0329
Conflicts
lumtis Jul 5, 2021
5e50910
Fix tests
lumtis Jul 5, 2021
67e1870
Module create fix
lumtis Jul 5, 2021
f4b5a1f
Merge branch 'develop' into feat/module-deps
lumtis Jul 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions integration/cmd_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,36 @@ func TestGenerateAStargateAppWithEmptyModule(t *testing.T) {
ExecShouldError(),
))

env.Must(env.Exec("create a module with dependencies",
step.NewSteps(step.New(
step.Exec(
"starport",
"module",
"create",
"example_with_dep",
"--dep",
"account,bank,staking,slashing,example",
"--require-registration",
),
step.Workdir(path),
)),
))

env.Must(env.Exec("should prevent creating a module with invalid dependencies",
step.NewSteps(step.New(
step.Exec(
"starport",
"module",
"create",
"example_with_wrong_dep",
"--dep",
"dup,dup",
"--require-registration",
),
step.Workdir(path),
)),
ExecShouldError(),
))

env.EnsureAppIsSteady(path)
}
51 changes: 47 additions & 4 deletions integration/cmd_ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,59 @@ func TestCreateModuleWithIBC(t *testing.T) {

env.Must(env.Exec("create an IBC module with an ordered channel",
step.NewSteps(step.New(
step.Exec("starport", "module", "create", "--ibc", "orderedfoo", "--ordering", "ordered", "--require-registration"),
step.Exec(
"starport",
"module",
"create",
"--ibc",
"orderedfoo",
"--ordering",
"ordered",
"--require-registration",
),
step.Workdir(path),
)),
))

env.Must(env.Exec("create an IBC module with an unordered channel",
step.NewSteps(step.New(
step.Exec("starport", "module", "create", "--ibc", "unorderedfoo", "--ordering", "unordered", "--require-registration"),
step.Exec(
"starport",
"module",
"create",
"--ibc",
"unorderedfoo",
"--ordering",
"unordered",
"--require-registration",
),
step.Workdir(path),
)),
))

env.Must(env.Exec("create an non IBC module",
env.Must(env.Exec("create a non IBC module",
step.NewSteps(step.New(
step.Exec("starport", "module", "create", "foobar", "--require-registration"),
step.Workdir(path),
)),
))

env.Must(env.Exec("create an IBC module with dependencies",
step.NewSteps(step.New(
step.Exec(
"starport",
"module",
"create",
"example_with_dep",
"--ibc",
"--dep",
"account,bank,staking,slashing",
"--require-registration",
),
step.Workdir(path),
)),
))

env.EnsureAppIsSteady(path)
}

Expand All @@ -69,7 +103,16 @@ func TestCreateIBCPacket(t *testing.T) {

env.Must(env.Exec("create a packet",
step.NewSteps(step.New(
step.Exec("starport", "packet", "bar", "text", "--module", "foo", "--ack", "foo:string,bar:int,foobar:bool"),
step.Exec(
"starport",
"packet",
"bar",
"text",
"--module",
"foo",
"--ack",
"foo:string,bar:int,foobar:bool",
),
step.Workdir(path),
)),
))
Expand Down
24 changes: 22 additions & 2 deletions integration/cmd_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ func TestGenerateAnAppWithMessage(t *testing.T) {

env.Must(env.Exec("create a message",
step.NewSteps(step.New(
step.Exec("starport", "message", "do-foo", "text", "vote:int", "like:bool", "-r", "foo,bar:int,foobar:bool"),
step.Exec(
"starport",
"message",
"do-foo",
"text",
"vote:int",
"like:bool",
"-r",
"foo,bar:int,foobar:bool",
),
step.Workdir(path),
)),
))
Expand Down Expand Up @@ -45,7 +54,18 @@ func TestGenerateAnAppWithMessage(t *testing.T) {

env.Must(env.Exec("create a message in a module",
step.NewSteps(step.New(
step.Exec("starport", "message", "do-foo", "text", "--module", "foo", "--desc", "foo bar foobar", "--response", "foo,bar:int,foobar:bool"),
step.Exec(
"starport",
"message",
"do-foo",
"text",
"--module",
"foo",
"--desc",
"foo bar foobar",
"--response",
"foo,bar:int,foobar:bool",
),
step.Workdir(path),
)),
))
Expand Down
36 changes: 33 additions & 3 deletions integration/cmd_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,33 @@ func TestGenerateAnAppWithQuery(t *testing.T) {

env.Must(env.Exec("create a query",
step.NewSteps(step.New(
step.Exec("starport", "query", "foo", "text", "vote:int", "like:bool", "-r", "foo,bar:int,foobar:bool"),
step.Exec(
"starport",
"query",
"foo",
"text",
"vote:int",
"like:bool",
"-r",
"foo,bar:int,foobar:bool",
),
step.Workdir(path),
)),
))

env.Must(env.Exec("create a paginated query",
step.NewSteps(step.New(
step.Exec("starport", "query", "bar", "text", "vote:int", "like:bool", "-r", "foo,bar:int,foobar:bool", "--paginated"),
step.Exec(
"starport",
"query",
"bar",
"text",
"vote:int",
"like:bool",
"-r",
"foo,bar:int,foobar:bool",
"--paginated",
),
step.Workdir(path),
)),
))
Expand Down Expand Up @@ -50,7 +69,18 @@ func TestGenerateAnAppWithQuery(t *testing.T) {

env.Must(env.Exec("create a query in a module",
step.NewSteps(step.New(
step.Exec("starport", "query", "foo", "text", "--module", "foo", "--desc", "foo bar foobar", "--response", "foo,bar:int,foobar:bool"),
step.Exec(
"starport",
"query",
"foo",
"text",
"--module",
"foo",
"--desc",
"foo bar foobar",
"--response",
"foo,bar:int,foobar:bool",
),
step.Workdir(path),
)),
))
Expand Down
57 changes: 57 additions & 0 deletions starport/cmd/module-create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import (
"errors"
"fmt"
"io"
"strings"

"github.com/spf13/cobra"
"github.com/tendermint/starport/starport/pkg/clispinner"
"github.com/tendermint/starport/starport/pkg/placeholder"
"github.com/tendermint/starport/starport/pkg/validation"
"github.com/tendermint/starport/starport/services/scaffolder"
"github.com/tendermint/starport/starport/templates/module"
modulecreate "github.com/tendermint/starport/starport/templates/module/create"
)

const (
flagDep = "dep"
flagIBC = "ibc"
flagIBCOrdering = "ordering"
flagRequireRegistration = "require-registration"
Expand Down Expand Up @@ -53,6 +56,7 @@ func NewModuleCreate() *cobra.Command {
Args: cobra.MinimumNArgs(1),
RunE: createModuleHandler,
}
c.Flags().StringSlice(flagDep, []string{}, "module dependencies (e.g. --dep account,bank)")
c.Flags().Bool(flagIBC, false, "scaffold an IBC module")
c.Flags().String(flagIBCOrdering, "none", "channel ordering of the IBC module [none|ordered|unordered]")
c.Flags().Bool(flagRequireRegistration, false, "if true command will fail if module can't be registered")
Expand Down Expand Up @@ -86,6 +90,35 @@ func createModuleHandler(cmd *cobra.Command, args []string) error {
options = append(options, scaffolder.WithIBCChannelOrdering(ibcOrdering), scaffolder.WithIBC())
}

// Get module dependencies
dependencies, err := cmd.Flags().GetStringSlice(flagDep)
if err != nil {
return err
}
if len(dependencies) > 0 {
var formattedDependencies []modulecreate.Dependency

// Parse the provided dependencies
for _, dependency := range dependencies {
splitted := strings.Split(dependency, ".")
lumtis marked this conversation as resolved.
Show resolved Hide resolved
switch {
case len(splitted) == 1:
formattedDependencies = append(
lumtis marked this conversation as resolved.
Show resolved Hide resolved
formattedDependencies,
modulecreate.NewDependency(splitted[0], ""),
)
case len(splitted) == 2:
formattedDependencies = append(
formattedDependencies,
modulecreate.NewDependency(splitted[0], splitted[1]),
)
default:
return fmt.Errorf("dependency %s is invalid, must have <depName> or <depName>.<depKeeperName>", dependency)
}
}
options = append(options, scaffolder.WithDependencies(formattedDependencies))
}

sc, err := scaffolder.New(appPath)
if err != nil {
return err
Expand All @@ -111,6 +144,30 @@ func createModuleHandler(cmd *cobra.Command, args []string) error {
fmt.Println(sourceModificationToString(sm))
}

if len(dependencies) > 0 {
dependencyWarning(dependencies)
Pantani marked this conversation as resolved.
Show resolved Hide resolved
}

io.Copy(cmd.OutOrStdout(), &msg)
return nil
}

// in previously scaffolded apps gov keeper is defined below the scaffolded module keeper definition
// therefore we must warn the user to manually move the definition if it's the case
// https://github.com/tendermint/starport/issues/818#issuecomment-865736052
const govWarning = `⚠️ If your app has been scaffolded with Starport 0.16.x or below
Please make sure that your module keeper definition is defined after gov module keeper definition in app/app.go:

app.GovKeeper = ...
...
[your module keeper definition]
`

// dependencyWarning is used to print a warning if gov is provided as a dependency
func dependencyWarning(dependencies []string) {
for _, dep := range dependencies {
if dep == "gov" {
fmt.Print(govWarning)
}
}
}
52 changes: 52 additions & 0 deletions starport/pkg/cosmosanalysis/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package app

import (
"fmt"
"go/ast"
"go/parser"
"go/token"
)

const (
appTypeName = "App"
lumtis marked this conversation as resolved.
Show resolved Hide resolved
)

// CheckKeeper checks for the existence of the keeper with the provided name in the app structure
func CheckKeeper(appPath, keeperName string) error {
fileSet := token.NewFileSet()
f, err := parser.ParseFile(fileSet, appPath, nil, 0)
if err != nil {
return err
}

// Inspect the file for app struct
var found bool
ast.Inspect(f, func(n ast.Node) bool {
// look for struct methods.
appType, ok := n.(*ast.TypeSpec)
if !ok || appType.Name.Name != appTypeName {
return true
}

appStruct, ok := appType.Type.(*ast.StructType)
if !ok {
return true
}

// Search for the keeper specific field
for _, field := range appStruct.Fields.List {
for _, fieldName := range field.Names {
if fieldName.Name == keeperName {
found = true
lumtis marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

return false
})

if !found {
return fmt.Errorf("app doesn't contain %s", keeperName)
}
return nil
}
Loading