-
Notifications
You must be signed in to change notification settings - Fork 531
/
cosmosgen.go
272 lines (228 loc) · 7.37 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package cosmosgen
import (
"context"
"os"
"path/filepath"
"strings"
"github.com/iancoleman/strcase"
gomodule "golang.org/x/mod/module"
"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/events"
)
// generateOptions used to configure code generation.
type generateOptions struct {
includeDirs []string
useCache bool
updateBufModule bool
ev events.Bus
generateProtobuf 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
}
// 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 protobuf (gogoproto and pulsar) code generation.
func WithGoGeneration() Option {
return func(o *generateOptions) {
o.generateProtobuf = true
}
}
// 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
}
}
// UpdateBufModule enables Buf config proto dependencies update.
// This option updates app's Buf config when proto packages or
// Buf modules are found within the Go dependencies.
func UpdateBufModule() Option {
return func(o *generateOptions) {
o.updateBufModule = true
}
}
// CollectEvents sets an event bus for sending generation feedback events.
func CollectEvents(ev events.Bus) Option {
return func(c *generateOptions) {
c.ev = ev
}
}
// generator generates code for sdk and sdk apps.
type generator struct {
buf cosmosbuf.Buf
cacheStorage cache.Storage
appPath string
protoDir string
gomodPath string
opts *generateOptions
sdkImport string
sdkDir string
deps []gomodule.Version
appModules []module.Module
appIncludes protoIncludes
thirdModules map[string][]module.Module
thirdModuleIncludes map[string]protoIncludes
tmpDirs []string
}
func (g *generator) cleanup() {
// Remove temporary directories created during generation
for _, path := range g.tmpDirs {
_ = os.RemoveAll(path)
}
}
// 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, gomodPath string, options ...Option) error {
b, err := cosmosbuf.New()
if err != nil {
return err
}
defer b.Cleanup()
g := &generator{
buf: b,
appPath: appPath,
protoDir: protoDir,
gomodPath: gomodPath,
opts: &generateOptions{},
thirdModules: make(map[string][]module.Module),
thirdModuleIncludes: make(map[string]protoIncludes),
cacheStorage: cacheStorage,
}
defer g.cleanup()
for _, apply := range options {
apply(g.opts)
}
if err := g.setup(ctx); err != nil {
return err
}
// Update app's Buf config for third party discovered proto modules.
// Go dependency packages might contain proto files which could also
// optionally be using Buf, so for those cases the discovered proto
// files should be available before code generation.
if g.opts.updateBufModule {
if err := g.updateBufModule(ctx); 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.opts.generateProtobuf {
if err := g.generateGoGo(ctx); err != nil {
return err
}
if err := g.generatePulsar(ctx); err != nil {
return err
}
}
if g.opts.specOut != "" {
if err := g.generateOpenAPISpec(ctx); err != nil {
return err
}
}
if g.opts.jsOut != nil {
if err := g.generateTS(ctx); err != nil {
return err
}
}
if g.opts.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.opts.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.opts.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
}
}
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)
}
}