Skip to content

Commit

Permalink
refactor!: version scaffolded protos
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed May 8, 2024
1 parent 53573da commit 40ff9f4
Show file tree
Hide file tree
Showing 37 changed files with 200 additions and 112 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- [#4008](https://github.com/ignite/cli/pull/4008) Rename `pkg/yaml` to `pkg/xyaml`
- [#4075](https://github.com/ignite/cli/pull/4075) Use `gopkg.in/yaml.v3` instead `gopkg.in/yaml.v2`
- [#4103](https://github.com/ignite/cli/pull/4103) Bump cosmos-sdk to `v0.50.6`
- [#4118](https://github.com/ignite/cli/pull/4118) Version scaffolded protos as `v1` to follow SDK structure.

### Fixes

Expand Down
1 change: 1 addition & 0 deletions ignite/services/scaffolder/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (s Scaffolder) CreateConfigs(
AppName: s.modpath.Package,
AppPath: s.appPath,
ProtoDir: s.protoDir,
ProtoVer: "v1", // TODO(@julienrbrt): possibly in the future add flag to specify custom proto version.
}

g, err := modulecreate.NewModuleConfigs(opts)
Expand Down
1 change: 1 addition & 0 deletions ignite/services/scaffolder/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func generate(
AppName: pathInfo.Package,
AppPath: absRoot,
ProtoDir: protoDir,
ProtoVer: "v1", // TODO(@julienrbrt): possibly in the future add flag to specify custom proto version.
Params: paramsFields,
Configs: configsFields,
IsIBC: false,
Expand Down
2 changes: 2 additions & 0 deletions ignite/services/scaffolder/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func (s Scaffolder) AddMessage(
AppName: s.modpath.Package,
AppPath: s.appPath,
ProtoDir: s.protoDir,
ProtoVer: "v1", // TODO(@julienrbrt): possibly in the future add flag to specify custom proto version.
ModulePath: s.modpath.RawPath,
ModuleName: moduleName,
MsgName: name,
Expand All @@ -153,6 +154,7 @@ func (s Scaffolder) AddMessage(
AppName: opts.AppName,
AppPath: opts.AppPath,
ProtoDir: opts.ProtoDir,
ProtoVer: opts.ProtoVer,
},
)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions ignite/services/scaffolder/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func (s Scaffolder) CreateModule(
AppName: s.modpath.Package,
AppPath: s.appPath,
ProtoDir: s.protoDir,
ProtoVer: "v1", // TODO(@julienrbrt): possibly in the future add flag to specify custom proto version.
IsIBC: creationOpts.ibc,
IBCOrdering: creationOpts.ibcChannelOrdering,
Dependencies: creationOpts.dependencies,
Expand Down
1 change: 1 addition & 0 deletions ignite/services/scaffolder/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func (s Scaffolder) AddPacket(
AppName: s.modpath.Package,
AppPath: s.appPath,
ProtoDir: s.protoDir,
ProtoVer: "v1", // TODO(@julienrbrt): possibly in the future add flag to specify custom proto version.
ModulePath: s.modpath.RawPath,
ModuleName: moduleName,
PacketName: name,
Expand Down
1 change: 1 addition & 0 deletions ignite/services/scaffolder/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (s Scaffolder) CreateParams(
AppName: s.modpath.Package,
AppPath: s.appPath,
ProtoDir: s.protoDir,
ProtoVer: "v1", // TODO(@julienrbrt): possibly in the future add flag to specify custom proto version.
}

g, err := modulecreate.NewModuleParam(opts)
Expand Down
1 change: 1 addition & 0 deletions ignite/services/scaffolder/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (s Scaffolder) AddQuery(
AppName: s.modpath.Package,
AppPath: s.appPath,
ProtoDir: s.protoDir,
ProtoVer: "v1", // TODO(@julienrbrt): possibly in the future add flag to specify custom proto version.
ModulePath: s.modpath.RawPath,
ModuleName: moduleName,
QueryName: name,
Expand Down
3 changes: 3 additions & 0 deletions ignite/services/scaffolder/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func (s Scaffolder) AddType(
AppName: s.modpath.Package,
AppPath: s.appPath,
ProtoDir: s.protoDir,
ProtoVer: "v1", // TODO(@julienrbrt): possibly in the future add flag to specify custom proto version.
ModulePath: s.modpath.RawPath,
ModuleName: moduleName,
TypeName: name,
Expand All @@ -188,6 +189,8 @@ func (s Scaffolder) AddType(
ModulePath: opts.ModulePath,
AppName: opts.AppName,
AppPath: opts.AppPath,
ProtoDir: opts.ProtoDir,
ProtoVer: opts.ProtoVer,
},
)
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions ignite/templates/ibc/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type PacketOptions struct {
AppName string
AppPath string
ProtoDir string
ProtoVer string
ModuleName string
ModulePath string
PacketName multiformatname.Name
Expand All @@ -44,6 +45,11 @@ type PacketOptions struct {
NoMessage bool
}

// ProtoFile returns the path to the proto folder
func (opts *PacketOptions) ProtoFile(fname string) string {
return filepath.Join(opts.AppPath, opts.ProtoDir, opts.AppName, opts.ModuleName, opts.ProtoVer, fname)
}

// NewPacket returns the generator to scaffold a packet in an IBC module.
func NewPacket(replacer placeholder.Replacer, opts *PacketOptions) (*genny.Generator, error) {
var (
Expand Down Expand Up @@ -83,6 +89,7 @@ func NewPacket(replacer placeholder.Replacer, opts *PacketOptions) (*genny.Gener
ctx.Set("moduleName", opts.ModuleName)
ctx.Set("ModulePath", opts.ModulePath)
ctx.Set("appName", opts.AppName)
ctx.Set("protoVer", opts.ProtoVer)
ctx.Set("packetName", opts.PacketName)
ctx.Set("MsgSigner", opts.MsgSigner)
ctx.Set("fields", opts.Fields)
Expand All @@ -93,6 +100,7 @@ func NewPacket(replacer placeholder.Replacer, opts *PacketOptions) (*genny.Gener
g.Transformer(genny.Replace("{{protoDir}}", opts.ProtoDir))
g.Transformer(genny.Replace("{{appName}}", opts.AppName))
g.Transformer(genny.Replace("{{moduleName}}", opts.ModuleName))
g.Transformer(genny.Replace("{{protoVer}}", opts.ProtoVer))
g.Transformer(genny.Replace("{{packetName}}", opts.PacketName.Snake))

// Create the 'testutil' package with the test helpers
Expand Down Expand Up @@ -183,7 +191,7 @@ func moduleModify(replacer placeholder.Replacer, opts *PacketOptions) genny.RunF
// - Existence of a Oneof field named 'packet'.
func protoModify(opts *PacketOptions) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, opts.ProtoDir, opts.AppName, opts.ModuleName, "packet.proto")
path := opts.ProtoFile("packet.proto")
f, err := r.Disk.Find(path)
if err != nil {
return err
Expand Down Expand Up @@ -290,7 +298,7 @@ func eventModify(replacer placeholder.Replacer, opts *PacketOptions) genny.RunFn
// elements in the file.
func protoTxModify(opts *PacketOptions) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, opts.ProtoDir, opts.AppName, opts.ModuleName, "tx.proto")
path := opts.ProtoFile("tx.proto")
f, err := r.Disk.Find(path)
if err != nil {
return err
Expand Down
6 changes: 4 additions & 2 deletions ignite/templates/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func Box(box packd.Walker, opts *Options, g *genny.Generator) error {
}
ctx := plush.NewContext()
ctx.Set("ModuleName", opts.ModuleName)
ctx.Set("ProtoVer", opts.ProtoVer)
ctx.Set("AppName", opts.AppName)
ctx.Set("MsgName", opts.MsgName)
ctx.Set("MsgDesc", opts.MsgDesc)
Expand All @@ -47,6 +48,7 @@ func Box(box packd.Walker, opts *Options, g *genny.Generator) error {
g.Transformer(genny.Replace("{{protoDir}}", opts.ProtoDir))
g.Transformer(genny.Replace("{{appName}}", opts.AppName))
g.Transformer(genny.Replace("{{moduleName}}", opts.ModuleName))
g.Transformer(genny.Replace("{{protoVer}}", opts.ProtoVer))
g.Transformer(genny.Replace("{{msgName}}", opts.MsgName.Snake))

// Create the 'testutil' package with the test helpers
Expand Down Expand Up @@ -88,7 +90,7 @@ func NewGenerator(replacer placeholder.Replacer, opts *Options) (*genny.Generato
// - A service named "Msg" to exist in the proto file, it appends the RPCs inside it.
func protoTxRPCModify(opts *Options) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, opts.ProtoDir, opts.AppName, opts.ModuleName, "tx.proto")
path := opts.ProtoFile("tx.proto")
f, err := r.Disk.Find(path)
if err != nil {
return err
Expand Down Expand Up @@ -119,7 +121,7 @@ func protoTxRPCModify(opts *Options) genny.RunFn {

func protoTxMessageModify(opts *Options) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, opts.ProtoDir, opts.AppName, opts.ModuleName, "tx.proto")
path := opts.ProtoFile("tx.proto")
f, err := r.Disk.Find(path)
if err != nil {
return err
Expand Down
9 changes: 6 additions & 3 deletions ignite/templates/message/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package message

import (
"path/filepath"

"github.com/ignite/cli/v29/ignite/pkg/multiformatname"
"github.com/ignite/cli/v29/ignite/templates/field"
)
Expand All @@ -10,6 +12,7 @@ type Options struct {
AppName string
AppPath string
ProtoDir string
ProtoVer string
ModuleName string
ModulePath string
MsgName multiformatname.Name
Expand All @@ -20,7 +23,7 @@ type Options struct {
NoSimulation bool
}

// Validate that options are usable.
func (opts *Options) Validate() error {
return nil
// ProtoFile returns the path to the proto folder
func (opts *Options) ProtoFile(fname string) string {
return filepath.Join(opts.AppPath, opts.ProtoDir, opts.AppName, opts.ModuleName, opts.ProtoVer, fname)
}
6 changes: 4 additions & 2 deletions ignite/templates/module/create/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,21 @@ func NewGenerator(opts *CreateOptions) (*genny.Generator, error) {
ctx.Set("moduleName", opts.ModuleName)
ctx.Set("modulePath", opts.ModulePath)
ctx.Set("appName", opts.AppName)
ctx.Set("protoVer", opts.ProtoVer)
ctx.Set("dependencies", opts.Dependencies)
ctx.Set("params", opts.Params)
ctx.Set("configs", opts.Configs)
ctx.Set("isIBC", opts.IsIBC)
ctx.Set("apiPath", fmt.Sprintf("/%s/%s", appModulePath, opts.ModuleName))
ctx.Set("protoPkgName", module.ProtoPackageName(appModulePath, opts.ModuleName))
ctx.Set("protoPkgName", module.ProtoPackageName(appModulePath, opts.ModuleName, opts.ProtoVer))
ctx.Set("toVariableName", strcase.ToLowerCamel)

plushhelpers.ExtendPlushContext(ctx)
g.Transformer(xgenny.Transformer(ctx))
g.Transformer(genny.Replace("{{protoDir}}", opts.ProtoDir))
g.Transformer(genny.Replace("{{appName}}", opts.AppName))
g.Transformer(genny.Replace("{{moduleName}}", opts.ModuleName))
g.Transformer(genny.Replace("{{protoVer}}", opts.ProtoVer))

return g, nil
}
Expand Down Expand Up @@ -88,7 +90,7 @@ func appConfigModify(replacer placeholder.Replacer, opts *CreateOptions) genny.R
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),
fmt.Sprintf("%[1]v/api/%[2]v/%[3]v/module/%[4]v", opts.ModulePath, opts.AppName, opts.ModuleName, opts.ProtoVer),
),
xast.WithLastNamedImport(
"_",
Expand Down
4 changes: 1 addition & 3 deletions ignite/templates/module/create/configs.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package modulecreate

import (
"path/filepath"

"github.com/gobuffalo/genny/v2"

"github.com/ignite/cli/v29/ignite/pkg/errors"
Expand All @@ -18,7 +16,7 @@ func NewModuleConfigs(opts ConfigsOptions) (*genny.Generator, error) {

func configsProtoModify(opts ConfigsOptions) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, opts.ProtoDir, opts.AppName, opts.ModuleName, "module/module.proto")
path := opts.ProtoFile("module/module.proto")
f, err := r.Disk.Find(path)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package <%= moduleName %>
import (
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"

modulev1 "<%= modulePath %>/api/<%= appName %>/<%= moduleName %>"
modulev1 "<%= modulePath %>/api/<%= appName %>/<%= moduleName %>/<%= protoVer %>"
)

// AutoCLIOptions implements the autocli.HasAutoCLIConfig interface.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

// this line is used by starport scaffolding # 1

modulev1 "<%= modulePath %>/api/<%= appName %>/<%= moduleName %>/module"
modulev1 "<%= modulePath %>/api/<%= appName %>/<%= moduleName %>/module/<%= protoVer %>"
"<%= modulePath %>/x/<%= moduleName %>/keeper"
"<%= modulePath %>/x/<%= moduleName %>/types"
<%= if (isIBC) { %>"<%= modulePath %>/x/<%= moduleName %>/client/cli"<% } %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package <%= protoPkgName %>;

import "amino/amino.proto";
import "gogoproto/gogo.proto";
import "<%= appName %>/<%= moduleName %>/params.proto";
import "<%= appName %>/<%= moduleName %>/<%= protoVer %>/params.proto";

option go_package = "<%= modulePath %>/x/<%= moduleName %>/types";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "amino/amino.proto";
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "<%= appName %>/<%= moduleName %>/params.proto";
import "<%= appName %>/<%= moduleName %>/<%= protoVer %>/params.proto";

option go_package = "<%= modulePath %>/x/<%= moduleName %>/types";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "amino/amino.proto";
import "cosmos/msg/v1/msg.proto";
import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
import "<%= appName %>/<%= moduleName %>/params.proto";
import "<%= appName %>/<%= moduleName %>/<%= protoVer %>/params.proto";

option go_package = "<%= modulePath %>/x/<%= moduleName %>/types";

Expand Down
7 changes: 5 additions & 2 deletions ignite/templates/module/create/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ func NewIBC(replacer placeholder.Replacer, opts *CreateOptions) (*genny.Generato
ctx.Set("moduleName", opts.ModuleName)
ctx.Set("modulePath", opts.ModulePath)
ctx.Set("appName", opts.AppName)
ctx.Set("protoVer", opts.ProtoVer)
ctx.Set("ibcOrdering", opts.IBCOrdering)
ctx.Set("dependencies", opts.Dependencies)
ctx.Set("protoPkgName", module.ProtoPackageName(appModulePath, opts.ModuleName))
ctx.Set("protoPkgName", module.ProtoPackageName(appModulePath, opts.ModuleName, opts.ProtoVer))

plushhelpers.ExtendPlushContext(ctx)
g.Transformer(xgenny.Transformer(ctx))
g.Transformer(genny.Replace("{{protoDir}}", opts.ProtoDir))
g.Transformer(genny.Replace("{{appName}}", opts.AppName))
g.Transformer(genny.Replace("{{moduleName}}", opts.ModuleName))
g.Transformer(genny.Replace("{{protoVer}}", opts.ProtoVer))

return g, nil
}

Expand Down Expand Up @@ -131,7 +134,7 @@ func genesisTypesModify(replacer placeholder.Replacer, opts *CreateOptions) genn
// - Existence of a message named 'GenesisState' in genesis.proto.
func genesisProtoModify(opts *CreateOptions) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, opts.ProtoDir, opts.AppName, opts.ModuleName, "genesis.proto")
path := opts.ProtoFile("genesis.proto")
f, err := r.Disk.Find(path)
if err != nil {
return err
Expand Down
5 changes: 4 additions & 1 deletion ignite/templates/module/create/msgserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ func AddMsgServerConventionToLegacyModule(replacer placeholder.Replacer, opts *M
ctx.Set("moduleName", opts.ModuleName)
ctx.Set("modulePath", opts.ModulePath)
ctx.Set("appName", opts.AppName)
ctx.Set("protoPkgName", module.ProtoPackageName(appModulePath, opts.ModuleName))
ctx.Set("protoVer", opts.ProtoVer)
ctx.Set("protoPkgName", module.ProtoPackageName(appModulePath, opts.ModuleName, opts.ProtoVer))

plushhelpers.ExtendPlushContext(ctx)
g.Transformer(xgenny.Transformer(ctx))
g.Transformer(genny.Replace("{{protoDir}}", opts.ProtoDir))
g.Transformer(genny.Replace("{{appName}}", opts.AppName))
g.Transformer(genny.Replace("{{moduleName}}", opts.ModuleName))
g.Transformer(genny.Replace("{{protoVer}}", opts.ProtoVer))

return g, nil
}

Expand Down
Loading

0 comments on commit 40ff9f4

Please sign in to comment.