Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add or vendor proto packages from Go dependencies #3724

Merged
merged 34 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
fbafd40
refactor: change `cosmosgen` proto discovery to add extra paths
jeronimoalbi Oct 30, 2023
b3f89a4
feat(pkg/cosmosbuf): add Buf mod update support
jeronimoalbi Nov 1, 2023
355e804
feat: add third party deps to app's Buf config
jeronimoalbi Nov 1, 2023
61b9144
chore: update changelog
jeronimoalbi Nov 1, 2023
3d4de21
Merge branch 'main' into feat/buf-dependency-improvements
jeronimoalbi Nov 1, 2023
94ab03e
feat: add vendoring support for Go dependencies with proto files
jeronimoalbi Nov 1, 2023
07e00ed
refactor(pkg/cosmosgen): use context as function argument
jeronimoalbi Nov 1, 2023
ab40b48
feat: add `--update-buf-module` flag to generate go commands
jeronimoalbi Nov 2, 2023
65f3983
feat: change scaffolder to update Buf with third party dependencies
jeronimoalbi Nov 2, 2023
4416e94
feat: add events support to generator
jeronimoalbi Nov 2, 2023
af6fede
Merge branch 'main' into feat/buf-dependency-improvements
jeronimoalbi Nov 2, 2023
1c97777
chore: remove invalid TODO comments
jeronimoalbi Nov 2, 2023
9120b40
Merge branch 'main' into feat/buf-dependency-improvements
jeronimoalbi Nov 3, 2023
c460bf1
Merge branch 'main' into feat/buf-dependency-improvements
Pantani Nov 6, 2023
a2efaf5
Merge branch 'main' into feat/buf-dependency-improvements
jeronimoalbi Nov 6, 2023
58ff441
Merge branch 'main' into feat/buf-dependency-improvements
Pantani Nov 6, 2023
2294b84
Merge branch 'main' into feat/buf-dependency-improvements
Pantani Nov 6, 2023
1b4ee13
Merge branch 'main' into feat/buf-dependency-improvements
Pantani Nov 7, 2023
10cfe6d
chore: add Buf config error variable
jeronimoalbi Nov 8, 2023
e37aaa0
chore: change `--update-buf-module` flag to be persistent
jeronimoalbi Nov 8, 2023
911c9c2
Merge branch 'main' into feat/buf-dependency-improvements
jeronimoalbi Nov 8, 2023
39d52b0
refactor: enable Buf dep vendoring for generate ts-client command
jeronimoalbi Nov 8, 2023
1246448
refactor: add generate proto vendor target
jeronimoalbi Nov 8, 2023
6d93c26
chore: improve variable usage
jeronimoalbi Nov 8, 2023
d7bf326
Merge branch 'main' into feat/buf-dependency-improvements
Pantani Nov 9, 2023
6fdc289
refactor: rename proto vendoring flag to `--enable-proto-vendor`
jeronimoalbi Nov 9, 2023
3474203
Merge branch 'main' into feat/buf-dependency-improvements
jeronimoalbi Nov 10, 2023
e41c87d
Merge branch 'main' into feat/buf-dependency-improvements
Pantani Nov 11, 2023
3174a3c
Merge branch 'main' into feat/buf-dependency-improvements
Pantani Nov 11, 2023
296f881
Merge branch 'main' into feat/buf-dependency-improvements
jeronimoalbi Nov 13, 2023
d732be0
Merge branch 'main' into feat/buf-dependency-improvements
Pantani Nov 14, 2023
9f61ba6
Merge branch 'main' into feat/buf-dependency-improvements
jeronimoalbi Nov 14, 2023
f1cdd26
Merge branch 'main' into feat/buf-dependency-improvements
jeronimoalbi Nov 14, 2023
327ca6c
Merge branch 'main' into feat/buf-dependency-improvements
Pantani Nov 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- [#3614](https://github.com/ignite/cli/pull/3614) feat: use DefaultBaseappOptions for app.New method
- [#3536](https://github.com/ignite/cli/pull/3536) Change app.go to v2 and add AppWiring feature
- [#3670](https://github.com/ignite/cli/pull/3670) Remove nodetime binaries
- [#3724](https://github.com/ignite/cli/pull/3724) Add or vendor proto packages from Go dependencies
- [#3715](https://github.com/ignite/cli/pull/3715) Add test suite for the cli tests

### Changes
Expand Down
19 changes: 19 additions & 0 deletions ignite/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import (
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
)

const (
flagEnableProtoVendor = "enable-proto-vendor"
)

// NewGenerate returns a command that groups code generation related sub commands.
Expand All @@ -22,8 +27,11 @@
PersistentPreRunE: migrationPreRunHandler,
}

c.PersistentFlags().AddFlagSet(flagSetEnableProtoVendor())

Check warning on line 31 in ignite/cmd/generate.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate.go#L30-L31

Added lines #L30 - L31 were not covered by tests
flagSetPath(c)
flagSetClearCache(c)

Check warning on line 34 in ignite/cmd/generate.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate.go#L34

Added line #L34 was not covered by tests
c.AddCommand(NewGenerateGo())
c.AddCommand(NewGeneratePulsar())
c.AddCommand(NewGenerateTSClient())
Expand All @@ -34,3 +42,14 @@

return c
}

func flagSetEnableProtoVendor() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.Bool(flagEnableProtoVendor, false, "enable proto package vendor for missing Buf dependencies")
return fs

Check warning on line 49 in ignite/cmd/generate.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate.go#L46-L49

Added lines #L46 - L49 were not covered by tests
}

func flagGetEnableProtoVendor(cmd *cobra.Command) bool {
skip, _ := cmd.Flags().GetBool(flagEnableProtoVendor)
return skip

Check warning on line 54 in ignite/cmd/generate.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate.go#L52-L54

Added lines #L52 - L54 were not covered by tests
}
8 changes: 7 additions & 1 deletion ignite/cmd/generate_composables.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@
return err
}

if err := c.Generate(cmd.Context(), cacheStorage, chain.GenerateComposables(output)); err != nil {
var opts []chain.GenerateTarget
if flagGetEnableProtoVendor(cmd) {
opts = append(opts, chain.GenerateProtoVendor())
}

Check warning on line 50 in ignite/cmd/generate_composables.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate_composables.go#L47-L50

Added lines #L47 - L50 were not covered by tests

err = c.Generate(cmd.Context(), cacheStorage, chain.GenerateComposables(output), opts...)
if err != nil {

Check warning on line 53 in ignite/cmd/generate_composables.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate_composables.go#L52-L53

Added lines #L52 - L53 were not covered by tests
return err
}

Expand Down
8 changes: 7 additions & 1 deletion ignite/cmd/generate_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@
return err
}

if err := c.Generate(cmd.Context(), cacheStorage, chain.GenerateGo()); err != nil {
var opts []chain.GenerateTarget
if flagGetEnableProtoVendor(cmd) {
opts = append(opts, chain.GenerateProtoVendor())
}

Check warning on line 45 in ignite/cmd/generate_go.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate_go.go#L42-L45

Added lines #L42 - L45 were not covered by tests

err = c.Generate(cmd.Context(), cacheStorage, chain.GenerateGo(), opts...)
if err != nil {

Check warning on line 48 in ignite/cmd/generate_go.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate_go.go#L47-L48

Added lines #L47 - L48 were not covered by tests
return err
}

Expand Down
8 changes: 7 additions & 1 deletion ignite/cmd/generate_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@
return err
}

if err := c.Generate(cmd.Context(), cacheStorage, chain.GenerateHooks(output)); err != nil {
var opts []chain.GenerateTarget
if flagGetEnableProtoVendor(cmd) {
opts = append(opts, chain.GenerateProtoVendor())
}

Check warning on line 50 in ignite/cmd/generate_hooks.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate_hooks.go#L47-L50

Added lines #L47 - L50 were not covered by tests

err = c.Generate(cmd.Context(), cacheStorage, chain.GenerateHooks(output), opts...)
if err != nil {

Check warning on line 53 in ignite/cmd/generate_hooks.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate_hooks.go#L52-L53

Added lines #L52 - L53 were not covered by tests
return err
}

Expand Down
8 changes: 7 additions & 1 deletion ignite/cmd/generate_openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@
return err
}

if err := c.Generate(cmd.Context(), cacheStorage, chain.GenerateOpenAPI()); err != nil {
var opts []chain.GenerateTarget
if flagGetEnableProtoVendor(cmd) {
opts = append(opts, chain.GenerateProtoVendor())
}

Check warning on line 45 in ignite/cmd/generate_openapi.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate_openapi.go#L42-L45

Added lines #L42 - L45 were not covered by tests

err = c.Generate(cmd.Context(), cacheStorage, chain.GenerateOpenAPI(), opts...)
if err != nil {

Check warning on line 48 in ignite/cmd/generate_openapi.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate_openapi.go#L47-L48

Added lines #L47 - L48 were not covered by tests
return err
}

Expand Down
8 changes: 7 additions & 1 deletion ignite/cmd/generate_pulsar.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@
return err
}

if err := c.Generate(cmd.Context(), cacheStorage, chain.GeneratePulsar()); err != nil {
var opts []chain.GenerateTarget
if flagGetEnableProtoVendor(cmd) {
opts = append(opts, chain.GenerateProtoVendor())
}

Check warning on line 45 in ignite/cmd/generate_pulsar.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate_pulsar.go#L42-L45

Added lines #L42 - L45 were not covered by tests

err = c.Generate(cmd.Context(), cacheStorage, chain.GeneratePulsar(), opts...)
if err != nil {

Check warning on line 48 in ignite/cmd/generate_pulsar.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate_pulsar.go#L47-L48

Added lines #L47 - L48 were not covered by tests
return err
}

Expand Down
7 changes: 6 additions & 1 deletion ignite/cmd/generate_typescript_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@
return err
}

err = c.Generate(cmd.Context(), cacheStorage, chain.GenerateTSClient(output, useCache))
var opts []chain.GenerateTarget
if flagGetEnableProtoVendor(cmd) {
opts = append(opts, chain.GenerateProtoVendor())
}

Check warning on line 79 in ignite/cmd/generate_typescript_client.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate_typescript_client.go#L76-L79

Added lines #L76 - L79 were not covered by tests

err = c.Generate(cmd.Context(), cacheStorage, chain.GenerateTSClient(output, useCache), opts...)

Check warning on line 81 in ignite/cmd/generate_typescript_client.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate_typescript_client.go#L81

Added line #L81 was not covered by tests
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion ignite/cmd/generate_vuex.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@
return err
}

if err := c.Generate(cmd.Context(), cacheStorage, chain.GenerateVuex(output)); err != nil {
var opts []chain.GenerateTarget
if flagGetEnableProtoVendor(cmd) {
opts = append(opts, chain.GenerateProtoVendor())
}

Check warning on line 51 in ignite/cmd/generate_vuex.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate_vuex.go#L48-L51

Added lines #L48 - L51 were not covered by tests

err = c.Generate(cmd.Context(), cacheStorage, chain.GenerateVuex(output), opts...)
if err != nil {

Check warning on line 54 in ignite/cmd/generate_vuex.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate_vuex.go#L53-L54

Added lines #L53 - L54 were not covered by tests
return err
}

Expand Down
21 changes: 21 additions & 0 deletions ignite/pkg/cosmosbuf/buf.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,20 @@
flagOutput = "output"
flagErrorFormat = "error-format"
flagLogFormat = "log-format"
flagOnly = "only"
fmtJSON = "json"

// CMDGenerate generate command.
CMDGenerate Command = "generate"
CMDExport Command = "export"
CMDMod Command = "mod"
)

var (
commands = map[Command]struct{}{
CMDGenerate: {},
CMDExport: {},
CMDMod: {},
}

// ErrInvalidCommand indicates an invalid command name.
Expand All @@ -72,6 +75,24 @@
return string(c)
}

// 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, dependencies ...string) error {
var flags map[string]string
if dependencies != nil {
flags = map[string]string{
flagOnly: strings.Join(dependencies, ","),
}
}

Check warning on line 86 in ignite/pkg/cosmosbuf/buf.go

View check run for this annotation

Codecov / codecov/patch

ignite/pkg/cosmosbuf/buf.go#L80-L86

Added lines #L80 - L86 were not covered by tests

cmd, err := b.generateCommand(CMDMod, flags, "update", modDir)
if err != nil {
return err
}

Check warning on line 91 in ignite/pkg/cosmosbuf/buf.go

View check run for this annotation

Codecov / codecov/patch

ignite/pkg/cosmosbuf/buf.go#L88-L91

Added lines #L88 - L91 were not covered by tests

return b.runCommand(ctx, cmd...)

Check warning on line 93 in ignite/pkg/cosmosbuf/buf.go

View check run for this annotation

Codecov / codecov/patch

ignite/pkg/cosmosbuf/buf.go#L93

Added line #L93 was not covered by tests
}

// Export runs the buf Export command for the files in the proto directory.
func (b Buf) Export(ctx context.Context, protoDir, output string) error {
// Check if the proto directory is the Cosmos SDK one
Expand Down
51 changes: 39 additions & 12 deletions ignite/pkg/cosmosgen/cosmosgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
"github.com/ignite/cli/ignite/pkg/cache"
"github.com/ignite/cli/ignite/pkg/cosmosanalysis/module"
"github.com/ignite/cli/ignite/pkg/cosmosbuf"
"github.com/ignite/cli/ignite/pkg/events"
)

// generateOptions used to configure code generation.
type generateOptions struct {
includeDirs []string
useCache bool
includeDirs []string
useCache bool
updateBufModule bool
ev events.Bus

isGoEnabled bool
isPulsarEnabled bool
Expand Down Expand Up @@ -103,9 +106,24 @@
}
}

// UpdateBufModule enables Buf config proto dependencies update.
// This option updates app's Buf config when proto packages or
// Buf modules are found within the Go dependencies.
func UpdateBufModule() Option {
return func(o *generateOptions) {
o.updateBufModule = true
}

Check warning on line 115 in ignite/pkg/cosmosgen/cosmosgen.go

View check run for this annotation

Codecov / codecov/patch

ignite/pkg/cosmosgen/cosmosgen.go#L112-L115

Added lines #L112 - L115 were not covered by tests
}

// CollectEvents sets an event bus for sending generation feedback events.
func CollectEvents(ev events.Bus) Option {
return func(c *generateOptions) {
c.ev = ev
Pantani marked this conversation as resolved.
Show resolved Hide resolved
}

Check warning on line 122 in ignite/pkg/cosmosgen/cosmosgen.go

View check run for this annotation

Codecov / codecov/patch

ignite/pkg/cosmosgen/cosmosgen.go#L119-L122

Added lines #L119 - L122 were not covered by tests
}

// generator generates code for sdk and sdk apps.
type generator struct {
ctx context.Context
buf cosmosbuf.Buf
cacheStorage cache.Storage
appPath string
Expand All @@ -115,9 +133,9 @@
sdkImport string
deps []gomodule.Version
appModules []module.Module
appIncludes []string
appIncludes protoIncludes
thirdModules map[string][]module.Module
thirdModuleIncludes map[string][]string
thirdModuleIncludes map[string]protoIncludes
tmpDirs []string
}

Expand All @@ -139,14 +157,13 @@
defer b.Cleanup()

g := &generator{
ctx: ctx,
buf: b,
appPath: appPath,
protoDir: protoDir,
gomodPath: gomodPath,
opts: &generateOptions{},
thirdModules: make(map[string][]module.Module),
thirdModuleIncludes: make(map[string][]string),
thirdModuleIncludes: make(map[string]protoIncludes),

Check warning on line 166 in ignite/pkg/cosmosgen/cosmosgen.go

View check run for this annotation

Codecov / codecov/patch

ignite/pkg/cosmosgen/cosmosgen.go#L166

Added line #L166 was not covered by tests
cacheStorage: cacheStorage,
}

Expand All @@ -156,31 +173,41 @@
apply(g.opts)
}

if err := g.setup(); err != nil {
if err := g.setup(ctx); err != nil {

Check warning on line 176 in ignite/pkg/cosmosgen/cosmosgen.go

View check run for this annotation

Codecov / codecov/patch

ignite/pkg/cosmosgen/cosmosgen.go#L176

Added line #L176 was not covered by tests
return err
}

// Update app's Buf config for third party discovered proto modules.
// Go dependency packages might contain proto files which could also
// optionally be using Buf, so for those cases the discovered proto
// files should be available before code generation.
if g.opts.updateBufModule {
if err := g.updateBufModule(ctx); err != nil {
return err
}

Check warning on line 187 in ignite/pkg/cosmosgen/cosmosgen.go

View check run for this annotation

Codecov / codecov/patch

ignite/pkg/cosmosgen/cosmosgen.go#L184-L187

Added lines #L184 - L187 were not covered by tests
}

// Go generation must run first so the types are created before other
// generated code that requires sdk.Msg implementations to be defined
if g.opts.isGoEnabled {
if err := g.generateGo(); err != nil {
if err := g.generateGo(ctx); err != nil {

Check warning on line 193 in ignite/pkg/cosmosgen/cosmosgen.go

View check run for this annotation

Codecov / codecov/patch

ignite/pkg/cosmosgen/cosmosgen.go#L193

Added line #L193 was not covered by tests
return err
}
}
if g.opts.isPulsarEnabled {
if err := g.generatePulsar(); err != nil {
if err := g.generatePulsar(ctx); err != nil {

Check warning on line 198 in ignite/pkg/cosmosgen/cosmosgen.go

View check run for this annotation

Codecov / codecov/patch

ignite/pkg/cosmosgen/cosmosgen.go#L198

Added line #L198 was not covered by tests
return err
}
}

if g.opts.specOut != "" {
if err := g.generateOpenAPISpec(); err != nil {
if err := g.generateOpenAPISpec(ctx); err != nil {

Check warning on line 204 in ignite/pkg/cosmosgen/cosmosgen.go

View check run for this annotation

Codecov / codecov/patch

ignite/pkg/cosmosgen/cosmosgen.go#L204

Added line #L204 was not covered by tests
return err
}
}

if g.opts.jsOut != nil {
if err := g.generateTS(); err != nil {
if err := g.generateTS(ctx); err != nil {

Check warning on line 210 in ignite/pkg/cosmosgen/cosmosgen.go

View check run for this annotation

Codecov / codecov/patch

ignite/pkg/cosmosgen/cosmosgen.go#L210

Added line #L210 was not covered by tests
return err
}
}
Expand Down
Loading
Loading