Skip to content

Commit 8294078

Browse files
authored
refactor(templates): add variadic options in AddModuleToAppConfig (#4906)
* refactor(templates): add variadic options in `AddModuleToAppConfig` * allow config module entry
1 parent 8ae16d2 commit 8294078

3 files changed

Lines changed: 55 additions & 5 deletions

File tree

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
## [`v29.9.2`](https://github.com/ignite/cli/releases/tag/v29.9.2)
6+
7+
- [#4904](https://github.com/ignite/cli/pull/4904) Add variadic options in `modulecreate.AddModuleToAppConfig`.
8+
59
## [`v29.9.1`](https://github.com/ignite/cli/releases/tag/v29.9.1)
610

711
### Changes

ignite/templates/module/create/app_config_ast.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,38 @@ import (
1212
"github.com/ignite/cli/v29/ignite/pkg/errors"
1313
)
1414

15+
type addModuleAppConfigOptions struct {
16+
skipConfig bool
17+
runtimeFields []string
18+
}
19+
20+
type AddModuleAppConfigOption func(*addModuleAppConfigOptions)
21+
22+
func SkipConfigEntry() AddModuleAppConfigOption {
23+
return func(opts *addModuleAppConfigOptions) {
24+
opts.skipConfig = true
25+
}
26+
}
27+
28+
// SpecifyModuleEntry allows to define to which field the module should be added in the app config.
29+
// E.g. "PreBlockers", "InitGenesis", "BeginBlockers", "EndBlockers"
30+
func SpecifyModuleEntry(fields ...string) AddModuleAppConfigOption {
31+
return func(opts *addModuleAppConfigOptions) {
32+
opts.runtimeFields = fields
33+
}
34+
}
35+
1536
// AddModuleToAppConfig appends a given module to the chain app config.
16-
// TODO: Eventually add variadic options to specify adding before or after a module.
17-
func AddModuleToAppConfig(content, moduleName string) (string, error) {
37+
func AddModuleToAppConfig(content, moduleName string, opts ...AddModuleAppConfigOption) (string, error) {
38+
options := addModuleAppConfigOptions{}
39+
for _, opt := range opts {
40+
opt(&options)
41+
}
42+
return AddModuleToAppConfigWithOptions(content, moduleName, options)
43+
}
44+
45+
// AddModuleToAppConfigWithOptions appends a given module to the chain app config with options.
46+
func AddModuleToAppConfigWithOptions(content, moduleName string, opts addModuleAppConfigOptions) (string, error) {
1847
fileSet := token.NewFileSet()
1948
file, err := parser.ParseFile(fileSet, "", content, parser.ParseComments)
2049
if err != nil {
@@ -37,14 +66,21 @@ func AddModuleToAppConfig(content, moduleName string) (string, error) {
3766
return "", err
3867
}
3968

40-
for _, fieldName := range []string{"InitGenesis", "BeginBlockers", "EndBlockers"} {
69+
fields := opts.runtimeFields
70+
if len(fields) == 0 {
71+
fields = []string{"InitGenesis", "BeginBlockers", "EndBlockers"}
72+
}
73+
74+
for _, fieldName := range fields {
4175
if err := appendModuleNameToRuntimeField(file, runtimeModuleLit, fieldName, moduleName, fileSet); err != nil {
4276
return "", err
4377
}
4478
}
4579

46-
if err := appendModuleConfigEntry(file, modulesField.Value, moduleName, fileSet); err != nil {
47-
return "", err
80+
if !opts.skipConfig {
81+
if err := appendModuleConfigEntry(file, modulesField.Value, moduleName, fileSet); err != nil {
82+
return "", err
83+
}
4884
}
4985

5086
file.Comments = commentMap.Filter(file).Comments()

ignite/templates/module/create/app_config_ast_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,13 @@ func TestAddModuleToLegacyAppConfig(t *testing.T) {
2828
require.NoError(t, err)
2929
require.Equal(t, 4, strings.Count(normalizedExpr(modified), "venusmoduletypes.ModuleName"))
3030
}
31+
32+
func TestAddModuleToAppConfigWithSkipConfig(t *testing.T) {
33+
content := readFixture(t, "../../app/files/app/app_config.go.plush")
34+
35+
modified, err := AddModuleToAppConfig(content, "blog", SkipConfigEntry())
36+
require.NoError(t, err)
37+
normalized := normalizedExpr(modified)
38+
require.Equal(t, 3, strings.Count(normalized, "blogmoduletypes.ModuleName"))
39+
require.NotContains(t, normalized, "Config:appconfig.WrapAny(&blogmoduletypes.Module{}),")
40+
}

0 commit comments

Comments
 (0)