Skip to content

Commit

Permalink
change buf mod update to comand to buf dep update
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani authored and Pantani committed May 22, 2024
1 parent ec78d89 commit aa62b83
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 24 deletions.
51 changes: 38 additions & 13 deletions ignite/pkg/cosmosbuf/buf.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ type (

// genOptions used to configure code generation.
genOptions struct {
excluded []glob.Glob
fileByFile bool
flags map[string]string
excluded []glob.Glob
flags map[string]string
fileByFile bool
includeImports bool
includeWKT bool
}

// GenOption configures code generation.
Expand All @@ -72,9 +74,11 @@ type (

func newGenOptions() genOptions {
return genOptions{
flags: make(map[string]string),
excluded: make([]glob.Glob, 0),
fileByFile: false,
flags: make(map[string]string),
excluded: make([]glob.Glob, 0),
fileByFile: false,
includeWKT: false,
includeImports: false,
}
}

Expand All @@ -94,6 +98,22 @@ func ExcludeFiles(patterns ...string) GenOption {
}
}

// IncludeImports also generate all imports except for Well-Known Types.
func IncludeImports() GenOption {
return func(o *genOptions) {
o.includeImports = true
}
}

// IncludeWKT also generate Well-Known Types.
// Cannot be set without IncludeImports.
func IncludeWKT() GenOption {
return func(o *genOptions) {
o.includeImports = true
o.includeWKT = true
}
}

// FileByFile runs the generate command for each proto file.
func FileByFile() GenOption {
return func(o *genOptions) {
Expand Down Expand Up @@ -125,7 +145,8 @@ func (c Command) String() string {
return string(c)
}

// Update updates module dependencies. By default updates all dependencies.
// Update updates module dependencies.
// By default updates all dependencies unless one or more dependencies are specified.
func (b Buf) Update(ctx context.Context, modDir string) error {
cmd, err := b.command(CMDDep, nil, "update", modDir)
if err != nil {
Expand Down Expand Up @@ -202,16 +223,20 @@ func (b Buf) Generate(
}

flags := map[string]string{
flagTemplate: template,
flagOutput: output,
flagErrorFormat: fmtJSON,
flagLogFormat: fmtJSON,
flagIncludeImports: "true",
flagIncludeWellKnownTypes: "true",
flagTemplate: template,
flagOutput: output,
flagErrorFormat: fmtJSON,
flagLogFormat: fmtJSON,
}
for k, v := range opts.flags {
flags[k] = v
}
if opts.includeImports {
flags[flagIncludeImports] = "true"
}
if opts.includeWKT {
flags[flagIncludeWellKnownTypes] = "true"
}

if !opts.fileByFile {
cmd, err := b.command(CMDGenerate, flags, protoPath)
Expand Down
13 changes: 7 additions & 6 deletions ignite/pkg/cosmosgen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func (g generator) updateBufModule(ctx context.Context) error {
// When a Buf config with name is available add it to app's dependencies
// or otherwise export the proto files to a vendor directory.
if includes.BufPath != "" {
if err := g.resolveBufDependency(ctx, pkgName, includes.BufPath); err != nil {
if err := g.resolveBufDependency(pkgName, includes.BufPath); err != nil {
return err
}
} else {
Expand All @@ -335,10 +335,11 @@ func (g generator) updateBufModule(ctx context.Context) error {
}
}
}
return nil
path := g.appIncludes.BufPath
return g.buf.Update(ctx, filepath.Dir(path))
}

func (g generator) resolveBufDependency(ctx context.Context, pkgName, bufPath string) error {
func (g generator) resolveBufDependency(pkgName, bufPath string) error {
// Open the dependency Buf config to find the BSR package name
f, err := os.Open(bufPath)
if err != nil {
Expand All @@ -357,13 +358,13 @@ func (g generator) resolveBufDependency(ctx context.Context, pkgName, bufPath st
// When dependency package has a Buf config name try to add it to app's
// dependencies. Name is optional and defines the BSR package name.
if cfg.Name != "" {
return g.addBufDependency(ctx, cfg.Name)
return g.addBufDependency(cfg.Name)
}
// By default just vendor the proto package
return g.vendorProtoPackage(pkgName, filepath.Dir(bufPath))
}

func (g generator) addBufDependency(ctx context.Context, depName string) error {
func (g generator) addBufDependency(depName string) error {
// Read app's Buf config
path := g.appIncludes.BufPath
bz, err := os.ReadFile(path)
Expand Down Expand Up @@ -410,7 +411,7 @@ func (g generator) addBufDependency(ctx context.Context, depName string) error {
)

// Update Buf lock so it contains the new dependency
return g.buf.Update(ctx, filepath.Dir(path))
return nil
}

func (g generator) vendorProtoPackage(pkgName, protoPath string) (err error) {
Expand Down
8 changes: 7 additions & 1 deletion ignite/pkg/cosmosgen/generate_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ func (g *generator) generate(ctx context.Context, template, fromPath string, exc
defer os.RemoveAll(tmp)

// code generate for each module.
if err := g.buf.Generate(ctx, g.protoPath(), tmp, template, cosmosbuf.ExcludeFiles(excluded...)); err != nil {
if err := g.buf.Generate(
ctx,
g.protoPath(),
tmp,
template,
cosmosbuf.ExcludeFiles(excluded...),
); err != nil {
return err
}

Expand Down
1 change: 1 addition & 0 deletions ignite/pkg/cosmosgen/generate_typescript.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func (g *tsGenerator) generateModuleTemplate(
typesOut,
g.g.tsTemplate(),
cosmosbuf.ExcludeFiles("module.proto"),
cosmosbuf.IncludeWKT(),
); err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions ignite/templates/app/files/{{protoDir}}/buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,29 @@ deps:
owner: cosmos
repository: cosmos-proto
commit: 04467658e59e44bbb22fe568206e1f70
digest: shake256:73a640bd60e0c523b0f8237ff34eab67c45a38b64bbbde1d80224819d272dbf316ac183526bd245f994af6608b025f5130483d0133c5edd385531326b5990466
- remote: buf.build
owner: cosmos
repository: cosmos-sdk
commit: 05419252bcc241ea8023acf1ed4cadc5
digest: shake256:1e54a48c19a8b59d35e0a7efa76402939f515f2d8005df099856f24c37c20a52800308f025abb8cffcd014d437b49707388aaca4865d9d063d8f25d5d4eb77d5
- remote: buf.build
owner: cosmos
repository: gogo-proto
commit: 88ef6483f90f478fb938c37dde52ece3
digest: shake256:89c45df2aa11e0cff97b0d695436713db3d993d76792e9f8dc1ae90e6ab9a9bec55503d48ceedd6b86069ab07d3041b32001b2bfe0227fa725dd515ff381e5ba
- remote: buf.build
owner: cosmos
repository: ics23
commit: a9ee7c290ef34ee69d3f141b9b44dcee
digest: shake256:255dbee3e92a370723bf4d72b34868b18e7570543f30f79c0c8c10a5a332d230175e0c29cb7ebcb8020706312e3cd37c23974df0bacfb60a4afb968fee4c1afc
- remote: buf.build
owner: googleapis
repository: googleapis
commit: 09703837a2ed48dbbbb3fdfbe6a84f5c
digest: shake256:de26a277fc28b8b411ecf58729d78d32fcf15090ffd998a4469225b17889bfb51442eaab04bb7a8d88d203ecdf0a9febd4ffd52c18ed1c2229160c7bd353ca95
- remote: buf.build
owner: protocolbuffers
repository: wellknowntypes
commit: 3186086b2a8e44d9acdeeef2423c5de7
digest: shake256:3b9dc2f56d9ed2e4001f95b701985fd803f7e2559b19b6a18d5f4e792cfdde320e765638de69fff037edc202b0006532d7ff19eab9465526b5ec628e4a5e5a1a
8 changes: 4 additions & 4 deletions integration/cosmosgen/cosmosgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestCosmosGenScaffold(t *testing.T) {
tsDirGenerated := filepath.Join(app.SourcePath(), "ts-client")
require.NoError(t, os.RemoveAll(tsDirGenerated))

env.Must(env.Exec("generate vue and typescript",
env.Must(env.Exec("generate typescript",
step.NewSteps(step.New(
step.Exec(
envtest.IgniteApp,
Expand Down Expand Up @@ -135,9 +135,9 @@ func TestCosmosGenScaffold(t *testing.T) {
"cosmos.upgrade.v1beta1",
"cosmos.vesting.v1beta1",
// custom modules
"test.blog.blog",
"test.blog.withmsg",
"test.blog.withoutmsg",
"blog.blog.v1",
"blog.withmsg.v1",
"blog.withoutmsg.v1",
}

for _, mod := range expectedModules {
Expand Down

0 comments on commit aa62b83

Please sign in to comment.