Skip to content

Commit

Permalink
refactor!: version scaffolded protos (#4118)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored May 13, 2024
1 parent 8682f3e commit 711cac2
Show file tree
Hide file tree
Showing 42 changed files with 255 additions and 143 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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
3 changes: 2 additions & 1 deletion 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 @@ -146,13 +147,13 @@ func (s Scaffolder) AddMessage(
gens, err = supportMsgServer(
gens,
s.Tracer(),
s.appPath,
&modulecreate.MsgServerOptions{
ModuleName: opts.ModuleName,
ModulePath: opts.ModulePath,
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
7 changes: 3 additions & 4 deletions ignite/services/scaffolder/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import (
func supportMsgServer(
gens []*genny.Generator,
replacer placeholder.Replacer,
appPath string,
opts *modulecreate.MsgServerOptions,
) ([]*genny.Generator, error) {
// Check if convention used
msgServerDefined, err := isMsgServerDefined(appPath, opts.AppName, opts.ProtoDir, opts.ModuleName)
msgServerDefined, err := isMsgServerDefined(opts)
if err != nil {
return nil, err
}
Expand All @@ -37,8 +36,8 @@ func supportMsgServer(

// isMsgServerDefined checks if the module uses the MsgServer convention for transactions
// this is checked by verifying the existence of the tx.proto file.
func isMsgServerDefined(appPath, appName, protoPath, moduleName string) (bool, error) {
txProto, err := filepath.Abs(filepath.Join(appPath, protoPath, appName, moduleName, "tx.proto"))
func isMsgServerDefined(opts *modulecreate.MsgServerOptions) (bool, error) {
txProto, err := filepath.Abs(opts.ProtoFile("tx.proto"))
if err != nil {
return false, err
}
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
4 changes: 3 additions & 1 deletion ignite/services/scaffolder/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,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 @@ -181,12 +182,13 @@ func (s Scaffolder) AddType(
gens, err = supportMsgServer(
gens,
s.runner.Tracer(),
s.appPath,
&modulecreate.MsgServerOptions{
ModuleName: opts.ModuleName,
ModulePath: opts.ModulePath,
AppName: opts.AppName,
AppPath: opts.AppPath,
ProtoDir: opts.ProtoDir,
ProtoVer: opts.ProtoVer,
},
)
if err != nil {
Expand Down
5 changes: 0 additions & 5 deletions ignite/templates/app/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,3 @@ type Options struct {
IsChainMinimal bool
IsConsumerChain bool
}

// Validate that options are usable.
func (opts *Options) Validate() error {
return nil
}
18 changes: 13 additions & 5 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 @@ -248,8 +256,8 @@ func protoModify(opts *PacketOptions) genny.RunFn {
protoImports = append(protoImports, protoutil.NewImport(imp))
}
for _, f := range append(opts.Fields.Custom(), opts.AckFields.Custom()...) {
protopath := fmt.Sprintf("%[1]v/%[2]v/%[3]v.proto", opts.AppName, opts.ModuleName, f)
protoImports = append(protoImports, protoutil.NewImport(protopath))
protoPath := fmt.Sprintf("%[1]v/%[2]v/%[3]v/%[4]v.proto", opts.AppName, opts.ModuleName, opts.ProtoVer, f)
protoImports = append(protoImports, protoutil.NewImport(protoPath))
}
if err := protoutil.AddImports(protoFile, true, protoImports...); err != nil {
return errors.Errorf("failed while adding imports to %s: %w", path, 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 Expand Up @@ -341,7 +349,7 @@ func protoTxModify(opts *PacketOptions) genny.RunFn {
protoImports = append(protoImports, protoutil.NewImport(imp))
}
for _, f := range opts.Fields.Custom() {
protopath := fmt.Sprintf("%[1]v/%[2]v/%[3]v.proto", opts.AppName, opts.ModuleName, f)
protopath := fmt.Sprintf("%[1]v/%[2]v/%[3]v/%[4]v.proto", opts.AppName, opts.ModuleName, opts.ProtoVer, f)
protoImports = append(protoImports, protoutil.NewImport(protopath))
}
if err := protoutil.AddImports(protoFile, true, protoImports...); err != nil {
Expand Down
8 changes: 5 additions & 3 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 Expand Up @@ -155,7 +157,7 @@ func protoTxMessageModify(opts *Options) genny.RunFn {
protoImports = append(protoImports, protoutil.NewImport(imp))
}
for _, f := range append(opts.ResFields.Custom(), opts.Fields.Custom()...) {
protoPath := fmt.Sprintf("%[1]v/%[2]v/%[3]v.proto", opts.AppName, opts.ModuleName, f)
protoPath := fmt.Sprintf("%[1]v/%[2]v/%[3]v/%[4]v.proto", opts.AppName, opts.ModuleName, opts.ProtoVer, f)
protoImports = append(protoImports, protoutil.NewImport(protoPath))
}
if err = protoutil.AddImports(protoFile, true, protoImports...); err != nil {
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)
}
9 changes: 6 additions & 3 deletions ignite/templates/module/create/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,22 @@ 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("apiPath", fmt.Sprintf("/%s/%s/%s", appModulePath, opts.ModuleName, opts.ProtoVer))
ctx.Set("protoPkgName", module.ProtoPackageName(appModulePath, opts.ModuleName, opts.ProtoVer))
ctx.Set("protoModulePkgName", module.ProtoModulePackageName(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 +91,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
3 changes: 2 additions & 1 deletion ignite/templates/module/create/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ 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")
// here we do not use opts.ProtoFile as it will append an extra opts.ProtoVer in the path
path := filepath.Join(opts.AppPath, opts.ProtoDir, opts.AppName, opts.ModuleName, "module", opts.ProtoVer, "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
@@ -1,5 +1,5 @@
syntax = "proto3";
package <%= protoPkgName %>.module;
package <%= protoModulePkgName %>;

import "cosmos/app/v1alpha1/module.proto";

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
Loading

0 comments on commit 711cac2

Please sign in to comment.