8000 feat: enable js proto generation by ilgooz · Pull Request #751 · ignite/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: enable js proto generation #751

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

Merged
merged 7 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ require (
github.com/rdegges/go-ipify v0.0.0-20150526035502-2d94a6a86c40
github.com/rogpeppe/go-internal v1.7.0 // indirect
github.com/rs/cors v1.7.0
github.com/sirupsen/logrus v1.7.1 // indirect
github.com/sirupsen/logrus v1.8.0 // indirect
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
github.com/tendermint/spn v0.0.0-20201215081711-b9ec9286ed83
github.com/tendermint/tendermint v0.34.3
golang.org/x/crypto v0.0.0-20210218145215-b8e89b74b9df // indirect
golang.org/x/mod v0.4.1
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
golang.org/x/sys v0.0.0-20210216224549-f992740a1bac // indirect
golang.org/x/sys v0.0.0-20210218155724-8ebf48af031b // indirect
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf // indirect
google.golang.org/grpc v1.35.0
)
Expand Down
11 changes: 6 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,8 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.7.1 h1:rsizeFmZP+GYwyb4V6t6qpG7ZNWzA2bvgW/yC2xHCcg=
github.com/sirupsen/logrus v1.7.1/go.mod h1:4GuYW9TZmE769R5STWrRakJc4UqQ3+QQ95fyz7ENv1A=
github.com/sirupsen/logrus v1.8.0 h1:nfhvjKcUMhBMVqbKHJlk5RPrrfYr/NMo3692g0dwfWU=
github.com/sirupsen/logrus v1.8.0/go.mod h1:4GuYW9TZmE769R5STWrRakJc4UqQ3+QQ95fyz7ENv1A=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
Expand Down Expand Up @@ -868,8 +868,9 @@ golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201117144127-c1f2f97bffc9/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20210218145215-b8e89b74b9df h1:y7QZzfUiTwWam+xBn29Ulb8CBwVN5UdzmMDavl9Whlw=
golang.org/x/crypto v0.0.0-20210218145215-b8e89b74b9df/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -999,8 +1000,8 @@ golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210216224549-f992740a1bac h1:9glrpwtNjBYgRpb67AZJKHfzj1stG/8BL5H7In2oTC4=
golang.org/x/sys v0.0.0-20210216224549-f992740a1bac/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210218155724-8ebf48af031b h1:lAZ0/chPUDWwjqosYR0X4M490zQhMsiJ4K3DbA7o+3g=
golang.org/x/sys v0.0.0-20210218155724-8ebf48af031b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf h1:MZ2shdL+ZM/XzY3ZGOnh4Nlpnxz5GSOhOmtHo3iPU6M=
Expand Down
31 changes: 22 additions & 9 deletions starport/chainconf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ var (

// DefaultConf holds default configuration.
DefaultConf = Config{
Servers: Servers{
RPCAddr: "0.0.0.0:26657",
P2PAddr: "0.0.0.0:26656",
ProfAddr: "0.0.0.0:6060",
GRPCAddr: "0.0.0.0:9090",
APIAddr: "0.0.0.0:1317",
FrontendAddr: "0.0.0.0:8080",
DevUIAddr: "0.0.0.0:12345",
},
Build: Build{
Proto: Proto{
Path: "proto",
Expand All @@ -41,6 +32,15 @@ var (
Faucet: Faucet{
Port: 4500,
},
Servers: Servers{
RPCAddr: "0.0.0.0:26657",
P2PAddr: "0.0.0.0:26656",
ProfAddr: "0.0.0.0:6060",
GRPCAddr: "0.0.0.0:9090",
APIAddr: "0.0.0.0:1317",
FrontendAddr: "0.0.0.0:8080",
DevUIAddr: "0.0.0.0:12345",
},
}
)

Expand All @@ -50,6 +50,7 @@ type Config struct {
Accounts []Account `yaml:"accounts"`
Validator Validator `yaml:"validator"`
Faucet Faucet `yaml:"faucet"`
Client Client `yaml:"client"`
Build Build `yaml:"build"`
Init Init `yaml:"init"`
Genesis map[string]interface{} `yaml:"genesis"`
Expand Down Expand Up @@ -99,6 +100,18 @@ type Proto struct {
ThirdPartyPaths []string `yaml:"third_party_paths"`
}

// Client configures code generation for clients.
type Client struct {
// Vuex configures code generation for Vuex.
Vuex Vuex `yaml:"vuex"`
}

// Vuex configures code generation for Vuex.
type Vuex struct {
// Path configures out location for generated Vuex code.
Path string `yaml:"path"`
}

// Faucet configuration.
type Faucet struct {
// Port number for faucet server to listen at.
Expand Down
185 changes: 185 additions & 0 deletions starport/pkg/cosmosproto/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package cosmosproto

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

"github.com/otiai10/copy"
"github.com/pkg/errors"
"github.com/tendermint/starport/starport/pkg/cmdrunner"
"github.com/tendermint/starport/starport/pkg/cmdrunner/step"
"github.com/tendermint/starport/starport/pkg/gomodule"
"github.com/tendermint/starport/starport/pkg/nodetime/protobufjs"
"github.com/tendermint/starport/starport/pkg/protoanalysis"
"github.com/tendermint/starport/starport/pkg/protoc"
"github.com/tendermint/starport/starport/pkg/protopath"
"golang.org/x/mod/modfile"
)

var (
protocOuts = []string{
"--gocosmos_out=plugins=interfacetype+grpc,Mgoogle/protobuf/any.proto=github.com/cosmos/cosmos-sdk/codec/types:.",
"--grpc-gateway_out=logtostderr=true:.",
}

sdkImport = "github.com/cosmos/cosmos-sdk"
sdkProto = "proto"
sdkProtoThirdParty = "third_party/proto"

fileTypes = "types"
)

type generateOptions struct {
gomodPath string
jsOut func(protoanalysis.Package, string) string
}

// Target adds a new code generation target to Generate.
type Target func(*generateOptions)

// WithJSGeneration adds JS code generation.
func WithJSGeneration(out func(pkg protoanalysis.Package, moduleName string) (path string)) Target {
return func(o *generateOptions) {
o.jsOut = out
}
}

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

// generator generates code for sdk and sdk apps.
type generator struct {
ctx context.Context
projectPath string
protoPath string
includePaths []string
o *generateOptions
modfile *modfile.File
}

// Generate generates code from proto app's proto files.
// make sure that all paths are absolute.
func Generate(
ctx context.Context,
projectPath,
protoPath string,
includePaths []string,
target Target,
otherTargets ...Target,
) error {
g := &generator{
ctx: ctx,
projectPath: projectPath,
protoPath: protoPath,
includePaths: includePaths,
o: &generateOptions{},
}

for _, target := range append(otherTargets, target) {
target(g.o)
}

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

if g.o.jsOut != nil {
if err := g.generateJS(); err != nil {
return err
}
}

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

return nil
}

func (g *generator) setup() (err error) {
// Cosmos SDK hosts proto files of own x/ modules and some third party ones needed by itself and
// blockchain apps. Generate should be aware of these and make them available to the blockchain
// app that wants to generate code for its own proto.
//
// blockchain apps may use different versions of the SDK. following code first makes sure that
// app's dependencies are download by 'go mod' and cached under the local filesystem.
// and then, it determines which version of the SDK is used by the app and what is the absolute path
// of its source code.
if err := cmdrunner.
New(cmdrunner.DefaultWorkdir(g.projectPath)).
Run(g.ctx, step.New(step.Exec("go", "mod", "download"))); err != nil {
return err
}

// parse the go.mod of the app.
g.modfile, err = gomodule.ParseAt(g.projectPath)

return
}

func (g *generator) generateGo() error {
includePaths, err := g.resolveInclude(protopath.NewModule(sdkImport, sdkProto, sdkProtoThirdParty))
if err != nil {
return err
}

// created a temporary dir to locate generated code under which later only some of them will be moved to the
// app's source code. this also prevents having leftover files in the app's source code or its parent dir -when
// command executed directly there- in case of an interrupt.
tmp, err := ioutil.TempDir("", "")
if err != nil {
return err
}
defer os.RemoveAll(tmp)

if err := protoc.Generate(g.ctx, tmp, g.protoPath, append(g.includePaths, includePaths...), protocOuts); err != nil {
return err
}

// move generated code for the app under the relative locations in its source code.
generatedPath := filepath.Join(tmp, g.o.gomodPath)
err = copy.Copy(generatedPath, g.projectPath)
return errors.Wrap(err, "cannot copy path")
}

func (g *generator) generateJS() error {
includePaths, err := g.resolveInclude(protopath.NewModule(sdkImport, sdkProto))
if err != nil {
return err
}

pkgs, err := protoanalysis.DiscoverPackages(g.protoPath)
if err != nil {
return err
}

for _, pkg := range pkgs {
msp := strings.Split(pkg.Name, ".")
moduleName := msp[len(msp)-1]

if err := protobufjs.Generate(
g.ctx,
g.o.jsOut(pkg, moduleName),
fileTypes,
pkg.Path,
append(g.includePaths, includePaths...),
); err != nil {
return err
}
}

return nil
}

func (g *generator) resolveInclude(modules ...protopath.Module) (paths []string, err error) {
return protopath.ResolveDependencyPaths(g.modfile.Require, modules...)
}
50 changes: 50 additions & 0 deletions starport/pkg/cosmosproto/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cosmosproto

import (
"bytes"
"context"

"github.com/pkg/errors"
"github.com/tendermint/starport/starport/pkg/cmdrunner"
"github.com/tendermint/starport/starport/pkg/cmdrunner/step"
"github.com/tendermint/starport/starport/pkg/xexec"
)

// ErrProtocNotInstalled is returned when protoc isn't installed on the system.
var ErrProtocNotInstalled = errors.New("protoc is not installed")

// InstallDependencies installs protoc dependencies needed by Cosmos ecosystem.
func InstallDependencies(ctx context.Context, appPath string) error {
if !xexec.IsCommandAvailable("protoc") {
return ErrProtocNotInstalled
}

errb := &bytes.Buffer{}
err := cmdrunner.
New(
cmdrunner.DefaultStderr(errb),
cmdrunner.DefaultWorkdir(appPath),
).
Run(ctx,
// installs the gocosmos plugin with the version specified under the
// go.mod of the app.
step.New(
step.Exec(
"go",
"get",
"github.com/regen-network/cosmos-proto/protoc-gen-gocosmos@v0.3.1",
),
),
// install grpc-gateway.
step.New(
step.Exec(
"go",
"get",
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway@v1.16.0",
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger@v1.16.0",
"github.com/golang/protobuf/protoc-gen-go@v1.4.3",
),
),
)
return errors.Wrap(err, errb.String())
}
Loading
0