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: remove protoc pkg and also nodetime helpers ts-proto and sta #4090

Merged
merged 33 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c2b34d5
remove protoc
Apr 20, 2024
b9eef1f
add changelog
Apr 20, 2024
d1f1163
run go mod tidy
Apr 20, 2024
294dd9f
Merge branch 'main' into feat/remove-protoc
Pantani Apr 20, 2024
312d2b1
Merge branch 'main' into feat/remove-protoc
Pantani Apr 20, 2024
24eff37
Merge branch 'main' into feat/remove-protoc
Pantani Apr 22, 2024
589cf6a
Merge branch 'main' into feat/remove-protoc
Pantani Apr 23, 2024
de46f32
Merge remote-tracking branch 'origin/main' into feat/remove-protoc
Apr 24, 2024
1aa2acd
Merge branch 'main' into feat/remove-protoc
Pantani Apr 24, 2024
3a62bfd
Merge branch 'main' into feat/remove-protoc
Pantani Apr 24, 2024
b7fbc96
Merge branch 'main' into feat/remove-protoc
Pantani Apr 26, 2024
dbd9235
remove unused proto folder handling
Apr 26, 2024
fb43bf5
add ts-client tests
Apr 26, 2024
d70d093
Merge branch 'main' into feat/remove-protoc
Pantani Apr 29, 2024
bde5326
Merge branch 'main' into feat/remove-protoc
Pantani May 3, 2024
d4f842c
Merge remote-tracking branch 'origin/main' into feat/remove-protoc
May 14, 2024
0e94d18
remove unused methods
May 14, 2024
5e18e2a
Merge branch 'main' into feat/remove-protoc
Pantani May 15, 2024
95207bc
Merge branch 'main' into feat/remove-protoc
Pantani May 16, 2024
0066723
Merge branch 'main' into feat/remove-protoc
Pantani May 16, 2024
3250955
Merge branch 'main' into feat/remove-protoc
Pantani May 16, 2024
cb6be1e
Merge branch 'main' into feat/remove-protoc
Pantani May 16, 2024
6db8f58
Merge remote-tracking branch 'origin/main' into feat/remove-protoc
May 21, 2024
c3d8ac0
use import includes instead manual
May 21, 2024
ec78d89
use buf dep instead buf mod
May 21, 2024
aa62b83
change buf mod update to comand to buf dep update
May 22, 2024
ec9804e
improve code readbility
May 22, 2024
d8a9c42
bump buf.build
May 22, 2024
18aa025
bump buf into the go.mod
May 22, 2024
6dc0147
bump protobuf pkgs for buf
May 22, 2024
7817f4c
check if the folder has proto before update buf dependencies
May 22, 2024
9247202
Merge branch 'main' into feat/remove-protoc
Pantani May 28, 2024
7d85e0b
Merge branch 'main' into feat/remove-protoc
Pantani May 28, 2024
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 @@ -13,6 +13,7 @@
- [#4071](https://github.com/ignite/cli/pull/4071) Support custom proto path
- [#3718](https://github.com/ignite/cli/pull/3718) Add `gen-mig-diffs` tool app to compare scaffold output of two versions of ignite
- [#4077](https://github.com/ignite/cli/pull/4077) Merge the swagger files manually instead use nodetime `swagger-combine`
- [#4090](https://github.com/ignite/cli/pull/4090) Remove `protoc` pkg and also nodetime helpers `ts-proto` and `sta`

### Changes

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ require (
github.com/hashicorp/go-plugin v1.6.0
github.com/iancoleman/strcase v0.3.0
github.com/ignite/ignite-files/nodetime v0.0.4
github.com/ignite/ignite-files/protoc v0.0.1
github.com/ignite/web v0.6.1
github.com/imdario/mergo v0.3.13
github.com/jpillora/chisel v1.9.1
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -911,8 +911,6 @@ github.com/ianlancetaylor/demangle v0.0.0-20210905161508-09a460cdf81d/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20230524184225-eabc099b10ab/go.mod h1:gx7rwoVhcfuVKG5uya9Hs3Sxj7EIvldVofAWIUtGouw=
github.com/ignite/ignite-files/nodetime v0.0.4 h1:4mA/lZTAWczsNBHZ543VSxpK+P6BAU0HsLglxUtRrMU=
github.com/ignite/ignite-files/nodetime v0.0.4/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.6.1 h1:kHG+T7NnR8cCPjAGxEFQD+njVYM08toeG57iYRXzpwo=
github.com/ignite/web v0.6.1/go.mod h1:WZWBaBYF8RazN7dE462BLpvXDY8ScacxcJ07BKwX/jY=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
Expand Down
64 changes: 52 additions & 12 deletions ignite/pkg/cosmosbuf/buf.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,47 @@ type (
sdkProtoDir string
cache *protoanalysis.Cache
}

// genOptions represents the buf generate options.
genOptions struct {
flags map[string]string
excludeFiles []string
includeImports []string
}

// GenOptions represents the buf generate options function.
GenOptions func(o *genOptions)
)

func newGenOptions() *genOptions {
return &genOptions{
flags: make(map[string]string),
excludeFiles: make([]string, 0),
includeImports: make([]string, 0),
}
}

// WithGenerateFlag provides flag options for the buf generate command.
func WithGenerateFlag(flag, value string) GenOptions {
return func(o *genOptions) {
o.flags[flag] = value
}
}

// ExcludeFiles exclude proto files.
func ExcludeFiles(excludeFiles ...string) GenOptions {
return func(o *genOptions) {
o.excludeFiles = append(o.excludeFiles, excludeFiles...)
}
}

// IncludeImports include proto import.
func IncludeImports(includeImports ...string) GenOptions {
return func(o *genOptions) {
o.includeImports = append(o.includeImports, includeImports...)
}
}

const (
binaryName = "buf"
flagTemplate = "template"
Expand Down Expand Up @@ -146,18 +185,19 @@ func (b Buf) Generate(
protoDir,
output,
template string,
excludeFilename ...string,
options ...GenOptions,
) (err error) {
var (
excluded = make(map[string]struct{})
flags = map[string]string{
flagTemplate: template,
flagOutput: output,
flagErrorFormat: fmtJSON,
flagLogFormat: fmtJSON,
}
)
for _, file := range excludeFilename {
opts := newGenOptions()
for _, apply := range options {
apply(opts)
}
opts.flags[flagTemplate] = template
opts.flags[flagOutput] = output
opts.flags[flagErrorFormat] = fmtJSON
opts.flags[flagLogFormat] = fmtJSON

excluded := make(map[string]struct{})
for _, file := range opts.excludeFiles {
excluded[file] = struct{}{}
}

Expand Down Expand Up @@ -197,7 +237,7 @@ func (b Buf) Generate(
continue
}

cmd, err := b.generateCommand(CMDGenerate, flags, file.Path)
cmd, err := b.generateCommand(CMDGenerate, opts.flags, file.Path)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion ignite/pkg/cosmosgen/generate_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/otiai10/copy"

"github.com/ignite/cli/v29/ignite/pkg/cosmosbuf"
"github.com/ignite/cli/v29/ignite/pkg/errors"
)

Expand All @@ -31,7 +32,7 @@ func (g *generator) generateGoGo(ctx context.Context) error {
protoPath := filepath.Join(g.appPath, g.protoDir)

// code generate for each module.
err = g.buf.Generate(ctx, protoPath, tmp, g.gogoTemplate(), "module.proto")
err = g.buf.Generate(ctx, protoPath, tmp, g.gogoTemplate(), cosmosbuf.ExcludeFiles("module.proto"))
if err != nil {
return err
}
Expand Down
50 changes: 2 additions & 48 deletions ignite/pkg/cosmosgen/generate_openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ignite/cli/v29/ignite/pkg/cache"
"github.com/ignite/cli/v29/ignite/pkg/cosmosanalysis/module"
"github.com/ignite/cli/v29/ignite/pkg/cosmosbuf"
"github.com/ignite/cli/v29/ignite/pkg/dirchange"
"github.com/ignite/cli/v29/ignite/pkg/errors"
swaggercombine "github.com/ignite/cli/v29/ignite/pkg/swagger-combine"
Expand All @@ -25,10 +26,6 @@ func (g *generator) openAPITemplate() string {
return filepath.Join(g.appPath, g.protoDir, "buf.gen.swagger.yaml")
}

func (g *generator) openAPITemplateForSTA() string {
return filepath.Join(g.appPath, g.protoDir, "buf.gen.sta.yaml")
}

func (g *generator) generateOpenAPISpec(ctx context.Context) error {
var (
specDirs []string
Expand Down Expand Up @@ -72,7 +69,7 @@ func (g *generator) generateOpenAPISpec(ctx context.Context) error {
}

hasAnySpecChanged = true
err = g.buf.Generate(ctx, m.Pkg.Path, dir, g.openAPITemplate(), "module.proto")
err = g.buf.Generate(ctx, m.Pkg.Path, dir, g.openAPITemplate(), cosmosbuf.ExcludeFiles("module.proto"))
if err != nil {
return err
}
Expand Down Expand Up @@ -144,46 +141,3 @@ func (g *generator) generateOpenAPISpec(ctx context.Context) error {

return dirchange.SaveDirChecksum(specCache, out, g.appPath, out)
}

// generateModuleOpenAPISpec generates a spec for a module where it's source code resides at src.
// and adds needed swaggercombine configure for it.
func (g *generator) generateModuleOpenAPISpec(ctx context.Context, m module.Module, out string) error {
var (
specDirs []string
title = "HTTP API Console " + m.Pkg.Name
conf = swaggercombine.New(title, g.gomodPath)
)
defer func() {
for _, dir := range specDirs {
os.RemoveAll(dir)
}
}()

// generate specs for each module and persist them in the file system
// after add their path and config to swaggercombine.Config so we can combine them
// into a single spec.
dir, err := os.MkdirTemp("", "gen-openapi-module-spec")
if err != nil {
return err
}

err = g.buf.Generate(ctx, m.Pkg.Path, dir, g.openAPITemplateForSTA(), "module.proto")
if err != nil {
return err
}

specs, err := xos.FindFiles(dir, xos.JSONFile)
if err != nil {
return err
}

for _, spec := range specs {
if err := conf.AddSpec(strcase.ToCamel(m.Pkg.Name), spec, false); err != nil {
return err
}
}
specDirs = append(specDirs, dir)

// combine specs into one and save to out.
return conf.Combine(out)
}
89 changes: 23 additions & 66 deletions ignite/pkg/cosmosgen/generate_typescript.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,12 @@ import (

"github.com/ignite/cli/v29/ignite/pkg/cache"
"github.com/ignite/cli/v29/ignite/pkg/cosmosanalysis/module"
"github.com/ignite/cli/v29/ignite/pkg/cosmosbuf"
"github.com/ignite/cli/v29/ignite/pkg/dirchange"
"github.com/ignite/cli/v29/ignite/pkg/gomodulepath"
"github.com/ignite/cli/v29/ignite/pkg/nodetime/programs/sta"
tsproto "github.com/ignite/cli/v29/ignite/pkg/nodetime/programs/ts-proto"
"github.com/ignite/cli/v29/ignite/pkg/protoc"
)

var (
dirchangeCacheNamespace = "generate.typescript.dirchange"
tsOut = []string{"--ts_proto_out=."}
)
var dirchangeCacheNamespace = "generate.typescript.dirchange"

type tsGenerator struct {
g *generator
Expand All @@ -37,6 +32,10 @@ func newTSGenerator(g *generator) *tsGenerator {
return &tsGenerator{g}
}

func (g *generator) tsTemplate() string {
return filepath.Join(g.appPath, g.protoDir, "buf.gen.ts.yaml")
}

func (g *generator) generateTS(ctx context.Context) error {
chainPath, _, err := gomodulepath.Find(g.appPath)
if err != nil {
Expand Down Expand Up @@ -78,26 +77,6 @@ func (g *generator) generateTS(ctx context.Context) error {
}

func (g *tsGenerator) generateModuleTemplates(ctx context.Context) error {
protocCmd, cleanupProtoc, err := protoc.Command()
if err != nil {
return err
}

defer cleanupProtoc()

tsprotoPluginPath, cleanupPlugin, err := tsproto.BinaryPath()
if err != nil {
return err
}

defer cleanupPlugin()

staCmd, cleanupSTA, err := sta.Command()
if err != nil {
return err
}

defer cleanupSTA()
gg := &errgroup.Group{}
dirCache := cache.New[[]byte](g.g.cacheStorage, dirchangeCacheNamespace)
add := func(sourcePath string, modules []module.Module, includes []string) {
Expand All @@ -122,8 +101,7 @@ func (g *tsGenerator) generateModuleTemplates(ctx context.Context) error {
}
}

err = g.generateModuleTemplate(ctx, protocCmd, staCmd, tsprotoPluginPath, sourcePath, m, includes)
if err != nil {
if err := g.generateModuleTemplate(ctx, sourcePath, m, includes); err != nil {
return err
}

Expand All @@ -150,9 +128,6 @@ func (g *tsGenerator) generateModuleTemplates(ctx context.Context) error {

func (g *tsGenerator) generateModuleTemplate(
ctx context.Context,
protocCmd protoc.Cmd,
staCmd sta.Cmd,
tsprotoPluginPath,
appPath string,
m module.Module,
includePaths []string,
Expand All @@ -164,48 +139,30 @@ func (g *tsGenerator) generateModuleTemplate(
if err := os.MkdirAll(typesOut, 0o766); err != nil {
return err
}

// generate ts-proto types
err := protoc.Generate(
ctx,
typesOut,
m.Pkg.Path,
includePaths,
tsOut,
protoc.Plugin(tsprotoPluginPath, "--ts_proto_opt=snakeToCamel=true", "--ts_proto_opt=esModuleInterop=true"),
protoc.Env("NODE_OPTIONS="), // unset nodejs options to avoid unexpected issues with vercel "pkg"
protoc.WithCommand(protocCmd),
)
if err != nil {
return err
}

specPath := filepath.Join(out, "api.swagger.yml")

if err = g.g.generateModuleOpenAPISpec(ctx, m, specPath); err != nil {
return err
}
// generate the REST client from the OpenAPI spec

var (
srcSpec = specPath
outREST = filepath.Join(out, "rest.ts")
)

if err := sta.Generate(ctx, outREST, srcSpec, sta.WithCommand(staCmd)); err != nil {
if err := generateRouteNameFile(typesOut); err != nil {
return err
}

// All "cosmossdk.io" module packages must use SDK's
// proto path which is where the proto files are stored.
var pp string
protoPath := filepath.Join(g.g.appPath, g.g.protoDir)
if module.IsCosmosSDKModulePkg(appPath) {
pp = filepath.Join(g.g.sdkDir, "proto")
} else {
pp = filepath.Join(appPath, g.g.protoDir)
protoPath = filepath.Join(g.g.sdkDir, "proto")
}

// code generate for each module.
if err := g.g.buf.Generate(
ctx,
protoPath,
typesOut,
g.g.tsTemplate(),
cosmosbuf.ExcludeFiles("module.proto"),
cosmosbuf.IncludeImports(includePaths...),
); err != nil {
return err
}

return templateTSClientModule.Write(out, pp, struct {
return templateTSClientModule.Write(out, protoPath, struct {
Module module.Module
}{
Module: m,
Expand Down
Loading
Loading