From bdabf2f73bf38d5733c87dc19499cc1247b8b4a3 Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Fri, 16 Feb 2024 20:40:28 -0600 Subject: [PATCH] `golang.org/x/text/language` for Title --- cmd/spawn/module.go | 183 +++++++++++++++++++++++--------------------- go.mod | 1 + go.sum | 2 + 3 files changed, 97 insertions(+), 89 deletions(-) diff --git a/cmd/spawn/module.go b/cmd/spawn/module.go index ed959fea..dda24475 100644 --- a/cmd/spawn/module.go +++ b/cmd/spawn/module.go @@ -11,6 +11,9 @@ import ( "github.com/spf13/cobra" "github.com/strangelove-ventures/simapp" "gitub.com/strangelove-ventures/spawn/spawn" + + textcases "golang.org/x/text/cases" + lang "golang.org/x/text/language" ) var moduleCmd = &cobra.Command{ @@ -70,94 +73,8 @@ var moduleCmd = &cobra.Command{ }, } -// AddModuleToAppGo adds the new module to the app.go file to be registered with the chain binary. -func AddModuleToAppGo(logger *slog.Logger, extName string) error { - extNameTitle := strings.Title(extName) - - cwd, err := os.Getwd() - if err != nil { - fmt.Println("Error getting current working directory", err) - return err - } - - goModName := spawn.ReadCurrentGoModuleName(path.Join(cwd, "go.mod")) - - appGoPath := path.Join(cwd, "app", "app.go") - fmt.Println("appGoPath", appGoPath) - - var buffer []byte - buffer, err = os.ReadFile(appGoPath) - if err != nil { - fmt.Println("Error reading file", err) - return err - } - - // Gets the source code of the app.go file line by line. - appGoLines := strings.Split(string(buffer), "\n") - - // generates the new imports for the module - appGoLines = appendNewImportsToSource( - appGoPath, // reads file imports from this location - appGoLines, - []string{ - // example "github.com/rollchain/simapp/x/example" - fmt.Sprintf(`%s "%s/x/%s"`, extName, goModName, extName), - fmt.Sprintf(`%skeeper "%s/x/%s/keeper"`, extName, goModName, extName), - fmt.Sprintf(`%stypes "%s/x/%s/types"`, extName, goModName, extName), - }, - ) - - // Add keeper to the ChainApp struct. - appModuleManagerLine := spawn.FindLineWithText(appGoLines, "*module.Manager") - fmt.Println("appModuleManager", appModuleManagerLine) - appGoLines = append(appGoLines[:appModuleManagerLine-2], append([]string{fmt.Sprintf(` %sKeeper %skeeper.Keeper`, extNameTitle, extName)}, appGoLines[appModuleManagerLine-2:]...)...) - - // Setup the new module store key. - start, end := spawn.FindLinesWithText(appGoLines, "storetypes.NewKVStoreKeys") - logger.Debug("store key", "extName", extName, "start", start, "end", end) - appGoLines = append(appGoLines[:end-1], append([]string{fmt.Sprintf(` %stypes.StoreKey,`, extName)}, appGoLines[end-1:]...)...) - - // Initialize the new module keeper. - evidenceTextLine := spawn.FindLineWithText(appGoLines, "app.EvidenceKeeper = *evidenceKeeper") - logger.Debug("evidence keeper", "extName", extName, "line", evidenceTextLine) - keeperText := fmt.Sprintf(` // Create the %s Keeper - app.%sKeeper = %skeeper.NewKeeper( - appCodec, - runtime.NewKVStoreService(keys[%stypes.StoreKey]), - logger, - )`+"\n", extName, extNameTitle, extName, extName) - appGoLines = append(appGoLines[:evidenceTextLine+2], append([]string{keeperText}, appGoLines[evidenceTextLine+2:]...)...) - - // Register the app module. - start, end = spawn.FindLinesWithText(appGoLines, "app.ModuleManager = module.NewManager") - logger.Debug("module manager", "extName", extName, "start", start, "end", end) - newAppModuleText := fmt.Sprintf(` %s.NewAppModule(appCodec, app.%sKeeper),`+"\n", extName, extNameTitle) - appGoLines = append(appGoLines[:end-1], append([]string{newAppModuleText}, appGoLines[end-1:]...)...) - - // Set the begin block order of the new module. - start, end = spawn.FindLinesWithText(appGoLines, "SetOrderBeginBlockers(") - logger.Debug("begin block order", "extName", extName, "start", start, "end", end) - appGoLines = append(appGoLines[:end-1], append([]string{fmt.Sprintf(` %stypes.ModuleName,`, extName)}, appGoLines[end-1:]...)...) - - // Set the end block order of the new module. - start, end = spawn.FindLinesWithText(appGoLines, "SetOrderEndBlockers(") - logger.Debug("end block order", "extName", extName, "start", start, "end", end) - appGoLines = append(appGoLines[:end-1], append([]string{fmt.Sprintf(` %stypes.ModuleName,`, extName)}, appGoLines[end-1:]...)...) - - // Set the genesis module order of the new module. - start, end = spawn.FindLinesWithText(appGoLines, "genesisModuleOrder := []string") - logger.Debug("genesis module order", "extName", extName, "start", start, "end", end) - appGoLines = append(appGoLines[:end-1], append([]string{fmt.Sprintf(` %stypes.ModuleName,`, extName)}, appGoLines[end-1:]...)...) - - // Register the params to x/params module. (Removed in SDK v51) - start, end = spawn.FindLinesWithText(appGoLines, "initParamsKeeper(appCodec") - logger.Debug("initParamsKeeper register", "extName", extName, "start", start, "end", end) - appGoLines = append(appGoLines[:end-3], append([]string{fmt.Sprintf(` paramsKeeper.Subspace(%stypes.ModuleName)`, extName)}, appGoLines[end-3:]...)...) - - // save the new app.go - return os.WriteFile(appGoPath, []byte(strings.Join(appGoLines, "\n")), 0644) -} - +// SetupModuleProtoBase iterates through the proto embedded fs and replaces the paths and goMod names to match +// the new desired module. func SetupModuleProtoBase(logger *slog.Logger, extName string) error { protoFS := simapp.ProtoModuleFS @@ -202,6 +119,8 @@ func SetupModuleProtoBase(logger *slog.Logger, extName string) error { }) } +// SetupModuleExtensionFiles iterates through the x/example embedded fs and replaces the paths and goMod names to match +// the new desired module. func SetupModuleExtensionFiles(logger *slog.Logger, extName string) error { extFS := simapp.ExtensionFS @@ -245,6 +164,93 @@ func SetupModuleExtensionFiles(logger *slog.Logger, extName string) error { }) } +// AddModuleToAppGo adds the new module to the app.go file. +func AddModuleToAppGo(logger *slog.Logger, extName string) error { + extNameTitle := textcases.Title(lang.AmericanEnglish).String(extName) + + cwd, err := os.Getwd() + if err != nil { + fmt.Println("Error getting current working directory", err) + return err + } + + goModName := spawn.ReadCurrentGoModuleName(path.Join(cwd, "go.mod")) + + appGoPath := path.Join(cwd, "app", "app.go") + fmt.Println("appGoPath", appGoPath) + + var buffer []byte + buffer, err = os.ReadFile(appGoPath) + if err != nil { + fmt.Println("Error reading file", err) + return err + } + + // Gets the source code of the app.go file line by line. + appGoLines := strings.Split(string(buffer), "\n") + + // generates the new imports for the module + appGoLines = appendNewImportsToSource( + appGoPath, // reads file imports from this location + appGoLines, + []string{ + // example "github.com/rollchain/simapp/x/example" + fmt.Sprintf(`%s "%s/x/%s"`, extName, goModName, extName), + fmt.Sprintf(`%skeeper "%s/x/%s/keeper"`, extName, goModName, extName), + fmt.Sprintf(`%stypes "%s/x/%s/types"`, extName, goModName, extName), + }, + ) + + // Add keeper to the ChainApp struct. + appModuleManagerLine := spawn.FindLineWithText(appGoLines, "*module.Manager") + fmt.Println("appModuleManager", appModuleManagerLine) + appGoLines = append(appGoLines[:appModuleManagerLine-2], append([]string{fmt.Sprintf(` %sKeeper %skeeper.Keeper`, extNameTitle, extName)}, appGoLines[appModuleManagerLine-2:]...)...) + + // Setup the new module store key. + start, end := spawn.FindLinesWithText(appGoLines, "storetypes.NewKVStoreKeys") + logger.Debug("store key", "extName", extName, "start", start, "end", end) + appGoLines = append(appGoLines[:end-1], append([]string{fmt.Sprintf(` %stypes.StoreKey,`, extName)}, appGoLines[end-1:]...)...) + + // Initialize the new module keeper. + evidenceTextLine := spawn.FindLineWithText(appGoLines, "app.EvidenceKeeper = *evidenceKeeper") + logger.Debug("evidence keeper", "extName", extName, "line", evidenceTextLine) + keeperText := fmt.Sprintf(` // Create the %s Keeper + app.%sKeeper = %skeeper.NewKeeper( + appCodec, + runtime.NewKVStoreService(keys[%stypes.StoreKey]), + logger, + )`+"\n", extName, extNameTitle, extName, extName) + appGoLines = append(appGoLines[:evidenceTextLine+2], append([]string{keeperText}, appGoLines[evidenceTextLine+2:]...)...) + + // Register the app module. + start, end = spawn.FindLinesWithText(appGoLines, "app.ModuleManager = module.NewManager") + logger.Debug("module manager", "extName", extName, "start", start, "end", end) + newAppModuleText := fmt.Sprintf(` %s.NewAppModule(appCodec, app.%sKeeper),`+"\n", extName, extNameTitle) + appGoLines = append(appGoLines[:end-1], append([]string{newAppModuleText}, appGoLines[end-1:]...)...) + + // Set the begin block order of the new module. + start, end = spawn.FindLinesWithText(appGoLines, "SetOrderBeginBlockers(") + logger.Debug("begin block order", "extName", extName, "start", start, "end", end) + appGoLines = append(appGoLines[:end-1], append([]string{fmt.Sprintf(` %stypes.ModuleName,`, extName)}, appGoLines[end-1:]...)...) + + // Set the end block order of the new module. + start, end = spawn.FindLinesWithText(appGoLines, "SetOrderEndBlockers(") + logger.Debug("end block order", "extName", extName, "start", start, "end", end) + appGoLines = append(appGoLines[:end-1], append([]string{fmt.Sprintf(` %stypes.ModuleName,`, extName)}, appGoLines[end-1:]...)...) + + // Set the genesis module order of the new module. + start, end = spawn.FindLinesWithText(appGoLines, "genesisModuleOrder := []string") + logger.Debug("genesis module order", "extName", extName, "start", start, "end", end) + appGoLines = append(appGoLines[:end-1], append([]string{fmt.Sprintf(` %stypes.ModuleName,`, extName)}, appGoLines[end-1:]...)...) + + // Register the params to x/params module. (Removed in SDK v51) + start, end = spawn.FindLinesWithText(appGoLines, "initParamsKeeper(appCodec") + logger.Debug("initParamsKeeper register", "extName", extName, "start", start, "end", end) + appGoLines = append(appGoLines[:end-3], append([]string{fmt.Sprintf(` paramsKeeper.Subspace(%stypes.ModuleName)`, extName)}, appGoLines[end-3:]...)...) + + return os.WriteFile(appGoPath, []byte(strings.Join(appGoLines, "\n")), 0644) +} + // appendNewImportsToSource appends new imports to the source file at the end of the import block (before the closing `)` ). func appendNewImportsToSource(filePath string, oldSource, newImports []string) []string { imports, start, end, err := spawn.ParseFileImports(filePath) @@ -263,7 +269,6 @@ func appendNewImportsToSource(filePath string, oldSource, newImports []string) [ // convertGoModuleNameToProtoNamespace converts the github.com/*/* module name to a proto module compatible name. // i.e. github.com/rollchains/myproject -> rollchains.myproject func convertGoModuleNameToProtoNamespace(moduleName string) string { - // github.com/rollchains/myproject -> rollchains.myproject text := strings.Replace(moduleName, "github.com/", "", 1) return strings.Replace(text, "/", ".", -1) } diff --git a/go.mod b/go.mod index 7e7d4a29..2846afa2 100644 --- a/go.mod +++ b/go.mod @@ -21,5 +21,6 @@ require ( github.com/mattn/go-isatty v0.0.20 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect golang.org/x/sys v0.17.0 // indirect + golang.org/x/text v0.14.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index d26fccb2..9b20908d 100644 --- a/go.sum +++ b/go.sum @@ -34,6 +34,8 @@ golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=