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: remove colon syntax from module scaffolding --dep flag #3047

Merged
merged 7 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [#2998](https://github.com/ignite/cli/pull/2998) Hide `ignite generate dart` command and remove functionality.
- [#2991](https://github.com/ignite/cli/pull/2991) Hide `ignite scaffold flutter` command and remove functionality.
- [#2944](https://github.com/ignite/cli/pull/2944) Add a new event "update" status option to `pkg/cliui`.
- [#3030](https://github.com/ignite/cli/issues/3030) Remove colon syntax from module scaffolding `--dep` flag.
- [#3025](https://github.com/ignite/cli/issues/3025) Improve config version error handling.

### Breaking Changes
Expand Down
37 changes: 17 additions & 20 deletions ignite/cmd/scaffold_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"strings"
"regexp"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -104,7 +104,7 @@ params.
flagSetClearCache(c)

c.Flags().AddFlagSet(flagSetYes())
c.Flags().StringSlice(flagDep, []string{}, "module dependencies (e.g. --dep account,bank)")
c.Flags().StringSlice(flagDep, []string{}, "module dependencies (e.g. --dep account,bank,FeeGrant)")
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 @@ -161,24 +161,19 @@ func scaffoldModuleHandler(cmd *cobra.Command, args []string) error {
return err
}
if len(dependencies) > 0 {
var formattedDependencies []modulecreate.Dependency

// Parse the provided dependencies
for _, dependency := range dependencies {
var formattedDependency modulecreate.Dependency

splitted := strings.Split(dependency, ":")
switch len(splitted) {
case 1:
formattedDependency = modulecreate.NewDependency(splitted[0], "")
case 2:
formattedDependency = modulecreate.NewDependency(splitted[0], splitted[1])
default:
return fmt.Errorf("dependency %s is invalid, must have <depName> or <depName>.<depKeeperName>", dependency)
var deps []modulecreate.Dependency

isValid := regexp.MustCompile(`^[a-zA-Z]+$`).MatchString

for _, name := range dependencies {
if !isValid(name) {
return fmt.Errorf("invalid module dependency name format '%s'", name)
}
formattedDependencies = append(formattedDependencies, formattedDependency)

deps = append(deps, modulecreate.NewDependency(name))
}
options = append(options, scaffolder.WithDependencies(formattedDependencies))

options = append(options, scaffolder.WithDependencies(deps))
}

var msg bytes.Buffer
Expand Down Expand Up @@ -210,9 +205,11 @@ func scaffoldModuleHandler(cmd *cobra.Command, args []string) error {
// 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/ignite/cli/issues/818#issuecomment-865736052
for _, dep := range dependencies {
if dep == "gov" {
for _, name := range dependencies {
if name == "Gov" {
session.Print(govDependencyWarning)

break
}
}

Expand Down
2 changes: 1 addition & 1 deletion ignite/services/scaffolder/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func checkDependencies(dependencies []modulecreate.Dependency, appPath string) e
for _, dep := range dependencies {
// check the dependency has been registered
path := filepath.Join(appPath, module.PathAppModule)
if err := appanalysis.CheckKeeper(path, dep.KeeperName); err != nil {
if err := appanalysis.CheckKeeper(path, dep.KeeperName()); err != nil {
return fmt.Errorf(
"the module cannot have %s as a dependency: %w",
dep.Name,
Expand Down
3 changes: 3 additions & 0 deletions ignite/templates/field/plushhelpers/plushhelpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package plushhelpers

import (
"strings"

"github.com/gobuffalo/plush/v4"

"github.com/ignite/cli/ignite/pkg/xstrings"
Expand All @@ -14,6 +16,7 @@ func ExtendPlushContext(ctx *plush.Context) {
ctx.Set("mergeProtoImports", mergeProtoImports)
ctx.Set("mergeCustomImports", mergeCustomImports)
ctx.Set("title", xstrings.Title)
ctx.Set("toLower", strings.ToLower)
}

func mergeCustomImports(fields ...field.Fields) []string {
Expand Down
24 changes: 11 additions & 13 deletions ignite/templates/module/create/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package modulecreate
import (
"fmt"

"github.com/ignite/cli/ignite/pkg/xstrings"
"github.com/iancoleman/strcase"

"github.com/ignite/cli/ignite/templates/field"
)

Expand Down Expand Up @@ -38,20 +39,17 @@ func (opts *CreateOptions) Validate() error {
return nil
}

// NewDependency returns a new dependency.
func NewDependency(name string) Dependency {
return Dependency{Name: strcase.ToCamel(name)}
}

// Dependency represents a module dependency of a module
type Dependency struct {
Name string
KeeperName string // KeeperName represents the name of the keeper for the module in app.go
Name string
}

// NewDependency returns a new dependency object
func NewDependency(name, keeperName string) Dependency {
// Default keeper name
if keeperName == "" {
keeperName = fmt.Sprintf("%sKeeper", xstrings.Title(name))
}
return Dependency{
name,
keeperName,
}
// KeeperName returns the keeper's name for the dependency module.
func (d Dependency) KeeperName() string {
return fmt.Sprint(d.Name, "Keeper")
}
6 changes: 4 additions & 2 deletions ignite/templates/module/create/stargate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/gobuffalo/genny"
"github.com/gobuffalo/plush/v4"
"github.com/iancoleman/strcase"

"github.com/ignite/cli/ignite/pkg/gomodulepath"
"github.com/ignite/cli/ignite/pkg/placeholder"
Expand Down Expand Up @@ -58,6 +59,7 @@ func NewStargate(opts *CreateOptions) (*genny.Generator, error) {
ctx.Set("isIBC", opts.IsIBC)
ctx.Set("apiPath", fmt.Sprintf("/%s/%s", appModulePath, opts.ModuleName))
ctx.Set("protoPkgName", module.ProtoPackageName(appModulePath, opts.ModuleName))
ctx.Set("toVariableName", strcase.ToLowerCamel)

plushhelpers.ExtendPlushContext(ctx)
g.Transformer(xgenny.Transformer(ctx))
Expand Down Expand Up @@ -134,10 +136,10 @@ func appModifyStargate(replacer placeholder.Replacer, opts *CreateOptions) genny
// Module dependencies
var depArgs string
for _, dep := range opts.Dependencies {
depArgs = fmt.Sprintf("%sapp.%s,\n", depArgs, dep.KeeperName)
depArgs = fmt.Sprintf("%sapp.%s,\n", depArgs, dep.KeeperName())

// If bank is a dependency, add account permissions to the module
if dep.Name == "bank" {
if dep.Name == "Bank" {
template = `%[2]vmoduletypes.ModuleName: {authtypes.Minter, authtypes.Burner, authtypes.Staking},
%[1]v`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type (
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
<%= for (dependency) in dependencies { %>
<%= dependency.Name %>Keeper types.<%= title(dependency.Name) %>Keeper<% } %>
<%= toVariableName(dependency.KeeperName()) %> types.<%= dependency.KeeperName() %><% } %>
}
)

Expand All @@ -32,7 +32,8 @@ func NewKeeper(
<%= if (isIBC) { %>channelKeeper cosmosibckeeper.ChannelKeeper,
portKeeper cosmosibckeeper.PortKeeper,
scopedKeeper cosmosibckeeper.ScopedKeeper,<% } %>
<%= for (dependency) in dependencies { %><%= dependency.Name %>Keeper types.<%= title(dependency.Name) %>Keeper,<% } %>
<%= for (dependency) in dependencies { %>
<%= toVariableName(dependency.KeeperName()) %> types.<%= dependency.KeeperName() %>,<% } %>
) *Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
Expand All @@ -51,7 +52,8 @@ func NewKeeper(
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
<%= for (dependency) in dependencies { %><%= dependency.Name %>Keeper: <%= dependency.Name %>Keeper,<% } %>
<%= for (dependency) in dependencies { %>
<%= toVariableName(dependency.KeeperName()) %>: <%= toVariableName(dependency.KeeperName()) %>,<% } %>
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

<%= for (dependency) in dependencies { %>
<%= if (dependency.Name != "bank" && dependency.Name != "account") { %>
type <%= title(dependency.Name) %>Keeper interface {
// Methods imported from <%= dependency.Name %> should be defined here
<%= if (dependency.Name != "Bank" && dependency.Name != "Account") { %>
type <%= dependency.KeeperName() %> interface {
// Methods imported from <%= toLower(dependency.Name) %> should be defined here
}
<% } %>
<% } %>
Expand All @@ -23,4 +23,4 @@ type AccountKeeper interface {
type BankKeeper interface {
SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins
// Methods imported from bank should be defined here
}
}