Skip to content

Commit

Permalink
Merge branch 'main' into feat/plugin-system-improvements-versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani authored Oct 24, 2023
2 parents e8cb887 + f643ccd commit e55b4c9
Show file tree
Hide file tree
Showing 22 changed files with 820 additions and 404 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- [#3610](https://github.com/ignite/cli/pull/3610) Fix overflow issue of cosmos faucet in `pkg/cosmosfaucet/transfer.go` and `pkg/cosmosfaucet/cosmosfaucet.go`
- [#3618](https://github.com/ignite/cli/pull/3618) Fix TS client generation import path issue
- [#3631](https://github.com/ignite/cli/pull/3631) Fix unnecessary vue import in hooks/composables template
- [#3655](https://github.com/ignite/cli/pull/3655) Re-enable TS client generation
- [#3661](https://github.com/ignite/cli/pull/3661) Change `pkg/cosmosanalysis` to find Cosmos SDK runtime app registered modules

## [`v0.27.0`](https://github.com/ignite/cli/releases/tag/v0.27.0)
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/ignite/cli

go 1.21
go 1.21.1

toolchain go1.21.3

require (
cosmossdk.io/math v1.0.1
Expand Down Expand Up @@ -38,7 +40,8 @@ require (
github.com/hashicorp/go-hclog v1.2.0
github.com/hashicorp/go-plugin v1.4.9
github.com/iancoleman/strcase v0.2.0
github.com/ignite/ignite-files/nodetime v0.0.1
github.com/ignite/ignite-files/nodetime v0.0.2
github.com/ignite/ignite-files/protoc v0.0.1
github.com/ignite/web v0.4.3
github.com/imdario/mergo v0.3.15
github.com/jpillora/chisel v1.8.1
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,10 @@ github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHL
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ignite/ignite-files/nodetime v0.0.1 h1:GwsUI+hYOwT5w0GayHSJlOVoDJDhg7RVMrl4oV7VEbc=
github.com/ignite/ignite-files/nodetime v0.0.1/go.mod h1:oMsEk/+FHcHKNiCedFRbLmSAnb6zs3n23KbedSb09LE=
github.com/ignite/ignite-files/nodetime v0.0.2 h1:9Aj0OEa7FWI22J/Zdq7M2JvsjgFLngZSm7vB4i9X4t4=
github.com/ignite/ignite-files/nodetime v0.0.2/go.mod h1:GKDsXdeazHyhSBPdVLp7mNIo/m9LmZ6/h8RmQ0/CoaM=
github.com/ignite/ignite-files/protoc v0.0.1 h1:wXxU1dzruUgSVl1diAuAOA+xv0NQKXJFsDWht2+tAP8=
github.com/ignite/ignite-files/protoc v0.0.1/go.mod h1:cVCHJbEHPIeKHMPk3ZoPS0Xw4XQfUc76BAMAPU9Fwjg=
github.com/ignite/web v0.4.3 h1:LHucUEXttzCf5JmxO5/xLKVx1Qz1o124HqF4Nii5uWQ=
github.com/ignite/web v0.4.3/go.mod h1:WZWBaBYF8RazN7dE462BLpvXDY8ScacxcJ07BKwX/jY=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
Expand Down
98 changes: 81 additions & 17 deletions ignite/pkg/cosmosbuf/buf.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ type (

// Buf represents the buf application structure.
Buf struct {
path string
sdkCache string
cache *protoanalysis.Cache
path string
sdkProtoDir string
cache *protoanalysis.Cache
}
)

Expand All @@ -39,15 +39,20 @@ const (

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

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

// ErrInvalidCommand error invalid command name.
// ErrInvalidCommand indicates an invalid command name.
ErrInvalidCommand = errors.New("invalid command name")

// ErrProtoFilesNotFound indicates that no ".proto" files were found.
ErrProtoFilesNotFound = errors.New("no proto files found")
)

// New creates a new Buf based on the installed binary.
Expand All @@ -67,6 +72,51 @@ func (c Command) String() string {
return string(c)
}

// 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
if strings.Contains(protoDir, cosmosver.CosmosModulePath) {
if b.sdkProtoDir == "" {
// Copy Cosmos SDK proto path without the Buf workspace.
// This is done because the workspace contains a reference to
// a "orm/internal" proto folder that is not present by default
// in the SDK repository.
d, err := copySDKProtoDir(protoDir)
if err != nil {
return err
}

b.sdkProtoDir = d
}

// Split absolute path into an absolute prefix and a relative suffix
paths := strings.Split(protoDir, "/proto")
if len(paths) < 2 {
return fmt.Errorf("invalid Cosmos SDK mod path: %s", protoDir)
}

// Use the SDK copy to resolve SDK proto files
protoDir = filepath.Join(b.sdkProtoDir, paths[1])
}
specs, err := xos.FindFiles(protoDir, xos.ProtoFile)
if err != nil {
return err
}
if len(specs) == 0 {
return fmt.Errorf("%w: %s", ErrProtoFilesNotFound, protoDir)
}
flags := map[string]string{
flagOutput: output,
}

cmd, err := b.generateCommand(CMDExport, flags, protoDir)
if err != nil {
return err
}

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

// Generate runs the buf Generate command for each file into the proto directory.
func (b Buf) Generate(
ctx context.Context,
Expand Down Expand Up @@ -94,17 +144,17 @@ func (b Buf) Generate(
// change the workspace copying the files to another folder and generate the
// files.
if strings.Contains(protoDir, cosmosver.CosmosModulePath) {
if b.sdkCache == "" {
b.sdkCache, err = prepareSDK(protoDir)
if b.sdkProtoDir == "" {
b.sdkProtoDir, err = copySDKProtoDir(protoDir)
if err != nil {
return err
}
}
dirs := strings.Split(protoDir, "/proto/")
if len(dirs) < 2 {
return fmt.Errorf("invalid cosmos sdk mod path: %s", dirs)
return fmt.Errorf("invalid Cosmos SDK mod path: %s", dirs)
}
protoDir = filepath.Join(b.sdkCache, dirs[1])
protoDir = filepath.Join(b.sdkProtoDir, dirs[1])
}

pkgs, err := protoanalysis.Parse(ctx, b.cache, protoDir)
Expand All @@ -118,14 +168,20 @@ func (b Buf) Generate(
if _, ok := excluded[filepath.Base(file.Path)]; ok {
continue
}
cmd, err := b.generateCommand(
CMDGenerate,
flags,
file.Path,
)

specs, err := xos.FindFiles(protoDir, "proto")
if err != nil {
return err
}
if len(specs) == 0 {
continue
}

cmd, err := b.generateCommand(CMDGenerate, flags, file.Path)
if err != nil {
return err
}

g.Go(func() error {
cmd := cmd
return b.runCommand(ctx, cmd...)
Expand All @@ -135,6 +191,14 @@ func (b Buf) Generate(
return g.Wait()
}

// Cleanup deletes temporary files and directories.
func (b Buf) Cleanup() error {
if b.sdkProtoDir != "" {
return os.RemoveAll(b.sdkProtoDir)
}
return nil
}

// runCommand run the buf CLI command.
func (b Buf) runCommand(ctx context.Context, cmd ...string) error {
execOpts := []exec.Option{
Expand Down Expand Up @@ -167,7 +231,7 @@ func (b Buf) generateCommand(
return command, nil
}

// findSDKProtoPath find the cosmos-sdk proto folder path.
// findSDKProtoPath finds the Cosmos SDK proto folder path.
func findSDKProtoPath(protoDir string) (string, error) {
paths := strings.Split(protoDir, "@")
if len(paths) < 2 {
Expand All @@ -177,9 +241,9 @@ func findSDKProtoPath(protoDir string) (string, error) {
return fmt.Sprintf("%s@%s/proto", paths[0], version), nil
}

// prepareSDK copy the cosmos sdk proto folder to a temporary directory
// so we can skip the buf workspace.
func prepareSDK(protoDir string) (string, error) {
// copySDKProtoDir copies the Cosmos SDK proto folder to a temporary directory.
// The temporary directory must be removed by the caller.
func copySDKProtoDir(protoDir string) (string, error) {
tmpDir, err := os.MkdirTemp("", "proto-sdk")
if err != nil {
return "", err
Expand Down
3 changes: 1 addition & 2 deletions ignite/pkg/cosmosclient/txservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

abci "github.com/cometbft/cometbft/abci/types"
ctypes "github.com/cometbft/cometbft/rpc/core/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdktypes "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
Expand Down Expand Up @@ -45,7 +44,7 @@ func TestTxServiceBroadcast(t *testing.T) {
}
tests := []struct {
name string
msg sdk.Msg
msg sdktypes.Msg
opts []cosmosclient.Option
expectedResponse *sdktypes.TxResponse
expectedError string
Expand Down
82 changes: 48 additions & 34 deletions ignite/pkg/cosmosgen/cosmosgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cosmosgen

import (
"context"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -36,8 +37,6 @@ type generateOptions struct {
specOut string
}

// TODO add WithInstall.

// ModulePathFunc defines a function type that returns a path based on a Cosmos SDK module.
type ModulePathFunc func(module.Module) string

Expand Down Expand Up @@ -106,17 +105,27 @@ func IncludeDirs(dirs []string) Option {

// generator generates code for sdk and sdk apps.
type generator struct {
ctx context.Context
buf cosmosbuf.Buf
cacheStorage cache.Storage
appPath string
protoDir string
gomodPath string
o *generateOptions
sdkImport string
deps []gomodule.Version
appModules []module.Module
thirdModules map[string][]module.Module // app dependency-modules pair.
ctx context.Context
buf cosmosbuf.Buf
cacheStorage cache.Storage
appPath string
protoDir string
gomodPath string
opts *generateOptions
sdkImport string
deps []gomodule.Version
appModules []module.Module
appIncludes []string
thirdModules map[string][]module.Module
thirdModuleIncludes map[string][]string
tmpDirs []string
}

func (g *generator) cleanup() {
// Remove temporary directories created during generation
for _, path := range g.tmpDirs {
_ = os.RemoveAll(path)
}
}

// Generate generates code from protoDir of an SDK app residing at appPath with given options.
Expand All @@ -127,19 +136,24 @@ func Generate(ctx context.Context, cacheStorage cache.Storage, appPath, protoDir
return err
}

defer b.Cleanup()

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

defer g.cleanup()

for _, apply := range options {
apply(g.o)
apply(g.opts)
}

if err := g.setup(); err != nil {
Expand All @@ -148,24 +162,30 @@ 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.isGoEnabled {
if g.opts.isGoEnabled {
if err := g.generateGo(); err != nil {
return err
}
}
if g.o.isPulsarEnabled {
if g.opts.isPulsarEnabled {
if err := g.generatePulsar(); err != nil {
return err
}
}

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

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

if g.o.vuexOut != nil {
if g.opts.vuexOut != nil {
if err := g.generateVuex(); err != nil {
return err
}
Expand All @@ -186,7 +206,7 @@ func Generate(ctx context.Context, cacheStorage cache.Storage, appPath, protoDir

}

if g.o.composablesRootPath != "" {
if g.opts.composablesRootPath != "" {
if err := g.generateComposables("vue"); err != nil {
return err
}
Expand All @@ -198,7 +218,7 @@ func Generate(ctx context.Context, cacheStorage cache.Storage, appPath, protoDir
return err
}
}
if g.o.hooksRootPath != "" {
if g.opts.hooksRootPath != "" {
if err := g.generateComposables("react"); err != nil {
return err
}
Expand All @@ -211,12 +231,6 @@ func Generate(ctx context.Context, cacheStorage cache.Storage, appPath, protoDir
}
}

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

return nil
}

Expand Down
Loading

0 comments on commit e55b4c9

Please sign in to comment.