Skip to content

Commit

Permalink
feat: remove protoc pkg and also nodetime helpers ts-proto and `s…
Browse files Browse the repository at this point in the history
…ta` (backport #4090) (#4224)

* feat: remove `protoc` pkg and also nodetime helpers `ts-proto` and `sta` (#4090)

* remove protoc

* add changelog

* run go mod tidy

* remove unused proto folder handling

* add ts-client tests

* remove unused methods

* use import includes instead manual

* use buf dep instead buf mod

* change buf mod update to comand to buf dep update

* improve code readbility

* bump buf.build

* bump buf into the go.mod

* bump protobuf pkgs for buf

* check if the folder has proto before update buf dependencies

---------

Co-authored-by: Pantani <Pantani>
(cherry picked from commit 8e0937d)

# Conflicts:
#	go.mod
#	go.sum
#	ignite/internal/plugin/testdata/execute_fail/go.mod
#	ignite/internal/plugin/testdata/execute_ok/go.mod
#	ignite/internal/tools/gen-config-doc/go.mod
#	ignite/internal/tools/gen-config-doc/go.sum
#	ignite/internal/tools/gen-mig-diffs/go.mod
#	ignite/internal/tools/gen-mig-diffs/go.sum
#	ignite/pkg/cosmosbuf/buf.go
#	ignite/pkg/cosmosgen/generate_go.go
#	ignite/pkg/cosmosgen/generate_openapi.go
#	ignite/pkg/cosmosgen/generate_typescript.go
#	ignite/pkg/nodetime/nodetime.go
#	ignite/pkg/nodetime/programs/sta/sta.go
#	ignite/pkg/nodetime/programs/ts-proto/tsproto.go
#	ignite/pkg/protoc/protoc.go
#	ignite/templates/app/files/go.mod.plush
#	ignite/templates/app/files/proto/buf.lock
#	integration/doctor/testdata/missing-tools.go.txt
#	integration/plugin/testdata/example-plugin/go.mod

* resolve conflicts

* fix changelog

* remove unused files

* fix TestPluginExecute

* fix tests for ts generation

* rollback go version from tests

* fix text namespace version for ts client generation

---------

Co-authored-by: Danilo Pantani <[email protected]>
Co-authored-by: Julien Robert <[email protected]>
  • Loading branch information
3 people authored Jul 15, 2024
1 parent 9e853ee commit c1ae9c2
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 493 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
scripts/**/nodetime-*
**/testdata/**/go.sum
**/testdata/go.sum
dist/
node_modules
.DS_Store
Expand All @@ -8,3 +9,4 @@ node_modules
docs/.vuepress/dist
build/
*coverage.*
*.ign
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features

- [#4183](https://github.com/ignite/cli/pull/4183) Set `chain-id` in the client.toml
- [#4090](https://github.com/ignite/cli/pull/4090) Remove `protoc` pkg and also nodetime helpers `ts-proto` and `sta`
- [#4076](https://github.com/ignite/cli/pull/4076) Remove the ignite `relayer` and `tools` commands with all ts-relayer logic
- [#4133](https://github.com/ignite/cli/pull/4133) Improve buf rate limit

Expand Down
11 changes: 2 additions & 9 deletions ignite/cmd/generate_typescript_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,8 @@ func generateTSClientHandler(cmd *cobra.Command, _ []string) error {
return err
}

output, err := cmd.Flags().GetString(flagOutput)
if err != nil {
return err
}

useCache, err := cmd.Flags().GetBool(flagUseCache)
if err != nil {
return err
}
output, _ := cmd.Flags().GetString(flagOutput)
useCache, _ := cmd.Flags().GetBool(flagUseCache)

var opts []chain.GenerateTarget
if flagGetEnableProtoVendor(cmd) {
Expand Down
99 changes: 26 additions & 73 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/v28/ignite/pkg/cache"
"github.com/ignite/cli/v28/ignite/pkg/cosmosanalysis/module"
"github.com/ignite/cli/v28/ignite/pkg/cosmosbuf"
"github.com/ignite/cli/v28/ignite/pkg/dirchange"
"github.com/ignite/cli/v28/ignite/pkg/gomodulepath"
"github.com/ignite/cli/v28/ignite/pkg/nodetime/programs/sta"
tsproto "github.com/ignite/cli/v28/ignite/pkg/nodetime/programs/ts-proto"
"github.com/ignite/cli/v28/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,32 +77,11 @@ 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) {
add := func(sourcePath string, modules []module.Module) {
for _, m := range modules {
m := m

gg.Go(func() error {
cacheKey := m.Pkg.Path
paths := []string{m.Pkg.Path, g.g.opts.jsOut(m)}
Expand All @@ -122,8 +100,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); err != nil {
return err
}

Expand All @@ -132,30 +109,24 @@ func (g *tsGenerator) generateModuleTemplates(ctx context.Context) error {
}
}

add(g.g.appPath, g.g.appModules, g.g.appIncludes.Paths)
add(g.g.appPath, g.g.appModules)

// Always generate third party modules; This is required because not generating them might
// lead to issues with the module registration in the root template. The root template must
// always be generated with 3rd party modules which means that if a new 3rd party module
// is available and not generated it would lead to the registration of a new not generated
// 3rd party module.
for sourcePath, modules := range g.g.thirdModules {
// TODO: Skip modules without proto files?
thirdIncludes := g.g.thirdModuleIncludes[sourcePath]
add(sourcePath, modules, append(g.g.appIncludes.Paths, thirdIncludes.Paths...))
add(sourcePath, modules)
}

return gg.Wait()
}

func (g *tsGenerator) generateModuleTemplate(
ctx context.Context,
protocCmd protoc.Cmd,
staCmd sta.Cmd,
tsprotoPluginPath,
appPath string,
m module.Module,
includePaths []string,
) error {
var (
out = g.g.opts.jsOut(m)
Expand All @@ -164,48 +135,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.generateOpenAPISpec(ctx); 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.IncludeWKT(),
); err != nil {
return err
}

return templateTSClientModule.Write(out, pp, struct {
return templateTSClientModule.Write(out, protoPath, struct {
Module module.Module
}{
Module: m,
Expand Down
58 changes: 58 additions & 0 deletions ignite/pkg/cosmosgen/sta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cosmosgen

import (
"os"
"path/filepath"
)

const routeNameTemplate = `<%
const { routeInfo, utils } = it;
const {
operationId,
method,
route,
moduleName,
responsesTypes,
description,
tags,
summary,
pathArgs,
} = routeInfo;
const { _, fmtToJSDocLine, require } = utils;
const methodAliases = {
get: (pathName, hasPathInserts) =>
_.camelCase(` + "`" + `${pathName}_${hasPathInserts ? "detail" : "list"}` + "`" + `),
post: (pathName, hasPathInserts) => _.camelCase(` + "`" + `${pathName}_create` + "`" + `),
put: (pathName, hasPathInserts) => _.camelCase(` + "`" + `${pathName}_update` + "`" + `),
patch: (pathName, hasPathInserts) => _.camelCase(` + "`" + `${pathName}_partial_update` + "`" + `),
delete: (pathName, hasPathInserts) => _.camelCase(` + "`" + `${pathName}_delete` + "`" + `),
};
const createCustomOperationId = (method, route, moduleName) => {
const hasPathInserts = /\{(\w){1,}\}/g.test(route);
const splitedRouteBySlash = _.compact(_.replace(route, /\{(\w){1,}\}/g, "").split("/"));
const routeParts = (splitedRouteBySlash.length > 1
? splitedRouteBySlash.splice(1)
: splitedRouteBySlash
).join("_");
return routeParts.length > 3 && methodAliases[method]
? methodAliases[method](routeParts, hasPathInserts)
: _.camelCase(_.lowerCase(method) + "_" + [moduleName].join("_")) || "index";
};
if (operationId) {
let routeName = operationId.replace('_','');
return routeName[0].toLowerCase() + routeName.slice(1);
}
if (route === "/")
return _.camelCase(` + "`" + `${_.lowerCase(method)}Root` + "`" + `);
return createCustomOperationId(method, route, moduleName);
%>`

// generateRouteNameFile generates the `route-name.eta` file.
func generateRouteNameFile(outPath string) error {
outTemplate := filepath.Join(outPath, "route-name.eta")
return os.WriteFile(outTemplate, []byte(routeNameTemplate), 0o644)
}
74 changes: 0 additions & 74 deletions ignite/pkg/nodetime/nodetime.go

This file was deleted.

Loading

0 comments on commit c1ae9c2

Please sign in to comment.