diff --git a/evolve/CHANGELOG.md b/evolve/CHANGELOG.md index 927b02f3..0653ae88 100644 --- a/evolve/CHANGELOG.md +++ b/evolve/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- [#236](https://github.com/ignite/apps/pull/236) Add `--start` flag to `evolve add` command to optionally disable addition of the start command. + ## [`v0.4.3`](https://github.com/ignite/apps/releases/tag/evolve/v0.4.3) - [#233](https://github.com/ignite/apps/pull/233) Bump dependencies. diff --git a/evolve/cmd/add.go b/evolve/cmd/add.go index 7317f3c9..832c0859 100644 --- a/evolve/cmd/add.go +++ b/evolve/cmd/add.go @@ -18,6 +18,7 @@ const ( statusScaffolding = "Scaffolding..." flagPath = "path" + flagStart = "start" flagMigrate = "migrate" ) @@ -32,6 +33,11 @@ func AddHandler(ctx context.Context, cmd *plugin.ExecutedCommand) error { return err } + withStartCmd, err := flags.GetBool(flagStart) + if err != nil { + return err + } + migrateCometBFT, err := flags.GetBool(flagMigrate) if err != nil { return err @@ -52,7 +58,7 @@ func AddHandler(ctx context.Context, cmd *plugin.ExecutedCommand) error { return err } - g, err := template.NewEvolveGenerator(c, migrateCometBFT) + g, err := template.NewEvolveGenerator(c, migrateCometBFT, withStartCmd) if err != nil { return err } diff --git a/evolve/cmd/cmd.go b/evolve/cmd/cmd.go index 82c9a6d7..9449aba6 100644 --- a/evolve/cmd/cmd.go +++ b/evolve/cmd/cmd.go @@ -23,6 +23,12 @@ func GetCommands() []*plugin.Command { Shorthand: "p", Type: plugin.FlagTypeString, }, + { + Name: flagStart, + Usage: "modify start command to use ev-abci (set to false to only add migrate command)", + Type: plugin.FlagTypeBool, + DefaultValue: "true", + }, { Name: flagMigrate, Usage: "scaffolds the migrations helpers and modules (to use when migrating from CometBFT)", diff --git a/evolve/template/generator.go b/evolve/template/generator.go index 8094d129..6e449037 100644 --- a/evolve/template/generator.go +++ b/evolve/template/generator.go @@ -11,7 +11,7 @@ import ( ) // NewEvolveGenerator returns the generator to scaffold a evolve integration inside an app. -func NewEvolveGenerator(chain *chain.Chain, withCometMigration bool) (*genny.Generator, error) { +func NewEvolveGenerator(chain *chain.Chain, withCometMigration, withStartCmd bool) (*genny.Generator, error) { g := genny.New() ctx := plush.NewContext() plushhelpers.ExtendPlushContext(ctx) @@ -28,9 +28,12 @@ func NewEvolveGenerator(chain *chain.Chain, withCometMigration bool) (*genny.Gen return nil, errors.Errorf("failed to update go.mod: %w", err) } - g.RunFn(commandsStartModify(appPath, binaryName, chain.Version)) - g.RunFn(commandsGenesisModify(appPath, binaryName)) - g.RunFn(commandsRollbackModify(appPath, binaryName)) + if withStartCmd { + g.RunFn(commandsStartModify(appPath, binaryName, chain.Version)) + g.RunFn(commandsGenesisInitModify(appPath, binaryName)) + g.RunFn(commandsRollbackModify(appPath, binaryName)) + } + g.RunFn(commandsMigrateModify(appPath, binaryName)) if withCometMigration { g.RunFn(migrateFromCometModify(appPath)) } diff --git a/evolve/template/init.go b/evolve/template/init.go index 140246b5..f412bb0c 100644 --- a/evolve/template/init.go +++ b/evolve/template/init.go @@ -71,8 +71,9 @@ func commandsStartModify(appPath, binaryName string, version cosmosver.Version) } } -// commandsGenesisModify modifies the application genesis command to use evolve. -func commandsGenesisModify(appPath, binaryName string) genny.RunFn { +// commandsGenesisInitModify modifies the application genesis init command to use evolve. +// this is only needed when the start command is also modified. +func commandsGenesisInitModify(appPath, binaryName string) genny.RunFn { return func(r *genny.Runner) error { cmdPath := filepath.Join(appPath, "cmd", binaryName, "cmd/commands.go") f, err := r.Disk.Find(cmdPath) @@ -109,13 +110,38 @@ func commandsGenesisModify(appPath, binaryName string) genny.RunFn { } // modify the add commands arguments using xast. - alreadyAdded := false // to avoid adding the migrate command multiple times as there are multiple calls to `rootCmd.AddCommand` content, err = xast.ModifyCaller(content, "rootCmd.AddCommand", func(args []string) ([]string, error) { if strings.Contains(args[0], "InitCmd") { args[0] = "genesisCmd" } - // add migrate command + return args, nil + }) + + return r.File(genny.NewFileS(cmdPath, content)) + } +} + +// commandsMigrateModify adds the evolve migrate command to the application. +func commandsMigrateModify(appPath, binaryName string) genny.RunFn { + return func(r *genny.Runner) error { + cmdPath := filepath.Join(appPath, "cmd", binaryName, "cmd/commands.go") + f, err := r.Disk.Find(cmdPath) + if err != nil { + return err + } + + content, err := xast.AppendImports( + f.String(), + xast.WithNamedImport("abciserver", "github.com/evstack/ev-abci/server"), + ) + if err != nil { + return err + } + + // add migrate command + alreadyAdded := false // to avoid adding the migrate command multiple times as there are multiple calls to `rootCmd.AddCommand` + content, err = xast.ModifyCaller(content, "rootCmd.AddCommand", func(args []string) ([]string, error) { if !alreadyAdded { args = append(args, evolveV1MigrateCmd) alreadyAdded = true diff --git a/evolve/template/migrate.go b/evolve/template/migrate.go index ff677d7e..fc44bdca 100644 --- a/evolve/template/migrate.go +++ b/evolve/template/migrate.go @@ -33,11 +33,26 @@ func migrateFromCometModify(appPath string) genny.RunFn { return err } + // add migrationmngr module config for depinject + moduleConfigTemplate := `{ + Name: migrationmngrtypes.ModuleName, + Config: appconfig.WrapAny(&migrationmngrmodule.Module{}), + }, + %[1]v` + moduleConfigReplacement := fmt.Sprintf(moduleConfigTemplate, module.PlaceholderSgAppModuleConfig) + content = replacer.Replace(content, module.PlaceholderSgAppModuleConfig, moduleConfigReplacement) + + // preblocker for migrationmngr + preBlockerTemplate := `migrationmngrtypes.ModuleName, + %[1]v` + preBlockerReplacement := fmt.Sprintf(preBlockerTemplate, "// this line is used by starport scaffolding # stargate/app/preBlockers") + content = replacer.Replace(content, "// this line is used by starport scaffolding # stargate/app/preBlockers", preBlockerReplacement) + // end block for migrationmngr - template := `migrationmngrtypes.ModuleName, + endBlockerTemplate := `migrationmngrtypes.ModuleName, %[1]v` - replacement := fmt.Sprintf(template, module.PlaceholderSgAppEndBlockers) - content = replacer.Replace(content, module.PlaceholderSgAppEndBlockers, replacement) + endBlockerReplacement := fmt.Sprintf(endBlockerTemplate, module.PlaceholderSgAppEndBlockers) + content = replacer.Replace(content, module.PlaceholderSgAppEndBlockers, endBlockerReplacement) // replace staking blank import content = strings.Replace(content, "github.com/cosmos/cosmos-sdk/x/staking", "github.com/evstack/ev-abci/modules/staking", 1)