8000 feat: provide more tpl data to loader & add user-side warnings by ilgooz · Pull Request #857 · ignite/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: provide more tpl data to loader & add user-side warnings #857

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 2 commits into from
Mar 15, 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
56 changes: 19 additions & 37 deletions starport/pkg/cosmosgen/generate_javascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,39 +185,17 @@ func (g *jsGenerator) generateModule(ctx context.Context, tsprotoPluginPath, app
}

// generate the js client wrapper.
outclient := filepath.Join(out, "index.ts")
f, err := os.OpenFile(outclient, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()

pp := filepath.Join(appPath, g.g.protoDir)
err = templateJSClient(pp).Execute(f, struct{ Module module.Module }{m})
if err != nil {
if err := templateJSClient.Write(out, pp, struct{ Module module.Module }{m}); err != nil {
return err
}

// generate Vuex if enabled.
if g.g.o.vuexStoreRootPath != "" {
storePath := filepath.Join(storeDirPath, "index.ts")
f, err := os.OpenFile(storePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
err = templateVuexStore.Write(storeDirPath, pp, struct{ Module module.Module }{m})
if err != nil {
return err
}
defer f.Close()

err = templateVuexStore(pp).Execute(f, struct{ Module module.Module }{m})
if err != nil {
return err
}

// mark vuex root dir.
f, err = os.Create(filepath.Join(storeDirPath, vuexRootMarker))
if err != nil {
return err
}
f.Close()
}

// generate .js and .d.ts files for all ts files.
Expand All @@ -231,8 +209,10 @@ func (g *jsGenerator) generateVuexModuleLoader() error {
}

type module struct {
Name string
Path string
Name string
Path string
FullName string
FullPath string
}

var modules []module
Expand All @@ -242,21 +222,23 @@ func (g *jsGenerator) generateVuexModuleLoader() error {
if err != nil {
return err
}
pathrel = filepath.Dir(pathrel)
name := strcase.ToCamel(strings.ReplaceAll(pathrel, "/", "_"))
modules = append(modules, module{name, pathrel})
var (
fullPath = filepath.Dir(pathrel)
fullName = strcase.ToCamel(strings.ReplaceAll(fullPath, "/", "_"))
path = filepath.Base(fullPath)
name = strcase.ToCamel(path)
)
modules = append(modules, module{
Name: name,
Copy link
Member Author
@ilgooz ilgooz Mar 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @clockworkgr these are for the module loader to enable shorter component names.

Path: path,
FullName: fullName,
FullPath: fullPath,
})
}

loaderPath := filepath.Join(g.g.o.vuexStoreRootPath, "index.ts")

f, err := os.OpenFile(loaderPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()

err = templateVuexLoader(g.g.o.vuexStoreRootPath).Execute(f, modules)
if err != nil {
if err := templateVuexRoot.Write(g.g.o.vuexStoreRootPath, "", modules); err != nil {
return err
}

Expand Down
75 changes: 58 additions & 17 deletions starport/pkg/cosmosgen/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cosmosgen

import (
"embed"
"os"
"path/filepath"
"strings"
"text/template"
Expand All @@ -13,32 +14,72 @@ var (
//go:embed templates/*
templates embed.FS

templateJSClient = tpl("js/client.ts.tpl") // js wrapper client.
templateVuexStore = tpl("vuex/store.ts.tpl") // vuex store.
templateVuexLoader = tpl("vuex/loader.ts.tpl") // vuex store loader.
templateJSClient = newTemplateWriter("js") // js wrapper client.
templateVuexRoot = newTemplateWriter("vuex/root") // vuex store loader.
templateVuexStore = newTemplateWriter("vuex/store") // vuex store.
)

type templateWriter struct {
templateDir string
}

// tpl returns a func for template residing at templatePath to initialize a text template
// with given protoPath.
func tpl(templatePath string) func(protoPath string) *template.Template {
return func(protoPath string) *template.Template {
path := filepath.Join("templates", templatePath)

funcs := template.FuncMap{
"camelCase": strcase.ToLowerCamel,
"resolveFile": func(fullPath string) string {
rel, _ := filepath.Rel(protoPath, fullPath)
rel = strings.TrimSuffix(rel, ".proto")
return rel
},
}
func newTemplateWriter(templateDir string) templateWriter {
return templateWriter{
templateDir,
}
}

func (t templateWriter) Write(destDir, protoPath string, data interface{}) error {
base := filepath.Join("templates", t.templateDir)

// find out templates inside the dir.
files, err := templates.ReadDir(base)
if err != nil {
return err
}

var paths []string
for _, file := range files {
paths = append(paths, filepath.Join(base, file.Name()))
}

return template.
funcs := template.FuncMap{
"camelCase": strcase.ToLowerCamel,
"resolveFile": func(fullPath string) string {
rel, _ := filepath.Rel(protoPath, fullPath)
rel = strings.TrimSuffix(rel, ".proto")
return rel
},
}

// render and write the template.
write := func(path string) error {
tpl := template.
Must(
template.
New(filepath.Base(path)).
Funcs(funcs).
ParseFS(templates, path),
ParseFS(templates, paths...),
)

out := filepath.Join(destDir, strings.TrimSuffix(filepath.Base(path), ".tpl"))

f, err := os.OpenFile(out, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()

return tpl.Execute(f, data)
}

for _, path := range paths {
if err := write(path); err != nil {
return err
}
}

return nil
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// THIS FILE IS GENERATED AUTOMATICALLY. DO NOT MODIFY.

import { coins, StdFee } from "@cosmjs/launchpad";
import { SigningStargateClient } from "@cosmjs/stargate";
import { Registry, OfflineSigner, EncodeObject, DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{{ range . }}import {{ .Name }} from './{{ .Path }}'
// THIS FILE IS GENERATED AUTOMATICALLY. DO NOT MODIFY.

{{ range . }}import {{ .FullName }} from './{{ .FullPath }}'
{{ end }}

export default {
{{ range . }}{{ .Name }}: load({{ .Name }}, 'chain/{{ .Path }}'),
{{ range . }}{{ .FullName }}: load({{ .FullName }}, 'chain/{{ .FullPath }}'),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{{ end }}
}


function load(mod, fullns) {
return function init(store) {
const fullnsLevels = fullns.split('/')
Expand Down
1 change: 1 addition & 0 deletions starport/pkg/cosmosgen/templates/vuex/root/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
THIS FOLDER IS GENERATED AUTOMATICALLY. DO NOT MODIFY.
1 change: 1 addition & 0 deletions starport/pkg/cosmosgen/templates/vuex/store/vuex-root
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
THIS FILE IS GENERATED AUTOMATICALLY. DO NOT DELETE.
0