Skip to content

Commit

Permalink
refactor: add generate proto vendor target
Browse files Browse the repository at this point in the history
All generate commands require support for proto vendoring and Buf
dependencies updates.
A new target was added to make proto vendoring optional and to avoid
using an extra argument in all generate targets.
  • Loading branch information
jeronimoalbi committed Nov 8, 2023
1 parent 39d52b0 commit 1246448
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 24 deletions.
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())
}

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: 6 additions & 2 deletions ignite/cmd/generate_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ func generateGoHandler(cmd *cobra.Command, _ []string) error {
return err
}

updateBufModule := flagGetUpdateBufModule(cmd)
err = c.Generate(cmd.Context(), cacheStorage, chain.GenerateGo(updateBufModule))
var opts []chain.GenerateTarget
if flagGetUpdateBufModule(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 @@ 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())
}

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 @@ 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())
}

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: 6 additions & 2 deletions ignite/cmd/generate_pulsar.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ func generatePulsarHandler(cmd *cobra.Command, _ []string) error {
return err
}

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

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

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate_pulsar.go#L43-L46

Added lines #L43 - L46 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/generate_pulsar.go#L48-L49

Added lines #L48 - L49 were not covered by tests
return err
}
Expand Down
12 changes: 6 additions & 6 deletions ignite/cmd/generate_typescript_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ func generateTSClientHandler(cmd *cobra.Command, _ []string) error {
return err
}

updateBufModule := flagGetUpdateBufModule(cmd)
err = c.Generate(
cmd.Context(),
cacheStorage,
chain.GenerateTSClient(output, useCache, updateBufModule),
)
var opts []chain.GenerateTarget
if flagGetUpdateBufModule(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 @@ 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())
}

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
29 changes: 19 additions & 10 deletions ignite/services/chain/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

type generateOptions struct {
useCache bool
updateBufModule bool
isProtoVendorEnabled bool
isGoEnabled bool
isPulsarEnabled bool
isTSClientEnabled bool
Expand All @@ -34,31 +34,28 @@ type generateOptions struct {
type GenerateTarget func(*generateOptions)

// GenerateGo enables generating proto based Go code needed for the chain's source code.
func GenerateGo(updateBufModule bool) GenerateTarget {
func GenerateGo() GenerateTarget {
return func(o *generateOptions) {
o.isGoEnabled = true
o.updateBufModule = updateBufModule
}
}

// GeneratePulsar enables generating proto based Go code needed for the chain's source code.
func GeneratePulsar(updateBufModule bool) GenerateTarget {
func GeneratePulsar() GenerateTarget {
return func(o *generateOptions) {
o.isPulsarEnabled = true
o.updateBufModule = updateBufModule
}
}

// GenerateTSClient enables generating proto based Typescript Client.
// The path assigns the output path to use for the generated Typescript client
// overriding the configured or default path. Path can be an empty string.
func GenerateTSClient(path string, useCache, updateBufModule bool) GenerateTarget {
func GenerateTSClient(path string, useCache bool) GenerateTarget {
return func(o *generateOptions) {
o.isOpenAPIEnabled = true
o.isTSClientEnabled = true
o.tsClientPath = path
o.useCache = useCache
o.updateBufModule = updateBufModule
}
}

Expand Down Expand Up @@ -99,6 +96,18 @@ func GenerateOpenAPI() GenerateTarget {
}
}

// GenerateProtoVendor enables `proto_vendor` folder generation.
// Proto vendor is generated from Go dependencies that contain proto files that
// are not included in the app's Buf config.
// Enabling proto vendoring might update Buf config with missing dependencies
// if a Go dependency contains proto files and a Buf config with a name that is
// not listed in the Buf dependencies.
func GenerateProtoVendor() GenerateTarget {
return func(o *generateOptions) {
o.isProtoVendorEnabled = true
}

Check warning on line 108 in ignite/services/chain/generate.go

View check run for this annotation

Codecov / codecov/patch

ignite/services/chain/generate.go#L105-L108

Added lines #L105 - L108 were not covered by tests
}

// generateFromConfig makes code generation from proto files from the given config.
func (c *Chain) generateFromConfig(ctx context.Context, cacheStorage cache.Storage, generateClients bool) error {
conf, err := c.Config()
Expand All @@ -115,7 +124,7 @@ func (c *Chain) generateFromConfig(ctx context.Context, cacheStorage cache.Stora

if generateClients {
if p := conf.Client.Typescript.Path; p != "" {
targets = append(targets, GenerateTSClient(p, true, false))
targets = append(targets, GenerateTSClient(p, true))
}

//nolint:staticcheck //ignore SA1019 until vuex config option is removed
Expand All @@ -133,7 +142,7 @@ func (c *Chain) generateFromConfig(ctx context.Context, cacheStorage cache.Stora
}

// Generate proto based code for Go and optionally for any optional targets
return c.Generate(ctx, cacheStorage, GenerateGo(false), targets...)
return c.Generate(ctx, cacheStorage, GenerateGo(), targets...)
}

// Generate makes code generation from proto files for given target and additionalTargets.
Expand Down Expand Up @@ -173,7 +182,7 @@ func (c *Chain) Generate(
options = append(options, cosmosgen.WithPulsarGeneration())
}

if targetOptions.updateBufModule {
if targetOptions.isProtoVendorEnabled {
options = append(options, cosmosgen.UpdateBufModule())
}

Check warning on line 187 in ignite/services/chain/generate.go

View check run for this annotation

Codecov / codecov/patch

ignite/services/chain/generate.go#L185-L187

Added lines #L185 - L187 were not covered by tests

Expand Down

0 comments on commit 1246448

Please sign in to comment.