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 (backport #4090) #4224

Merged
merged 14 commits into from
Jul 15, 2024
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
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
Loading