Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 7 additions & 1 deletion evolve/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
statusScaffolding = "Scaffolding..."

flagPath = "path"
flagStart = "start"
flagMigrate = "migrate"
)

Expand All @@ -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
Expand All @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions evolve/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
11 changes: 7 additions & 4 deletions evolve/template/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
}
Expand Down
34 changes: 30 additions & 4 deletions evolve/template/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
21 changes: 18 additions & 3 deletions evolve/template/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading