Skip to content

Commit 8ab2149

Browse files
authored
feat: Client config typing (#2569)
* feat: Client config typing * fix: remove console * wip * wip * fix: remove accidentally committed files * fixed: type issues/code duplication * fix: use new @starport/vuex getter * fix: correct export
1 parent 6e4efa4 commit 8ab2149

File tree

22 files changed

+565
-239
lines changed

22 files changed

+565
-239
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ require (
208208
github.com/spf13/viper v1.10.1 // indirect
209209
github.com/subosito/gotenv v1.2.0 // indirect
210210
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca // indirect
211+
github.com/takuoki/gocase v1.0.0 // indirect
211212
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
212213
github.com/tendermint/btcd v0.1.1 // indirect
213214
github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,8 @@ github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG
14561456
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
14571457
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca h1:Ld/zXl5t4+D69SiV4JoN7kkfvJdOWlPpfxrzxpLMoUk=
14581458
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM=
1459+
github.com/takuoki/gocase v1.0.0 h1:gPwLJTWVm2T1kUiCsKirg/faaIUGVTI0FA3SYr75a44=
1460+
github.com/takuoki/gocase v1.0.0/go.mod h1:QgOKJrbuJoDrtoKswBX1/Dw8mJrkOV9tbQZJaxaJ6zc=
14591461
github.com/tchap/go-patricia v2.2.6+incompatible/go.mod h1:bmLyhP68RS6kStMGxByiQ23RP/odRBOTVjwp2cDyi6I=
14601462
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c h1:g+WoO5jjkqGAzHWCjJB1zZfXPIAaDpzXIEJ0eS6B5Ok=
14611463
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c/go.mod h1:ahpPrc7HpcfEWDQRZEmnXMzHY03mLDYMCxeDzy46i+8=

ignite/chainconfig/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ type Client struct {
117117
// TSClient configures code generation for Typescript Client.
118118
Typescript Typescript `yaml:"typescript"`
119119

120+
// Vuex configures code generation for Vuex stores.
121+
Vuex Typescript `yaml:"vuex"`
122+
120123
// Dart configures client code generation for Dart.
121124
Dart Dart `yaml:"dart"`
122125

@@ -130,6 +133,12 @@ type Typescript struct {
130133
Path string `yaml:"path"`
131134
}
132135

136+
// Vuex configures code generation for Vuex stores.
137+
type Vuex struct {
138+
// Path configures out location for generated Vuex stores code.
139+
Path string `yaml:"path"`
140+
}
141+
133142
// Dart configures client code generation for Dart.
134143
type Dart struct {
135144
// Path configures out location for generated Dart code.

ignite/cmd/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Produced source code can be regenerated by running a command again and is not me
1919
flagSetPath(c)
2020
c.AddCommand(NewGenerateGo())
2121
c.AddCommand(NewGenerateTSClient())
22+
c.AddCommand(NewGenerateVuex())
2223
c.AddCommand(NewGenerateDart())
2324
c.AddCommand(NewGenerateOpenAPI())
2425

ignite/cmd/generate_vuex.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package ignitecmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
8+
"github.com/ignite-hq/cli/ignite/pkg/clispinner"
9+
"github.com/ignite-hq/cli/ignite/services/chain"
10+
)
11+
12+
func NewGenerateVuex() *cobra.Command {
13+
c := &cobra.Command{
14+
Use: "vuex",
15+
Short: "Generate Typescript client and Vuex stores for your chain's frontend from your `config.yml` file",
16+
RunE: generateVuexHandler,
17+
}
18+
c.Flags().AddFlagSet(flagSetProto3rdParty(""))
19+
return c
20+
}
21+
22+
func generateVuexHandler(cmd *cobra.Command, args []string) error {
23+
s := clispinner.New().SetText("Generating...")
24+
defer s.Stop()
25+
26+
c, err := newChainWithHomeFlags(cmd, chain.EnableThirdPartyModuleCodegen())
27+
if err != nil {
28+
return err
29+
}
30+
31+
if err := c.Generate(cmd.Context(), chain.GenerateVuex()); err != nil {
32+
return err
33+
}
34+
35+
s.Stop()
36+
fmt.Println("⛏️ Generated Typescript Client and Vuex stores")
37+
38+
return nil
39+
}

ignite/pkg/cosmosgen/cosmosgen.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ type generateOptions struct {
1717
jsIncludeThirdParty bool
1818
tsClientRootPath string
1919

20+
vuexOut func(module.Module) string
21+
vuexRootPath string
22+
2023
specOut string
2124

2225
dartOut func(module.Module) string
@@ -39,7 +42,13 @@ func WithTSClientGeneration(includeThirdPartyModules bool, out func(module.Modul
3942
o.tsClientRootPath = tsClientRootPath
4043
}
4144
}
42-
45+
func WithVuexGeneration(includeThirdPartyModules bool, out func(module.Module) (path string), vuexRootPath string) Option {
46+
return func(o *generateOptions) {
47+
o.vuexOut = out
48+
o.jsIncludeThirdParty = includeThirdPartyModules
49+
o.vuexRootPath = vuexRootPath
50+
}
51+
}
4352
func WithDartGeneration(includeThirdPartyModules bool, out func(module.Module) (path string), rootPath string) Option {
4453
return func(o *generateOptions) {
4554
o.dartOut = out
@@ -116,6 +125,12 @@ func Generate(ctx context.Context, appPath, protoDir string, options ...Option)
116125
}
117126
}
118127

128+
if g.o.vuexOut != nil {
129+
if err := g.generateVuex(); err != nil {
130+
return err
131+
}
132+
}
133+
119134
if g.o.dartOut != nil {
120135
if err := g.generateDart(); err != nil {
121136
return err

ignite/pkg/cosmosgen/generate_typescript.go

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ func (g *generator) generateTS() error {
6969
return err
7070
}
7171

72-
if err := tsg.generateVueTemplates(data); err != nil {
73-
return err
74-
}
75-
7672
if err := tsg.generateRootTemplates(data); err != nil {
7773
return err
7874
}
@@ -94,7 +90,7 @@ func (g *tsGenerator) generateModuleTemplates() error {
9490
m := m
9591
gg.Go(func() error {
9692
var (
97-
out = filepath.Join(g.g.o.tsClientRootPath, "client", m.Pkg.Name)
93+
out = filepath.Join(g.g.o.tsClientRootPath, m.Pkg.Name)
9894
typesOut = filepath.Join(out, "types")
9995
ctx = g.g.ctx
10096
appPath = sourcePath
@@ -171,58 +167,13 @@ func (g *tsGenerator) generateModuleTemplates() error {
171167
return gg.Wait()
172168
}
173169

174-
func (g *tsGenerator) generateVueTemplates(payload generatePayload) error {
175-
gg := &errgroup.Group{}
176-
177-
generate := func() {
178-
for _, m := range payload.Modules {
179-
m := m
180-
181-
gg.Go(func() error {
182-
vueAPIOut := filepath.Join(g.g.o.tsClientRootPath, "vue", m.Pkg.Name)
183-
184-
if err := os.MkdirAll(vueAPIOut, 0766); err != nil {
185-
return err
186-
}
187-
188-
if err := templateTSClientVue.Write(vueAPIOut, "", struct {
189-
Module module.Module
190-
User string
191-
Repo string
192-
}{
193-
Module: m,
194-
User: payload.User,
195-
Repo: payload.Repo,
196-
}); err != nil {
197-
return err
198-
}
199-
200-
return nil
201-
})
202-
}
203-
}
204-
205-
generate()
206-
207-
return gg.Wait()
208-
}
209-
210170
func (g *tsGenerator) generateRootTemplates(payload generatePayload) error {
211-
tsClientOut := filepath.Join(g.g.o.tsClientRootPath, "client")
171+
tsClientOut := filepath.Join(g.g.o.tsClientRootPath)
212172
if err := os.MkdirAll(tsClientOut, 0766); err != nil {
213173
return err
214174
}
215175
if err := templateTSClientRoot.Write(tsClientOut, "", payload); err != nil {
216176
return err
217177
}
218-
219-
vueOut := filepath.Join(g.g.o.tsClientRootPath, "vue")
220-
if err := os.MkdirAll(vueOut, 0766); err != nil {
221-
return err
222-
}
223-
if err := templateTSClientVueRoot.Write(vueOut, "", payload); err != nil {
224-
return err
225-
}
226-
227178
return nil
228179
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package cosmosgen
2+
3+
import (
4+
"github.com/ignite-hq/cli/ignite/pkg/cosmosanalysis/module"
5+
"github.com/ignite-hq/cli/ignite/pkg/giturl"
6+
"github.com/ignite-hq/cli/ignite/pkg/gomodulepath"
7+
8+
"os"
9+
"path/filepath"
10+
11+
"golang.org/x/sync/errgroup"
12+
)
13+
14+
type vuexGenerator struct {
15+
g *generator
16+
}
17+
18+
type generateVuexPayload struct {
19+
Modules []module.Module
20+
User string ``
21+
Repo string ``
22+
}
23+
24+
func newVuexGenerator(g *generator) *vuexGenerator {
25+
return &vuexGenerator{
26+
g: g,
27+
}
28+
}
29+
30+
func (g *generator) generateVuex() error {
31+
vsg := newVuexGenerator(g)
32+
33+
chainPath, _, err := gomodulepath.Find(g.appPath)
34+
if err != nil {
35+
return err
36+
}
37+
38+
chainInfo, err := giturl.Parse(chainPath.RawPath)
39+
if err != nil {
40+
return err
41+
}
42+
43+
data := generatePayload{
44+
Modules: g.appModules,
45+
User: chainInfo.User,
46+
Repo: chainInfo.Repo,
47+
}
48+
49+
if g.o.jsIncludeThirdParty {
50+
for _, modules := range g.thirdModules {
51+
data.Modules = append(data.Modules, modules...)
52+
}
53+
}
54+
55+
if err := vsg.generateVueTemplates(data); err != nil {
56+
return err
57+
}
58+
59+
if err := vsg.generateRootTemplates(data); err != nil {
60+
return err
61+
}
62+
63+
return nil
64+
}
65+
66+
func (g *vuexGenerator) generateVueTemplates(payload generatePayload) error {
67+
gg := &errgroup.Group{}
68+
69+
generate := func() {
70+
for _, m := range payload.Modules {
71+
m := m
72+
73+
gg.Go(func() error {
74+
vueAPIOut := filepath.Join(g.g.o.vuexRootPath, m.Pkg.Name)
75+
76+
if err := os.MkdirAll(vueAPIOut, 0766); err != nil {
77+
return err
78+
}
79+
80+
if err := templateTSClientVue.Write(vueAPIOut, "", struct {
81+
Module module.Module
82+
User string
83+
Repo string
84+
}{
85+
Module: m,
86+
User: payload.User,
87+
Repo: payload.Repo,
88+
}); err != nil {
89+
return err
90+
}
91+
92+
return nil
93+
})
94+
}
95+
}
96+
97+
generate()
98+
99+
return gg.Wait()
100+
}
101+
102+
func (g *vuexGenerator) generateRootTemplates(payload generatePayload) error {
103+
vueOut := filepath.Join(g.g.o.vuexRootPath)
104+
if err := os.MkdirAll(vueOut, 0766); err != nil {
105+
return err
106+
}
107+
if err := templateTSClientVueRoot.Write(vueOut, "", payload); err != nil {
108+
return err
109+
}
110+
111+
return nil
112+
}

ignite/pkg/cosmosgen/template.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"text/template"
99

1010
"github.com/iancoleman/strcase"
11+
"github.com/takuoki/gocase"
1112
)
1213

1314
var (
@@ -48,6 +49,9 @@ func (t templateWriter) Write(destDir, protoPath string, data interface{}) error
4849

4950
funcs := template.FuncMap{
5051
"camelCase": strcase.ToLowerCamel,
52+
"camelCaseSta": func(word string) string {
53+
return gocase.Revert(strcase.ToLowerCamel(word))
54+
},
5155
"capitalCase": func(word string) string {
5256
replacer := strings.NewReplacer("-", "_", ".", "_")
5357

0 commit comments

Comments
 (0)