Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/auto-migdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani authored and Pantani committed Mar 8, 2024
2 parents 02b507f + 59400a5 commit 31971fd
Show file tree
Hide file tree
Showing 22 changed files with 134 additions and 120 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [#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
- [#3718](https://github.com/ignite/cli/pull/3718) Add `gen-mig-diffs` tool app to compare scaffold output of two versions of ignite
- [#4004](https://github.com/ignite/cli/pull/4004) Remove all import placeholders using the `xast` pkg

### Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
1 change: 0 additions & 1 deletion ignite/pkg/cosmosanalysis/app/testdata/modules/spn/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
35 changes: 34 additions & 1 deletion ignite/pkg/xast/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
38 changes: 19 additions & 19 deletions ignite/pkg/xast/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
args: args{
fileContent: existingContent,
imports: []ImportOptions{
WithImport("strings", "", -1),
WithImport("strings", -1),
},
},
want: `package main
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -109,7 +109,7 @@ import (
st "strings"
)`,
imports: []ImportOptions{
WithImport("strconv", "", 1),
WithImport("strconv", 1),
},
},
want: `package main
Expand All @@ -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
Expand All @@ -155,7 +155,7 @@ import (
args: args{
fileContent: existingContent,
imports: []ImportOptions{
WithImport("fmt", "", -1),
WithLastImport("fmt"),
},
},
want: existingContent + "\n",
Expand All @@ -169,7 +169,7 @@ func main() {
fmt.Println("Hello, world!")
}`,
imports: []ImportOptions{
WithImport("fmt", "", -1),
WithImport("fmt", -1),
},
},
want: `package main
Expand All @@ -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
Expand All @@ -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"),
Expand All @@ -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))"),
Expand All @@ -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'"),
Expand Down
4 changes: 1 addition & 3 deletions ignite/templates/app/files-minimal/app/app.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 0 additions & 2 deletions ignite/templates/app/files-minimal/app/app_config.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 1 addition & 3 deletions ignite/templates/app/files/app/app.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 0 additions & 2 deletions ignite/templates/app/files/app/app_config.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 0 additions & 2 deletions ignite/templates/app/files/app/ibc.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 1 addition & 11 deletions ignite/templates/ibc/placeholders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
1 change: 0 additions & 1 deletion ignite/templates/message/placeholders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
65 changes: 44 additions & 21 deletions ignite/templates/module/create/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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),
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 31971fd

Please sign in to comment.