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 25 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 @@ -8,6 +8,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 @@ package ignitecmd

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

const (
flagUpdateBufModule = "update-buf-module"
)

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

c.PersistentFlags().AddFlagSet(flagSetUpdateBufModule())

flagSetPath(c)
flagSetClearCache(c)

c.AddCommand(NewGenerateGo())
c.AddCommand(NewGeneratePulsar())
c.AddCommand(NewGenerateTSClient())
Expand All @@ -34,3 +42,14 @@ meant to be edited by hand.

return c
}

func flagSetUpdateBufModule() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.Bool(flagUpdateBufModule, false, "update Buf config with missing proto dependencies")
return fs
}

func flagGetUpdateBufModule(cmd *cobra.Command) bool {
skip, _ := cmd.Flags().GetBool(flagUpdateBufModule)
return skip
}
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 @@ func generateComposablesHandler(cmd *cobra.Command, _ []string) error {
return err
}

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

err = c.Generate(cmd.Context(), cacheStorage, chain.GenerateComposables(output), opts...)
if err != nil {
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 @@ func generateGoHandler(cmd *cobra.Command, _ []string) error {
return err
}

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

err = c.Generate(cmd.Context(), cacheStorage, chain.GenerateGo(), opts...)
if err != nil {
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 @@ func generateHooksHandler(cmd *cobra.Command, _ []string) error {
return err
}

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

err = c.Generate(cmd.Context(), cacheStorage, chain.GenerateHooks(output), opts...)
if err != nil {
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 @@ func generateOpenAPIHandler(cmd *cobra.Command, _ []string) error {
return err
}

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

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

Expand Down
9 changes: 8 additions & 1 deletion ignite/cmd/generate_pulsar.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func NewGeneratePulsar() *cobra.Command {
}

c.Flags().AddFlagSet(flagSetYes())
c.Flags().AddFlagSet(flagSetUpdateBufModule())
jeronimoalbi marked this conversation as resolved.
Show resolved Hide resolved

return c
}
Expand All @@ -39,7 +40,13 @@ func generatePulsarHandler(cmd *cobra.Command, _ []string) error {
return err
}

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

err = c.Generate(cmd.Context(), cacheStorage, chain.GeneratePulsar(), opts...)
if err != nil {
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 @@ func generateTSClientHandler(cmd *cobra.Command, _ []string) error {
return err
}

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

err = c.Generate(cmd.Context(), cacheStorage, chain.GenerateTSClient(output, useCache), opts...)
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 @@ func generateVuexHandler(cmd *cobra.Command, _ []string) error {
return err
}

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

err = c.Generate(cmd.Context(), cacheStorage, chain.GenerateVuex(output), opts...)
if err != nil {
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 @@ const (
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 @@ func (c Command) String() string {
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, ","),
}
}

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

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

// 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 @@ import (
"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 @@ func IncludeDirs(dirs []string) Option {
}
}

// 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
}
}

// 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
}
}

// 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 @@ type generator struct {
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 @@ func Generate(ctx context.Context, cacheStorage cache.Storage, appPath, protoDir
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),
cacheStorage: cacheStorage,
}

Expand All @@ -156,31 +173,41 @@ func Generate(ctx context.Context, cacheStorage cache.Storage, appPath, protoDir
apply(g.opts)
}

if err := g.setup(); err != nil {
if err := g.setup(ctx); err != nil {
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
}
}

// 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 {
return err
}
}
if g.opts.isPulsarEnabled {
if err := g.generatePulsar(); err != nil {
if err := g.generatePulsar(ctx); err != nil {
return err
}
}

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

if g.opts.jsOut != nil {
if err := g.generateTS(); err != nil {
if err := g.generateTS(ctx); err != nil {
return err
}
}
Expand Down
Loading