Skip to content
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
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## [`v29.9.2`](https://github.com/ignite/cli/releases/tag/v29.9.2)

- [#4904](https://github.com/ignite/cli/pull/4904) Add variadic options in `modulecreate.AddModuleToAppConfig`.

## [`v29.9.1`](https://github.com/ignite/cli/releases/tag/v29.9.1)

### Changes
Expand Down
46 changes: 41 additions & 5 deletions ignite/templates/module/create/app_config_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,38 @@
"github.com/ignite/cli/v29/ignite/pkg/errors"
)

type addModuleAppConfigOptions struct {
skipConfig bool
runtimeFields []string
}

type AddModuleAppConfigOption func(*addModuleAppConfigOptions)

func SkipConfigEntry() AddModuleAppConfigOption {
return func(opts *addModuleAppConfigOptions) {
opts.skipConfig = true
}
}

// SpecifyModuleEntry allows to define to which field the module should be added in the app config.

Check failure on line 28 in ignite/templates/module/create/app_config_ast.go

View workflow job for this annotation

GitHub Actions / Lint Go code

Comment should end in a period (godot)
// E.g. "PreBlockers", "InitGenesis", "BeginBlockers", "EndBlockers"
func SpecifyModuleEntry(fields ...string) AddModuleAppConfigOption {
return func(opts *addModuleAppConfigOptions) {
opts.runtimeFields = fields
}
}

// AddModuleToAppConfig appends a given module to the chain app config.
// TODO: Eventually add variadic options to specify adding before or after a module.
func AddModuleToAppConfig(content, moduleName string) (string, error) {
func AddModuleToAppConfig(content, moduleName string, opts ...AddModuleAppConfigOption) (string, error) {
options := addModuleAppConfigOptions{}
for _, opt := range opts {
opt(&options)
}
return AddModuleToAppConfigWithOptions(content, moduleName, options)
}

// AddModuleToAppConfigWithOptions appends a given module to the chain app config with options.
func AddModuleToAppConfigWithOptions(content, moduleName string, opts addModuleAppConfigOptions) (string, error) {
fileSet := token.NewFileSet()
file, err := parser.ParseFile(fileSet, "", content, parser.ParseComments)
if err != nil {
Expand All @@ -37,14 +66,21 @@
return "", err
}

for _, fieldName := range []string{"InitGenesis", "BeginBlockers", "EndBlockers"} {
fields := opts.runtimeFields
if len(fields) == 0 {
fields = []string{"InitGenesis", "BeginBlockers", "EndBlockers"}
}

for _, fieldName := range fields {
if err := appendModuleNameToRuntimeField(file, runtimeModuleLit, fieldName, moduleName, fileSet); err != nil {
return "", err
}
}

if err := appendModuleConfigEntry(file, modulesField.Value, moduleName, fileSet); err != nil {
return "", err
if !opts.skipConfig {
if err := appendModuleConfigEntry(file, modulesField.Value, moduleName, fileSet); err != nil {
return "", err
}
}

file.Comments = commentMap.Filter(file).Comments()
Expand Down
10 changes: 10 additions & 0 deletions ignite/templates/module/create/app_config_ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,13 @@ func TestAddModuleToLegacyAppConfig(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 4, strings.Count(normalizedExpr(modified), "venusmoduletypes.ModuleName"))
}

func TestAddModuleToAppConfigWithSkipConfig(t *testing.T) {
content := readFixture(t, "../../app/files/app/app_config.go.plush")

modified, err := AddModuleToAppConfig(content, "blog", SkipConfigEntry())
require.NoError(t, err)
normalized := normalizedExpr(modified)
require.Equal(t, 3, strings.Count(normalized, "blogmoduletypes.ModuleName"))
require.NotContains(t, normalized, "Config:appconfig.WrapAny(&blogmoduletypes.Module{}),")
}
Loading