From 8812ce71fd2238f104870b3049a6a3f75027c0fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0lker=20G=C3=B6ktu=C4=9F=20=C3=96ZT=C3=9CRK?= Date: Mon, 15 Mar 2021 02:47:15 -0700 Subject: [PATCH 1/2] feat: provide more tpl data to loader & add user-side warnings about not modifying generated dirs/files. --- starport/pkg/cosmosgen/generate_javascript.go | 56 +++++--------- starport/pkg/cosmosgen/template.go | 77 +++++++++++++++---- .../js/{client.ts.tpl => index.ts.tpl} | 2 + .../vuex/{loader.ts.tpl => root/index.ts.tpl} | 7 +- .../cosmosgen/templates/vuex/root/readme.md | 1 + .../vuex/{store.ts.tpl => store/index.ts.tpl} | 0 .../cosmosgen/templates/vuex/store/vuex-root | 1 + 7 files changed, 88 insertions(+), 56 deletions(-) rename starport/pkg/cosmosgen/templates/js/{client.ts.tpl => index.ts.tpl} (96%) rename starport/pkg/cosmosgen/templates/vuex/{loader.ts.tpl => root/index.ts.tpl} (77%) create mode 100644 starport/pkg/cosmosgen/templates/vuex/root/readme.md rename starport/pkg/cosmosgen/templates/vuex/{store.ts.tpl => store/index.ts.tpl} (100%) create mode 100644 starport/pkg/cosmosgen/templates/vuex/store/vuex-root diff --git a/starport/pkg/cosmosgen/generate_javascript.go b/starport/pkg/cosmosgen/generate_javascript.go index 7a40c07e14..5f407231cf 100644 --- a/starport/pkg/cosmosgen/generate_javascript.go +++ b/starport/pkg/cosmosgen/generate_javascript.go @@ -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. @@ -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 @@ -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, + 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 } diff --git a/starport/pkg/cosmosgen/template.go b/starport/pkg/cosmosgen/template.go index 899ec57637..d3b5fa51ba 100644 --- a/starport/pkg/cosmosgen/template.go +++ b/starport/pkg/cosmosgen/template.go @@ -2,6 +2,8 @@ package cosmosgen import ( "embed" + "fmt" + "os" "path/filepath" "strings" "text/template" @@ -13,32 +15,73 @@ 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")) + fmt.Println(out) + + 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 } diff --git a/starport/pkg/cosmosgen/templates/js/client.ts.tpl b/starport/pkg/cosmosgen/templates/js/index.ts.tpl similarity index 96% rename from starport/pkg/cosmosgen/templates/js/client.ts.tpl rename to starport/pkg/cosmosgen/templates/js/index.ts.tpl index 588d655163..0d94d084ed 100644 --- a/starport/pkg/cosmosgen/templates/js/client.ts.tpl +++ b/starport/pkg/cosmosgen/templates/js/index.ts.tpl @@ -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"; diff --git a/starport/pkg/cosmosgen/templates/vuex/loader.ts.tpl b/starport/pkg/cosmosgen/templates/vuex/root/index.ts.tpl similarity index 77% rename from starport/pkg/cosmosgen/templates/vuex/loader.ts.tpl rename to starport/pkg/cosmosgen/templates/vuex/root/index.ts.tpl index d7e6759d94..06b5ef6590 100644 --- a/starport/pkg/cosmosgen/templates/vuex/loader.ts.tpl +++ b/starport/pkg/cosmosgen/templates/vuex/root/index.ts.tpl @@ -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 }}'), {{ end }} } + function load(mod, fullns) { return function init(store) { const fullnsLevels = fullns.split('/') diff --git a/starport/pkg/cosmosgen/templates/vuex/root/readme.md b/starport/pkg/cosmosgen/templates/vuex/root/readme.md new file mode 100644 index 0000000000..a9a292f4b9 --- /dev/null +++ b/starport/pkg/cosmosgen/templates/vuex/root/readme.md @@ -0,0 +1 @@ +THIS FOLDER IS GENERATED AUTOMATICALLY. DO NOT MODIFY. diff --git a/starport/pkg/cosmosgen/templates/vuex/store.ts.tpl b/starport/pkg/cosmosgen/templates/vuex/store/index.ts.tpl similarity index 100% rename from starport/pkg/cosmosgen/templates/vuex/store.ts.tpl rename to starport/pkg/cosmosgen/templates/vuex/store/index.ts.tpl diff --git a/starport/pkg/cosmosgen/templates/vuex/store/vuex-root b/starport/pkg/cosmosgen/templates/vuex/store/vuex-root new file mode 100644 index 0000000000..0fcc121a15 --- /dev/null +++ b/starport/pkg/cosmosgen/templates/vuex/store/vuex-root @@ -0,0 +1 @@ +THIS FILE IS GENERATED AUTOMATICALLY. DO NOT DELETE. From 0e75c1a132f922c3e4a649fdbef2453dccef4433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0lker=20G=C3=B6ktu=C4=9F=20=C3=96ZT=C3=9CRK?= Date: Mon, 15 Mar 2021 03:48:08 -0700 Subject: [PATCH 2/2] cosmetic --- starport/pkg/cosmosgen/template.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/starport/pkg/cosmosgen/template.go b/starport/pkg/cosmosgen/template.go index d3b5fa51ba..4d45111556 100644 --- a/starport/pkg/cosmosgen/template.go +++ b/starport/pkg/cosmosgen/template.go @@ -2,7 +2,6 @@ package cosmosgen import ( "embed" - "fmt" "os" "path/filepath" "strings" @@ -66,7 +65,6 @@ func (t templateWriter) Write(destDir, protoPath string, data interface{}) error ) out := filepath.Join(destDir, strings.TrimSuffix(filepath.Base(path), ".tpl")) - fmt.Println(out) f, err := os.OpenFile(out, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) if err != nil {