/
cosmosgen.go
215 lines (179 loc) · 5.75 KB
/
cosmosgen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package cosmosgen
import (
"context"
"path/filepath"
"strings"
"github.com/iancoleman/strcase"
gomodule "golang.org/x/mod/module"
"github.com/spellshape/cli/spellshape/pkg/cache"
"github.com/spellshape/cli/spellshape/pkg/cosmosanalysis/module"
)
// generateOptions used to configure code generation.
type generateOptions struct {
includeDirs []string
gomodPath string
useCache bool
jsOut func(module.Module) string
tsClientRootPath string
vuexOut func(module.Module) string
vuexRootPath string
composablesOut func(module.Module) string
composablesRootPath string
hooksOut func(module.Module) string
hooksRootPath string
specOut string
}
// TODO add WithInstall.
// ModulePathFunc defines a function type that returns a path based on a Cosmos SDK module.
type ModulePathFunc func(module.Module) string
// Option configures code generation.
type Option func(*generateOptions)
// WithTSClientGeneration adds Typescript Client code generation.
// The tsClientRootPath is used to determine the root path of generated Typescript classes.
func WithTSClientGeneration(out ModulePathFunc, tsClientRootPath string, useCache bool) Option {
return func(o *generateOptions) {
o.jsOut = out
o.tsClientRootPath = tsClientRootPath
o.useCache = useCache
}
}
func WithVuexGeneration(out ModulePathFunc, vuexRootPath string) Option {
return func(o *generateOptions) {
o.vuexOut = out
o.vuexRootPath = vuexRootPath
}
}
func WithComposablesGeneration(out ModulePathFunc, composablesRootPath string) Option {
return func(o *generateOptions) {
o.composablesOut = out
o.composablesRootPath = composablesRootPath
}
}
func WithHooksGeneration(out ModulePathFunc, hooksRootPath string) Option {
return func(o *generateOptions) {
o.hooksOut = out
o.hooksRootPath = hooksRootPath
}
}
// WithGoGeneration adds Go code generation.
func WithGoGeneration(gomodPath string) Option {
return func(o *generateOptions) {
o.gomodPath = gomodPath
}
}
// WithOpenAPIGeneration adds OpenAPI spec generation.
func WithOpenAPIGeneration(out string) Option {
return func(o *generateOptions) {
o.specOut = out
}
}
// IncludeDirs configures the third party proto dirs that used by app's proto.
// relative to the projectPath.
func IncludeDirs(dirs []string) Option {
return func(o *generateOptions) {
o.includeDirs = dirs
}
}
// generator generates code for sdk and sdk apps.
type generator struct {
ctx context.Context
cacheStorage cache.Storage
appPath string
protoDir string
o *generateOptions
sdkImport string
deps []gomodule.Version
appModules []module.Module
thirdModules map[string][]module.Module // app dependency-modules pair.
}
// Generate generates code from protoDir of an SDK app residing at appPath with given options.
// protoDir must be relative to the projectPath.
func Generate(ctx context.Context, cacheStorage cache.Storage, appPath, protoDir string, options ...Option) error {
g := &generator{
ctx: ctx,
appPath: appPath,
protoDir: protoDir,
o: &generateOptions{},
thirdModules: make(map[string][]module.Module),
cacheStorage: cacheStorage,
}
for _, apply := range options {
apply(g.o)
}
if err := g.setup(); err != nil {
return err
}
// Go generation must run first so the types are created before other
// generated code that requires sdk.Msg implementations to be defined
if g.o.gomodPath != "" {
if err := g.generateGo(); err != nil {
return err
}
}
if g.o.jsOut != nil {
if err := g.generateTS(); err != nil {
return err
}
}
if g.o.vuexOut != nil {
if err := g.generateVuex(); err != nil {
return err
}
// Update Vuex store dependencies when Vuex stores are generated.
// This update is required to link the "ts-client" folder so the
// package is available during development before publishing it.
if err := g.updateVuexDependencies(); err != nil {
return err
}
// Update Vue app dependencies when Vuex stores are generated.
// This update is required to link the "ts-client" folder so the
// package is available during development before publishing it.
if err := g.updateVueDependencies(); err != nil {
return err
}
}
if g.o.composablesRootPath != "" {
if err := g.generateComposables("vue"); err != nil {
return err
}
// Update Vue app dependencies when Vue composables are generated.
// This update is required to link the "ts-client" folder so the
// package is available during development before publishing it.
if err := g.updateComposableDependencies("vue"); err != nil {
return err
}
}
if g.o.hooksRootPath != "" {
if err := g.generateComposables("react"); err != nil {
return err
}
// Update React app dependencies when React hooks are generated.
// This update is required to link the "ts-client" folder so the
// package is available during development before publishing it.
if err := g.updateComposableDependencies("react"); err != nil {
return err
}
}
if g.o.specOut != "" {
if err := generateOpenAPISpec(g); err != nil {
return err
}
}
return nil
}
// TypescriptModulePath generates TS module paths for Cosmos SDK modules.
// The root path is used as prefix for the generated paths.
func TypescriptModulePath(rootPath string) ModulePathFunc {
return func(m module.Module) string {
return filepath.Join(rootPath, m.Pkg.Name)
}
}
// ComposableModulePath generates useQuery hook/composable module paths for Cosmos SDK modules.
// The root path is used as prefix for the generated paths.
func ComposableModulePath(rootPath string) ModulePathFunc {
return func(m module.Module) string {
replacer := strings.NewReplacer("-", "_", ".", "_")
modPath := strcase.ToCamel(replacer.Replace(m.Pkg.Name))
return filepath.Join(rootPath, "use"+modPath)
}
}