Skip to content

Commit

Permalink
create generate proto-pulsar commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani authored and Pantani committed Jul 3, 2023
1 parent 658897e commit 8f56bf3
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 10 deletions.
1 change: 1 addition & 0 deletions ignite/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ meant to be edited by hand.
flagSetPath(c)
flagSetClearCache(c)
c.AddCommand(NewGenerateGo())
c.AddCommand(NewGeneratePulsar())
c.AddCommand(NewGenerateTSClient())
c.AddCommand(NewGenerateVuex())
c.AddCommand(NewGenerateComposables())
Expand Down
47 changes: 47 additions & 0 deletions ignite/cmd/generate_pulsar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ignitecmd

import (
"github.com/spf13/cobra"

"github.com/ignite/cli/ignite/pkg/cliui"
"github.com/ignite/cli/ignite/pkg/cliui/icons"
"github.com/ignite/cli/ignite/services/chain"
)

func NewGeneratePulsar() *cobra.Command {
c := &cobra.Command{
Use: "proto-pulsar",
Short: "Compile protocol buffer files to Go pulsar source code required by Cosmos SDK",
RunE: generatePulsarHandler,
}

c.Flags().AddFlagSet(flagSetYes())

return c
}

func generatePulsarHandler(cmd *cobra.Command, _ []string) error {
session := cliui.New(cliui.StartSpinnerWithText(statusGenerating))
defer session.End()

c, err := newChainWithHomeFlags(
cmd,
chain.WithOutputer(session),
chain.CollectEvents(session.EventBus()),
chain.CheckCosmosSDKVersion(),
)
if err != nil {
return err
}

cacheStorage, err := newCache(cmd)
if err != nil {
return err
}

if err := c.Generate(cmd.Context(), cacheStorage, chain.GeneratePulsar()); err != nil {
return err
}

return session.Println(icons.OK, "Generated Go pulsar code")
}
23 changes: 18 additions & 5 deletions ignite/pkg/cosmosgen/cosmosgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import (
// generateOptions used to configure code generation.
type generateOptions struct {
includeDirs []string
gomodPath string
useCache bool

isGoEnabled bool
isPulsarEnabled bool

jsOut func(module.Module) string
tsClientRootPath string

Expand Down Expand Up @@ -74,9 +76,16 @@ func WithHooksGeneration(out ModulePathFunc, hooksRootPath string) Option {
}

// WithGoGeneration adds Go code generation.
func WithGoGeneration(gomodPath string) Option {
func WithGoGeneration() Option {
return func(o *generateOptions) {
o.isGoEnabled = true
}
}

// WithPulsarGeneration adds Go pulsar code generation.
func WithPulsarGeneration() Option {
return func(o *generateOptions) {
o.gomodPath = gomodPath
o.isPulsarEnabled = true
}
}

Expand All @@ -102,6 +111,7 @@ type generator struct {
cacheStorage cache.Storage
appPath string
protoDir string
gomodPath string
o *generateOptions
sdkImport string
deps []gomodule.Version
Expand All @@ -111,7 +121,7 @@ type generator struct {

// Generate generates code from protoDir of an SDK app residing at appPath with given options.
// protoDir must be relative to the projectPath.
func Generate(ctx context.Context, cacheStorage cache.Storage, appPath, protoDir string, options ...Option) error {
func Generate(ctx context.Context, cacheStorage cache.Storage, appPath, protoDir, gomodPath string, options ...Option) error {
b, err := cosmosbuf.New()
if err != nil {
return err
Expand All @@ -122,6 +132,7 @@ func Generate(ctx context.Context, cacheStorage cache.Storage, appPath, protoDir
buf: b,
appPath: appPath,
protoDir: protoDir,
gomodPath: gomodPath,
o: &generateOptions{},
thirdModules: make(map[string][]module.Module),
cacheStorage: cacheStorage,
Expand All @@ -137,10 +148,12 @@ func Generate(ctx context.Context, cacheStorage cache.Storage, appPath, protoDir

// Go generation must run first so the types are created before other
// generated code that requires sdk.Msg implementations to be defined
if g.o.gomodPath != "" {
if g.o.isGoEnabled {
if err := g.generateGo(); err != nil {
return err
}
}
if g.o.isPulsarEnabled {
if err := g.generatePulsar(); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion ignite/pkg/cosmosgen/generate_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (g *generator) generateGo() error {
}

// move generated code for the app under the relative locations in its source code.
generatedPath := filepath.Join(tmp, g.o.gomodPath)
generatedPath := filepath.Join(tmp, g.gomodPath)

_, err = os.Stat(generatedPath)
if err == nil {
Expand Down
23 changes: 21 additions & 2 deletions ignite/services/chain/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
type generateOptions struct {
useCache bool
isGoEnabled bool
isPulsarEnabled bool
isTSClientEnabled bool
isComposablesEnabled bool
isHooksEnabled bool
Expand All @@ -38,6 +39,13 @@ func GenerateGo() GenerateTarget {
}
}

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

// 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.
Expand Down Expand Up @@ -149,7 +157,11 @@ func (c *Chain) Generate(
}

if targetOptions.isGoEnabled {
options = append(options, cosmosgen.WithGoGeneration(c.app.ImportPath))
options = append(options, cosmosgen.WithGoGeneration())
}

if targetOptions.isPulsarEnabled {
options = append(options, cosmosgen.WithPulsarGeneration())
}

var (
Expand Down Expand Up @@ -272,7 +284,14 @@ func (c *Chain) Generate(
options = append(options, cosmosgen.WithOpenAPIGeneration(openAPIPath))
}

if err := cosmosgen.Generate(ctx, cacheStorage, c.app.Path, conf.Build.Proto.Path, options...); err != nil {
if err := cosmosgen.Generate(
ctx,
cacheStorage,
c.app.Path,
conf.Build.Proto.Path,
c.app.ImportPath,
options...,
); err != nil {
return &CannotBuildAppError{err}
}

Expand Down
5 changes: 3 additions & 2 deletions ignite/services/scaffolder/scaffolder.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ func protoc(ctx context.Context, cacheStorage cache.Storage, projectPath, gomodP
}

options := []cosmosgen.Option{
cosmosgen.WithGoGeneration(gomodPath),
cosmosgen.WithGoGeneration(),
cosmosgen.WithPulsarGeneration(),
cosmosgen.IncludeDirs(conf.Build.Proto.ThirdPartyPaths),
}

Expand Down Expand Up @@ -133,5 +134,5 @@ func protoc(ctx context.Context, cacheStorage cache.Storage, projectPath, gomodP
options = append(options, cosmosgen.WithOpenAPIGeneration(openAPIPath))
}

return cosmosgen.Generate(ctx, cacheStorage, projectPath, conf.Build.Proto.Path, options...)
return cosmosgen.Generate(ctx, cacheStorage, projectPath, conf.Build.Proto.Path, gomodPath, options...)
}

0 comments on commit 8f56bf3

Please sign in to comment.