From 59400a520fb3fc4a67799a54df39705c91e4374a Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Fri, 8 Mar 2024 16:55:03 -0300 Subject: [PATCH] feat: remove all import placeholders using the `xast` pkg (#4004) * remove all import placeholders * add changelog * remove unused placeholders * fix wrong import path * improve readbility * remove keeper definition placeholder --------- Co-authored-by: Pantani --- changelog.md | 1 + .../testdata/modules/app_config/app_config.go | 1 - .../app/testdata/modules/spn/app.go | 1 - .../module/testdata/earth/app/app_config.go | 1 - ignite/pkg/xast/import.go | 35 +++++++++- ignite/pkg/xast/import_test.go | 38 +++++------ .../app/files-minimal/app/app.go.plush | 4 +- .../app/files-minimal/app/app_config.go.plush | 2 - ignite/templates/app/files/app/app.go.plush | 4 +- .../app/files/app/app_config.go.plush | 2 - ignite/templates/app/files/app/ibc.go.plush | 2 - ignite/templates/ibc/placeholders.go | 12 +--- ignite/templates/message/placeholders.go | 1 - ignite/templates/module/create/base.go | 65 +++++++++++++------ .../x/{{moduleName}}/types/genesis.go.plush | 4 -- ignite/templates/module/create/ibc.go | 31 ++++++--- ignite/templates/module/placeholders.go | 4 -- ignite/templates/typed/genesis.go | 22 ------- ignite/templates/typed/list/genesis.go | 9 +-- ignite/templates/typed/map/map.go | 9 +-- ignite/templates/typed/placeholders.go | 2 - ignite/templates/typed/singleton/singleton.go | 4 +- 22 files changed, 134 insertions(+), 120 deletions(-) diff --git a/changelog.md b/changelog.md index 6a5c65c6c5..617ab6854e 100644 --- a/changelog.md +++ b/changelog.md @@ -8,6 +8,7 @@ - [#3770](https://github.com/ignite/cli/pull/3770) Add `scaffold configs` and `scaffold params` commands - [#3985](https://github.com/ignite/cli/pull/3985) Make some `cmd` pkg functions public - [#3967](https://github.com/ignite/cli/issues/3967) Add HD wallet parameters `address index` and `account number` to the chain account config +- [#4004](https://github.com/ignite/cli/pull/4004) Remove all import placeholders using the `xast` pkg ### Changes diff --git a/ignite/pkg/cosmosanalysis/app/testdata/modules/app_config/app_config.go b/ignite/pkg/cosmosanalysis/app/testdata/modules/app_config/app_config.go index 42e35eaa42..1e9aca5a9e 100644 --- a/ignite/pkg/cosmosanalysis/app/testdata/modules/app_config/app_config.go +++ b/ignite/pkg/cosmosanalysis/app/testdata/modules/app_config/app_config.go @@ -74,7 +74,6 @@ import ( marsmodulev1 "github.com/ignite/mars/api/mars/mars/module" _ "github.com/ignite/mars/x/mars" // import for side-effects marsmoduletypes "github.com/ignite/mars/x/mars/types" - // this line is used by starport scaffolding # stargate/app/moduleImport ) var ( diff --git a/ignite/pkg/cosmosanalysis/app/testdata/modules/spn/app.go b/ignite/pkg/cosmosanalysis/app/testdata/modules/spn/app.go index 40070dd24f..0da49006f5 100644 --- a/ignite/pkg/cosmosanalysis/app/testdata/modules/spn/app.go +++ b/ignite/pkg/cosmosanalysis/app/testdata/modules/spn/app.go @@ -125,7 +125,6 @@ import ( "github.com/tendermint/spn/x/reward" rewardkeeper "github.com/tendermint/spn/x/reward/keeper" rewardtypes "github.com/tendermint/spn/x/reward/types" - // this line is used by starport scaffolding # stargate/app/moduleImport ) const ( diff --git a/ignite/pkg/cosmosanalysis/module/testdata/earth/app/app_config.go b/ignite/pkg/cosmosanalysis/module/testdata/earth/app/app_config.go index 8ff441e81b..7f50257e85 100644 --- a/ignite/pkg/cosmosanalysis/module/testdata/earth/app/app_config.go +++ b/ignite/pkg/cosmosanalysis/module/testdata/earth/app/app_config.go @@ -77,7 +77,6 @@ import ( marsmodulev1 "github.com/tendermint/mars/api/mars/mars/module" _ "github.com/tendermint/mars/x/mars" // import for side-effects marsmoduletypes "github.com/tendermint/mars/x/mars/types" - // this line is used by starport scaffolding # stargate/app/moduleImport ) var ( diff --git a/ignite/pkg/xast/import.go b/ignite/pkg/xast/import.go index 08942c5bb5..0fb57271b3 100644 --- a/ignite/pkg/xast/import.go +++ b/ignite/pkg/xast/import.go @@ -27,17 +27,50 @@ type ( } ) +// WithLastImport add a new import int the end. +func WithLastImport(repo string) ImportOptions { + return func(c *importOpts) { + c.imports = append(c.imports, imp{ + repo: repo, + name: "", + index: -1, + }) + } +} + // WithImport add a new import. If the index is -1 will append in the end of the imports. -func WithImport(repo, name string, index int) ImportOptions { +func WithImport(repo string, index int) ImportOptions { return func(c *importOpts) { c.imports = append(c.imports, imp{ repo: repo, + name: "", + index: index, + }) + } +} + +// WithNamedImport add a new import with name. If the index is -1 will append in the end of the imports. +func WithNamedImport(name, repo string, index int) ImportOptions { + return func(c *importOpts) { + c.imports = append(c.imports, imp{ name: name, + repo: repo, index: index, }) } } +// WithLastNamedImport add a new import with name in the end of the imports. +func WithLastNamedImport(name, repo string) ImportOptions { + return func(c *importOpts) { + c.imports = append(c.imports, imp{ + name: name, + repo: repo, + index: -1, + }) + } +} + func newImportOptions() importOpts { return importOpts{ imports: make([]imp, 0), diff --git a/ignite/pkg/xast/import_test.go b/ignite/pkg/xast/import_test.go index e9f564fbab..afa0c28794 100644 --- a/ignite/pkg/xast/import_test.go +++ b/ignite/pkg/xast/import_test.go @@ -34,7 +34,7 @@ func main() { args: args{ fileContent: existingContent, imports: []ImportOptions{ - WithImport("strings", "", -1), + WithImport("strings", -1), }, }, want: `package main @@ -54,9 +54,9 @@ func main() { args: args{ fileContent: existingContent, imports: []ImportOptions{ - WithImport("strings", "st", -1), - WithImport("strconv", "", -1), - WithImport("os", "", -1), + WithNamedImport("st", "strings", -1), + WithImport("strconv", -1), + WithLastImport("os"), }, }, want: `package main @@ -78,10 +78,10 @@ func main() { args: args{ fileContent: existingContent, imports: []ImportOptions{ - WithImport("strings", "st", -1), - WithImport("strconv", "", -1), - WithImport("os", "", -1), - WithImport("fmt", "", -1), + WithNamedImport("st", "strings", -1), + WithImport("strconv", -1), + WithImport("os", -1), + WithLastImport("fmt"), }, }, want: `package main @@ -109,7 +109,7 @@ import ( st "strings" )`, imports: []ImportOptions{ - WithImport("strconv", "", 1), + WithImport("strconv", 1), }, }, want: `package main @@ -133,9 +133,9 @@ import ( st "strings" )`, imports: []ImportOptions{ - WithImport("strconv", "", 0), - WithImport("testing", "", 3), - WithImport("bytes", "", -1), + WithImport("strconv", 0), + WithNamedImport("", "testing", 3), + WithLastImport("bytes"), }, }, want: `package main @@ -155,7 +155,7 @@ import ( args: args{ fileContent: existingContent, imports: []ImportOptions{ - WithImport("fmt", "", -1), + WithLastImport("fmt"), }, }, want: existingContent + "\n", @@ -169,7 +169,7 @@ func main() { fmt.Println("Hello, world!") }`, imports: []ImportOptions{ - WithImport("fmt", "", -1), + WithImport("fmt", -1), }, }, want: `package main @@ -190,8 +190,8 @@ func main() { fmt.Println("Hello, world!") }`, imports: []ImportOptions{ - WithImport("fmt", "", -1), - WithImport("os", "", -1), + WithImport("fmt", -1), + WithLastImport("os"), }, }, want: `package main @@ -211,7 +211,7 @@ func main() { args: args{ fileContent: existingContent, imports: []ImportOptions{ - WithImport("strings", "", 10), + WithImport("strings", 10), }, }, err: errors.New("index out of range"), @@ -221,7 +221,7 @@ func main() { args: args{ fileContent: existingContent, imports: []ImportOptions{ - WithImport("fmt\"", "fmt\"", -1), + WithNamedImport("fmt\"", "fmt\"", -1), }, }, err: errors.New("format.Node internal error (5:8: expected ';', found fmt (and 2 more errors))"), @@ -231,7 +231,7 @@ func main() { args: args{ fileContent: "", imports: []ImportOptions{ - WithImport("fmt", "", -1), + WithImport("fmt", -1), }, }, err: errors.New("1:1: expected 'package', found 'EOF'"), diff --git a/ignite/templates/app/files-minimal/app/app.go.plush b/ignite/templates/app/files-minimal/app/app.go.plush index e5ea32dc46..82ed491840 100644 --- a/ignite/templates/app/files-minimal/app/app.go.plush +++ b/ignite/templates/app/files-minimal/app/app.go.plush @@ -38,8 +38,6 @@ import ( mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" - // this line is used by starport scaffolding # stargate/app/moduleImport - "<%= ModulePath %>/docs" ) @@ -181,7 +179,7 @@ func New( &app.StakingKeeper, &app.DistrKeeper, &app.ConsensusParamsKeeper, - // this line is used by starport scaffolding # stargate/app/keeperDefinition + ); err != nil { panic(err) } diff --git a/ignite/templates/app/files-minimal/app/app_config.go.plush b/ignite/templates/app/files-minimal/app/app_config.go.plush index e133dd41a7..02db10bacf 100644 --- a/ignite/templates/app/files-minimal/app/app_config.go.plush +++ b/ignite/templates/app/files-minimal/app/app_config.go.plush @@ -20,8 +20,6 @@ import ( genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - - // this line is used by starport scaffolding # stargate/app/moduleImport ) var ( diff --git a/ignite/templates/app/files/app/app.go.plush b/ignite/templates/app/files/app/app.go.plush index 00d39212b3..2673a020f2 100644 --- a/ignite/templates/app/files/app/app.go.plush +++ b/ignite/templates/app/files/app/app.go.plush @@ -76,8 +76,6 @@ import ( ibctransferkeeper "github.com/cosmos/ibc-go/v8/modules/apps/transfer/keeper" ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper" - // this line is used by starport scaffolding # stargate/app/moduleImport - "<%= ModulePath %>/docs" ) @@ -278,7 +276,7 @@ func New( &app.NFTKeeper, &app.GroupKeeper, &app.CircuitBreakerKeeper, - // this line is used by starport scaffolding # stargate/app/keeperDefinition + ); err != nil { panic(err) } diff --git a/ignite/templates/app/files/app/app_config.go.plush b/ignite/templates/app/files/app/app_config.go.plush index 02f6f7531e..dc0c19a24b 100644 --- a/ignite/templates/app/files/app/app_config.go.plush +++ b/ignite/templates/app/files/app/app_config.go.plush @@ -52,8 +52,6 @@ import ( ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types" ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported" "google.golang.org/protobuf/types/known/durationpb" - - // this line is used by starport scaffolding # stargate/app/moduleImport ) var ( diff --git a/ignite/templates/app/files/app/ibc.go.plush b/ignite/templates/app/files/app/ibc.go.plush index cd3e156664..9e255b0cab 100644 --- a/ignite/templates/app/files/app/ibc.go.plush +++ b/ignite/templates/app/files/app/ibc.go.plush @@ -34,8 +34,6 @@ import ( ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper" solomachine "github.com/cosmos/ibc-go/v8/modules/light-clients/06-solomachine" ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint" - - // this line is used by starport scaffolding # ibc/app/import ) // registerIBCModules register IBC keepers and non dependency inject modules. diff --git a/ignite/templates/ibc/placeholders.go b/ignite/templates/ibc/placeholders.go index ea2fc9077c..d1dfd154b9 100644 --- a/ignite/templates/ibc/placeholders.go +++ b/ignite/templates/ibc/placeholders.go @@ -2,21 +2,11 @@ package ibc //nolint:godot const ( - Placeholder = "// this line is used by starport scaffolding # 1" - Placeholder2 = "// this line is used by starport scaffolding # 2" - Placeholder3 = "// this line is used by starport scaffolding # 3" + Placeholder = "// this line is used by starport scaffolding # 1" // Placeholders IBC packets PlaceholderIBCPacketEvent = "// this line is used by starport scaffolding # ibc/packet/event" PlaceholderIBCPacketModuleRecv = "// this line is used by starport scaffolding # ibc/packet/module/recv" PlaceholderIBCPacketModuleAck = "// this line is used by starport scaffolding # ibc/packet/module/ack" PlaceholderIBCPacketModuleTimeout = "// this line is used by starport scaffolding # ibc/packet/module/timeout" - - // Placeholders for messages - PlaceholderProtoTxRPC = "// this line is used by starport scaffolding # proto/tx/rpc" - PlaceholderProtoTxMessage = "// this line is used by starport scaffolding # proto/tx/message" - - // Placeholders AutoCLI - PlaceholderAutoCLIQuery = "// this line is used by ignite scaffolding # autocli/query" - PlaceholderAutoCLITx = "// this line is used by ignite scaffolding # autocli/tx" ) diff --git a/ignite/templates/message/placeholders.go b/ignite/templates/message/placeholders.go index b6b121fb53..c6cf265b8a 100644 --- a/ignite/templates/message/placeholders.go +++ b/ignite/templates/message/placeholders.go @@ -2,6 +2,5 @@ package message const ( Placeholder = "// this line is used by starport scaffolding # 1" - Placeholder2 = "// this line is used by starport scaffolding # 2" Placeholder3 = "// this line is used by starport scaffolding # 3" ) diff --git a/ignite/templates/module/create/base.go b/ignite/templates/module/create/base.go index 0af3c1de82..99dc5d6464 100644 --- a/ignite/templates/module/create/base.go +++ b/ignite/templates/module/create/base.go @@ -10,6 +10,7 @@ import ( "github.com/ignite/cli/v28/ignite/pkg/gomodulepath" "github.com/ignite/cli/v28/ignite/pkg/placeholder" + "github.com/ignite/cli/v28/ignite/pkg/xast" "github.com/ignite/cli/v28/ignite/pkg/xgenny" "github.com/ignite/cli/v28/ignite/pkg/xstrings" "github.com/ignite/cli/v28/ignite/templates/field/plushhelpers" @@ -82,17 +83,29 @@ func appConfigModify(replacer placeholder.Replacer, opts *CreateOptions) genny.R } // Import - template := `%[2]vmodulev1 "%[3]v/api/%[4]v/%[2]v/module" -_ "%[3]v/x/%[2]v/module" // import for side-effects -%[2]vmoduletypes "%[3]v/x/%[2]v/types" -%[1]v` - replacement := fmt.Sprintf(template, module.PlaceholderSgAppModuleImport, opts.ModuleName, opts.ModulePath, opts.AppName) - content := replacer.Replace(fConfig.String(), module.PlaceholderSgAppModuleImport, replacement) + content, err := xast.AppendImports( + fConfig.String(), + xast.WithLastNamedImport( + fmt.Sprintf("%[1]vmodulev1", opts.ModuleName), + fmt.Sprintf("%[1]v/api/%[2]v/%[3]v/module", opts.ModulePath, opts.AppName, opts.ModuleName), + ), + xast.WithLastNamedImport( + "_", + fmt.Sprintf("%[1]v/x/%[2]v/module", opts.ModulePath, opts.ModuleName), + ), + xast.WithLastNamedImport( + fmt.Sprintf("%[1]vmoduletypes", opts.ModuleName), + fmt.Sprintf("%[1]v/x/%[2]v/types", opts.ModulePath, opts.ModuleName), + ), + ) + if err != nil { + return err + } // Init genesis - template = `%[2]vmoduletypes.ModuleName, + template := `%[2]vmoduletypes.ModuleName, %[1]v` - replacement = fmt.Sprintf(template, module.PlaceholderSgAppInitGenesis, opts.ModuleName) + replacement := fmt.Sprintf(template, module.PlaceholderSgAppInitGenesis, opts.ModuleName) content = replacer.Replace(content, module.PlaceholderSgAppInitGenesis, replacement) replacement = fmt.Sprintf(template, module.PlaceholderSgAppBeginBlockers, opts.ModuleName) content = replacer.Replace(content, module.PlaceholderSgAppBeginBlockers, replacement) @@ -139,15 +152,21 @@ func appModify(replacer placeholder.Replacer, opts *CreateOptions) genny.RunFn { } // Import - template := `%[2]vmodulekeeper "%[3]v/x/%[2]v/keeper" -%[1]v` - replacement := fmt.Sprintf(template, module.PlaceholderSgAppModuleImport, opts.ModuleName, opts.ModulePath) - content := replacer.Replace(f.String(), module.PlaceholderSgAppModuleImport, replacement) + content, err := xast.AppendImports( + f.String(), + xast.WithLastNamedImport( + fmt.Sprintf("%[1]vmodulekeeper", opts.ModuleName), + fmt.Sprintf("%[1]v/x/%[2]v/keeper", opts.ModulePath, opts.ModuleName), + ), + ) + if err != nil { + return err + } // Keeper declaration - template = `%[2]vKeeper %[3]vmodulekeeper.Keeper + template := `%[2]vKeeper %[3]vmodulekeeper.Keeper %[1]v` - replacement = fmt.Sprintf( + replacement := fmt.Sprintf( template, module.PlaceholderSgAppKeeperDeclaration, xstrings.Title(opts.ModuleName), @@ -156,14 +175,18 @@ func appModify(replacer placeholder.Replacer, opts *CreateOptions) genny.RunFn { content = replacer.Replace(content, module.PlaceholderSgAppKeeperDeclaration, replacement) // Keeper definition - template = `&app.%[2]vKeeper, - %[1]v` - replacement = fmt.Sprintf( - template, - module.PlaceholderSgAppKeeperDefinition, - xstrings.Title(opts.ModuleName), + content, err = xast.ModifyFunction( + content, + "New", + xast.AppendInsideFuncCall( + "Inject", + fmt.Sprintf("\n&app.%[1]vKeeper", xstrings.Title(opts.ModuleName)), + -1, + ), ) - content = replacer.Replace(content, module.PlaceholderSgAppKeeperDefinition, replacement) + if err != nil { + return err + } newFile := genny.NewFileS(appPath, content) return r.File(newFile) diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/types/genesis.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/types/genesis.go.plush index c41be07425..bb4de5a6f5 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/types/genesis.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/types/genesis.go.plush @@ -1,9 +1,5 @@ package types -import ( -// this line is used by starport scaffolding # genesis/types/import -) - // DefaultIndex is the default global index const DefaultIndex uint64 = 1 diff --git a/ignite/templates/module/create/ibc.go b/ignite/templates/module/create/ibc.go index 557b984c64..9cdad45009 100644 --- a/ignite/templates/module/create/ibc.go +++ b/ignite/templates/module/create/ibc.go @@ -11,6 +11,7 @@ import ( "github.com/ignite/cli/v28/ignite/pkg/gomodulepath" "github.com/ignite/cli/v28/ignite/pkg/placeholder" "github.com/ignite/cli/v28/ignite/pkg/protoanalysis/protoutil" + "github.com/ignite/cli/v28/ignite/pkg/xast" "github.com/ignite/cli/v28/ignite/pkg/xgenny" "github.com/ignite/cli/v28/ignite/pkg/xstrings" "github.com/ignite/cli/v28/ignite/templates/field/plushhelpers" @@ -95,10 +96,13 @@ func genesisTypesModify(replacer placeholder.Replacer, opts *CreateOptions) genn } // Import - templateImport := `host "github.com/cosmos/ibc-go/v8/modules/core/24-host" -%s` - replacementImport := fmt.Sprintf(templateImport, typed.PlaceholderGenesisTypesImport) - content := replacer.Replace(f.String(), typed.PlaceholderGenesisTypesImport, replacementImport) + content, err := xast.AppendImports( + f.String(), + xast.WithLastNamedImport("host", "github.com/cosmos/ibc-go/v8/modules/core/24-host"), + ) + if err != nil { + return err + } // Default genesis templateDefault := `PortId: PortID, @@ -189,11 +193,20 @@ func appIBCModify(replacer placeholder.Replacer, opts *CreateOptions) genny.RunF } // Import - templateImport := `%[1]v -%[2]vmodule "%[3]v/x/%[2]v/module" -%[2]vmoduletypes "%[3]v/x/%[2]v/types"` - replacementImport := fmt.Sprintf(templateImport, module.PlaceholderIBCImport, opts.ModuleName, opts.ModulePath) - content := replacer.Replace(f.String(), module.PlaceholderIBCImport, replacementImport) + content, err := xast.AppendImports( + f.String(), + xast.WithLastNamedImport( + fmt.Sprintf("%[1]vmodule", opts.ModuleName), + fmt.Sprintf("%[1]v/x/%[2]v/module", opts.ModulePath, opts.ModuleName), + ), + xast.WithLastNamedImport( + fmt.Sprintf("%[1]vmoduletypes", opts.ModuleName), + fmt.Sprintf("%[1]v/x/%[2]v/types", opts.ModulePath, opts.ModuleName), + ), + ) + if err != nil { + return err + } // create IBC module templateIBCModule := `%[2]vIBCModule := ibcfee.NewIBCMiddleware(%[2]vmodule.NewIBCModule(app.%[3]vKeeper), app.IBCFeeKeeper) diff --git a/ignite/templates/module/placeholders.go b/ignite/templates/module/placeholders.go index d8cf25041a..84c4040443 100644 --- a/ignite/templates/module/placeholders.go +++ b/ignite/templates/module/placeholders.go @@ -3,13 +3,10 @@ package module //nolint:godot const ( Placeholder = "// this line is used by starport scaffolding # 1" - Placeholder2 = "// this line is used by starport scaffolding # 2" Placeholder3 = "// this line is used by starport scaffolding # 3" // Placeholders in app.go - PlaceholderSgAppModuleImport = "// this line is used by starport scaffolding # stargate/app/moduleImport" PlaceholderSgAppKeeperDeclaration = "// this line is used by starport scaffolding # stargate/app/keeperDeclaration" - PlaceholderSgAppKeeperDefinition = "// this line is used by starport scaffolding # stargate/app/keeperDefinition" PlaceholderSgAppInitGenesis = "// this line is used by starport scaffolding # stargate/app/initGenesis" PlaceholderSgAppBeginBlockers = "// this line is used by starport scaffolding # stargate/app/beginBlockers" PlaceholderSgAppEndBlockers = "// this line is used by starport scaffolding # stargate/app/endBlockers" @@ -17,7 +14,6 @@ const ( PlaceholderSgAppModuleConfig = "// this line is used by starport scaffolding # stargate/app/moduleConfig" // Placeholders IBC - PlaceholderIBCImport = "// this line is used by starport scaffolding # ibc/app/import" PlaceholderIBCKeysName = "// this line is used by starport scaffolding # ibc/keys/name" PlaceholderIBCKeysPort = "// this line is used by starport scaffolding # ibc/keys/port" PlaceholderIBCNewModule = "// this line is used by starport scaffolding # ibc/app/module" diff --git a/ignite/templates/typed/genesis.go b/ignite/templates/typed/genesis.go index f58fa6cc39..a0a2bb6398 100644 --- a/ignite/templates/typed/genesis.go +++ b/ignite/templates/typed/genesis.go @@ -1,26 +1,4 @@ package typed -import ( - "fmt" - "strings" - - "github.com/ignite/cli/v28/ignite/pkg/placeholder" -) - // ProtoGenesisStateMessage is the name of the proto message that represents the genesis state. const ProtoGenesisStateMessage = "GenesisState" - -// PatchGenesisTypeImport patches types/genesis.go content from the issue: -// https://github.com/ignite/cli/issues/992 -func PatchGenesisTypeImport(replacer placeholder.Replacer, content string) string { - patternToCheck := "import (" - replacement := fmt.Sprintf(`import ( -%[1]v -)`, PlaceholderGenesisTypesImport) - - if !strings.Contains(content, patternToCheck) { - content = replacer.Replace(content, PlaceholderGenesisTypesImport, replacement) - } - - return content -} diff --git a/ignite/templates/typed/list/genesis.go b/ignite/templates/typed/list/genesis.go index c6f06ff79d..afe17718d3 100644 --- a/ignite/templates/typed/list/genesis.go +++ b/ignite/templates/typed/list/genesis.go @@ -9,6 +9,7 @@ import ( "github.com/ignite/cli/v28/ignite/pkg/errors" "github.com/ignite/cli/v28/ignite/pkg/placeholder" "github.com/ignite/cli/v28/ignite/pkg/protoanalysis/protoutil" + "github.com/ignite/cli/v28/ignite/pkg/xast" "github.com/ignite/cli/v28/ignite/templates/module" "github.com/ignite/cli/v28/ignite/templates/typed" ) @@ -74,10 +75,10 @@ func genesisTypesModify(replacer placeholder.Replacer, opts *typed.Options) genn return err } - content := typed.PatchGenesisTypeImport(replacer, f.String()) - - templateTypesImport := `"fmt"` - content = replacer.ReplaceOnce(content, typed.PlaceholderGenesisTypesImport, templateTypesImport) + content, err := xast.AppendImports(f.String(), xast.WithLastImport("fmt")) + if err != nil { + return err + } templateTypesDefault := `%[2]vList: []%[2]v{}, %[1]v` diff --git a/ignite/templates/typed/map/map.go b/ignite/templates/typed/map/map.go index b8fda5cd9a..a578299ba0 100644 --- a/ignite/templates/typed/map/map.go +++ b/ignite/templates/typed/map/map.go @@ -13,6 +13,7 @@ import ( "github.com/ignite/cli/v28/ignite/pkg/gomodulepath" "github.com/ignite/cli/v28/ignite/pkg/placeholder" "github.com/ignite/cli/v28/ignite/pkg/protoanalysis/protoutil" + "github.com/ignite/cli/v28/ignite/pkg/xast" "github.com/ignite/cli/v28/ignite/pkg/xgenny" "github.com/ignite/cli/v28/ignite/templates/field/datatype" "github.com/ignite/cli/v28/ignite/templates/module" @@ -328,10 +329,10 @@ func genesisTypesModify(replacer placeholder.Replacer, opts *typed.Options) genn return err } - content := typed.PatchGenesisTypeImport(replacer, f.String()) - - templateTypesImport := `"fmt"` - content = replacer.ReplaceOnce(content, typed.PlaceholderGenesisTypesImport, templateTypesImport) + content, err := xast.AppendImports(f.String(), xast.WithLastImport("fmt")) + if err != nil { + return err + } templateTypesDefault := `%[2]vList: []%[2]v{}, %[1]v` diff --git a/ignite/templates/typed/placeholders.go b/ignite/templates/typed/placeholders.go index 1ca61b23fb..78cfa927fd 100644 --- a/ignite/templates/typed/placeholders.go +++ b/ignite/templates/typed/placeholders.go @@ -3,12 +3,10 @@ package typed //nolint:godot const ( Placeholder = "// this line is used by starport scaffolding # 1" - Placeholder2 = "// this line is used by starport scaffolding # 2" Placeholder3 = "// this line is used by starport scaffolding # 3" Placeholder4 = "" // Genesis - PlaceholderGenesisTypesImport = "// this line is used by starport scaffolding # genesis/types/import" PlaceholderGenesisTypesDefault = "// this line is used by starport scaffolding # genesis/types/default" PlaceholderGenesisTypesValidate = "// this line is used by starport scaffolding # genesis/types/validate" PlaceholderGenesisModuleInit = "// this line is used by starport scaffolding # genesis/module/init" diff --git a/ignite/templates/typed/singleton/singleton.go b/ignite/templates/typed/singleton/singleton.go index c2cf350ec5..131a0930a9 100644 --- a/ignite/templates/typed/singleton/singleton.go +++ b/ignite/templates/typed/singleton/singleton.go @@ -234,8 +234,6 @@ func genesisTypesModify(replacer placeholder.Replacer, opts *typed.Options) genn return err } - content := typed.PatchGenesisTypeImport(replacer, f.String()) - templateTypesDefault := `%[2]v: nil, %[1]v` replacementTypesDefault := fmt.Sprintf( @@ -243,7 +241,7 @@ func genesisTypesModify(replacer placeholder.Replacer, opts *typed.Options) genn typed.PlaceholderGenesisTypesDefault, opts.TypeName.UpperCamel, ) - content = replacer.Replace(content, typed.PlaceholderGenesisTypesDefault, replacementTypesDefault) + content := replacer.Replace(f.String(), typed.PlaceholderGenesisTypesDefault, replacementTypesDefault) newFile := genny.NewFileS(path, content) return r.File(newFile)