-
Notifications
You must be signed in to change notification settings - Fork 532
/
generate.go
492 lines (418 loc) · 13.6 KB
/
generate.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package cosmosgen
import (
"bytes"
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"slices"
"gopkg.in/yaml.v2"
"github.com/ignite/cli/v28/ignite/pkg/cache"
"github.com/ignite/cli/v28/ignite/pkg/cliui/colors"
"github.com/ignite/cli/v28/ignite/pkg/cliui/icons"
"github.com/ignite/cli/v28/ignite/pkg/cmdrunner"
"github.com/ignite/cli/v28/ignite/pkg/cmdrunner/step"
"github.com/ignite/cli/v28/ignite/pkg/cosmosanalysis/module"
"github.com/ignite/cli/v28/ignite/pkg/cosmosbuf"
"github.com/ignite/cli/v28/ignite/pkg/cosmosver"
"github.com/ignite/cli/v28/ignite/pkg/errors"
"github.com/ignite/cli/v28/ignite/pkg/events"
"github.com/ignite/cli/v28/ignite/pkg/gomodule"
"github.com/ignite/cli/v28/ignite/pkg/xfilepath"
"github.com/ignite/cli/v28/ignite/pkg/xos"
)
const (
moduleCacheNamespace = "generate.setup.module"
includeProtoCacheNamespace = "generator.includes.proto"
workFilename = "buf.work.yaml"
)
var (
ErrBufConfig = errors.New("invalid Buf config")
ErrMissingSDKDep = errors.New("cosmos-sdk dependency not found")
protocGlobalInclude = xfilepath.List(
xfilepath.JoinFromHome(xfilepath.Path("local/include")),
xfilepath.JoinFromHome(xfilepath.Path(".local/include")),
)
)
// protoIncludes contains proto include paths for a package.
type protoIncludes struct {
// Paths is a list of proto include paths.
Paths []string
// BufPath is the path to the Buf config file when it exists.
BufPath string
// ProtoPath contains the path to the package's proto directory.
ProtoPath string
}
// protoAnalysis contains proto module analysis data for a Go package dependency.
type protoAnalysis struct {
// Path is the full path to the Go dependency
Path string
// Modules contains the proto modules analysis data.
// The list is empty when the Go package has no proto files.
Modules []module.Module
// Includes contain proto include paths.
// These paths should be used when generating code.
Includes protoIncludes
}
func newBufConfigError(path string, cause error) error {
return errors.Errorf("%w: %s: %w", ErrBufConfig, path, cause)
}
func (g *generator) setup(ctx context.Context) (err error) {
// Cosmos SDK hosts proto files of own x/ modules and some third party ones needed by itself and
// blockchain apps. Generate should be aware of these and make them available to the blockchain
// app that wants to generate code for its own proto.
//
// blockchain apps may use different versions of the SDK. following code first makes sure that
// app's dependencies are download by 'go mod' and cached under the local filesystem.
// and then, it determines which version of the SDK is used by the app and what is the absolute path
// of its source code.
var errb bytes.Buffer
if err := cmdrunner.
New(
cmdrunner.DefaultStderr(&errb),
cmdrunner.DefaultWorkdir(g.appPath),
).Run(ctx, step.New(step.Exec("go", "mod", "download"))); err != nil {
return errors.Wrap(err, errb.String())
}
modFile, err := gomodule.ParseAt(g.appPath)
if err != nil {
return err
}
// Read the dependencies defined in the `go.mod` file
g.deps, err = gomodule.ResolveDependencies(modFile, false)
if err != nil {
return err
}
// Dependencies are resolved, it is possible that the cosmos sdk has been replaced
g.sdkImport = cosmosver.CosmosModulePath
for _, dep := range g.deps {
if cosmosver.CosmosSDKModulePathPattern.MatchString(dep.Path) {
g.sdkImport = dep.Path
break
}
}
// Discover any custom modules defined by the user's app.
// Use the configured proto directory to locate app's proto files.
g.appModules, err = module.Discover(
ctx,
g.appPath,
g.appPath,
module.WithProtoDir(g.protoDir),
module.WithSDKDir(g.sdkDir),
)
if err != nil {
return err
}
g.appIncludes, _, err = g.resolveIncludes(ctx, g.appPath)
if err != nil {
return err
}
dep, found := filterCosmosSDKModule(g.deps)
if !found {
return ErrMissingSDKDep
}
// Find the full path to the Cosmos SDK Go package.
// The path is required to be able to discover proto packages for the
// set of "cosmossdk.io" packages that doesn't contain the proto files.
g.sdkDir, err = gomodule.LocatePath(ctx, g.cacheStorage, g.appPath, dep)
if err != nil {
return err
}
// Go through the Go dependencies of the user's app within go.mod, some of them
// might be hosting Cosmos SDK modules that could be in use by user's blockchain.
//
// Cosmos SDK is a dependency of all blockchains, so it's absolute that we'll be
// discovering all modules of the SDK as well during this process.
//
// Even if a dependency contains some SDK modules, not all of these modules could
// be used by user's blockchain. This is fine, we can still generate TS clients
// for those non modules, it is up to user to use (import in typescript) not use
// generated modules.
//
// TODO: we can still implement some sort of smart filtering to detect non used
// modules by the user's blockchain at some point, it is a nice to have.
moduleCache := cache.New[protoAnalysis](g.cacheStorage, moduleCacheNamespace)
for _, dep := range g.deps {
// Try to get the cached list of modules for the current dependency package
cacheKey := cache.Key(dep.Path, dep.Version)
depInfo, err := moduleCache.Get(cacheKey)
if err != nil && !errors.Is(err, cache.ErrorNotFound) {
return err
}
// Discover the modules of the dependency package when they are not cached
if errors.Is(err, cache.ErrorNotFound) {
// Get the absolute path to the package's directory
path, err := gomodule.LocatePath(ctx, g.cacheStorage, g.appPath, dep)
if err != nil {
return err
}
// Discover any modules defined by the package.
// Use an empty string for proto directory because it will be
// discovered automatically within the dependency package path.
modules, err := module.Discover(ctx, g.appPath, path, module.WithSDKDir(g.sdkDir))
if err != nil {
return err
}
// Dependency/includes resolution per module is done to solve versioning issues
var (
includes protoIncludes
cacheable = true
)
if len(modules) > 0 {
includes, cacheable, err = g.resolveIncludes(ctx, path)
if err != nil {
return err
}
}
depInfo = protoAnalysis{
Path: path,
Modules: modules,
Includes: includes,
}
if cacheable {
if err = moduleCache.Put(cacheKey, depInfo); err != nil {
return err
}
}
}
g.thirdModules[depInfo.Path] = depInfo.Modules
g.thirdModuleIncludes[depInfo.Path] = depInfo.Includes
}
return nil
}
func (g *generator) getProtoIncludeFolders(modPath string) []string {
// Add default protoDir and default includeDirs
includePaths := []string{filepath.Join(modPath, g.protoDir)}
for _, dir := range g.opts.includeDirs {
includePaths = append(includePaths, filepath.Join(modPath, dir))
}
return includePaths
}
func (g *generator) findBufPath(modpath string) (string, error) {
var bufPath string
err := filepath.WalkDir(modpath, func(path string, _ fs.DirEntry, err error) error {
if err != nil {
return err
}
base := filepath.Base(path)
if base == "buf.yaml" || base == "buf.yml" {
bufPath = path
return filepath.SkipAll
}
return nil
})
if err != nil {
return "", err
}
return bufPath, nil
}
func (g *generator) generateBufIncludeFolder(ctx context.Context, modpath string) (string, error) {
protoPath, err := os.MkdirTemp("", "includeFolder")
if err != nil {
return "", err
}
g.tmpDirs = append(g.tmpDirs, protoPath)
err = g.buf.Export(ctx, modpath, protoPath)
if err != nil {
return "", err
}
return protoPath, nil
}
func (g *generator) resolveIncludes(ctx context.Context, path string) (protoIncludes, bool, error) {
// Init paths with the global include paths for protoc
paths, err := protocGlobalInclude()
if err != nil {
return protoIncludes{}, false, err
}
includes := protoIncludes{Paths: paths}
// The "cosmossdk.io" module packages must use SDK's proto path which is
// where all proto files for there type of Go package are.
var protoPath string
if module.IsCosmosSDKModulePkg(path) {
protoPath = filepath.Join(g.sdkDir, "proto")
} else {
// Check that the app/package proto directory exists
protoPath = filepath.Join(path, g.protoDir)
fi, err := os.Stat(protoPath)
if err != nil && !os.IsNotExist(err) {
return protoIncludes{}, false, err
} else if !fi.IsDir() {
// Just return the global includes when a proto directory doesn't exist
return includes, true, nil
}
}
// Add app's proto path to the list of proto paths
includes.Paths = append(includes.Paths, protoPath)
includes.ProtoPath = protoPath
// Check if a Buf config file is present
includes.BufPath, err = g.findBufPath(protoPath)
if err != nil {
return includes, false, err
}
if includes.BufPath != "" {
// When a Buf config exists export all protos needed
// to build the modules to a temporary include folder.
bufProtoPath, err := g.generateBufIncludeFolder(ctx, protoPath)
if err != nil && !errors.Is(err, cosmosbuf.ErrProtoFilesNotFound) {
return protoIncludes{}, false, err
}
// Use exported files only when the path contains ".proto" files
if bufProtoPath != "" {
includes.Paths = append(includes.Paths, bufProtoPath)
return includes, false, nil
}
}
// When there is no Buf config add the configured directories
// instead to keep the legacy (non Buf) behavior.
includes.Paths = append(includes.Paths, g.getProtoIncludeFolders(path)...)
return includes, true, nil
}
func (g generator) updateBufModule(ctx context.Context) error {
for pkgPath, includes := range g.thirdModuleIncludes {
// Skip third party dependencies without proto files
if includes.ProtoPath == "" {
continue
}
// Resolve the Go package and use the module name as the proto vendor directory name
modFile, err := gomodule.ParseAt(pkgPath)
if err != nil {
return err
}
pkgName := modFile.Module.Mod.Path
// When a Buf config with name is available add it to app's dependencies
// or otherwise export the proto files to a vendor directory.
if includes.BufPath != "" {
if err := g.resolveBufDependency(ctx, pkgName, includes.BufPath); err != nil {
return err
}
} else {
if err := g.vendorProtoPackage(pkgName, includes.ProtoPath); err != nil {
return err
}
}
}
return nil
}
func (g generator) resolveBufDependency(ctx context.Context, pkgName, bufPath string) error {
// Open the dependency Buf config to find the BSR package name
f, err := os.Open(bufPath)
if err != nil {
return err
}
defer f.Close()
cfg := struct {
Name string `yaml:"name"`
}{}
if err := yaml.NewDecoder(f).Decode(&cfg); err != nil {
return newBufConfigError(bufPath, err)
}
// When dependency package has a Buf config name try to add it to app's
// dependencies. Name is optional and defines the BSR package name.
if cfg.Name != "" {
return g.addBufDependency(ctx, cfg.Name)
}
// By default just vendor the proto package
return g.vendorProtoPackage(pkgName, filepath.Dir(bufPath))
}
func (g generator) addBufDependency(ctx context.Context, depName string) error {
// Read app's Buf config
path := g.appIncludes.BufPath
bz, err := os.ReadFile(path)
if err != nil {
return err
}
// Check if the proto dependency is already present in app's Buf config
cfg := struct {
Deps []string `yaml:"deps"`
}{}
if err := yaml.Unmarshal(bz, &cfg); err != nil {
return newBufConfigError(path, err)
}
if slices.Contains(cfg.Deps, depName) {
return nil
}
// Add the new dependency and update app's Buf config
f, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0o644)
if err != nil {
return err
}
defer f.Close()
var rawCfg map[string]interface{}
if err := yaml.Unmarshal(bz, &rawCfg); err != nil {
return newBufConfigError(path, err)
}
rawCfg["deps"] = append(cfg.Deps, depName)
enc := yaml.NewEncoder(f)
defer enc.Close()
if err := enc.Encode(rawCfg); err != nil {
return err
}
g.opts.ev.Send(
fmt.Sprintf("New Buf dependency added: %s", colors.Name(depName)),
events.Icon(icons.OK),
)
// Update Buf lock so it contains the new dependency
return g.buf.Update(ctx, filepath.Dir(path), depName)
}
func (g generator) vendorProtoPackage(pkgName, protoPath string) (err error) {
// Check that the dependency vendor directory doesn't exist
vendorRelPath := filepath.Join("proto_vendor", pkgName)
vendorPath := filepath.Join(g.appPath, vendorRelPath)
_, err = os.Stat(vendorPath)
if err != nil && !os.IsNotExist(err) {
return err
}
// Skip vendoring when the dependency is already vendored
if !os.IsNotExist(err) {
return nil
}
if err = os.MkdirAll(vendorPath, 0o777); err != nil {
return err
}
// Make sure that the vendor folder is removed on error
defer func() {
if err != nil {
_ = os.RemoveAll(vendorPath)
}
}()
if err = xos.CopyFolder(protoPath, vendorPath); err != nil {
return err
}
path := filepath.Join(g.appPath, workFilename)
bz, err := os.ReadFile(path)
if err != nil {
return errors.Errorf("error reading Buf workspace file: %s: %w", path, err)
}
ws := struct {
Version string `yaml:"version"`
Directories []string `yaml:"directories"`
}{}
if err := yaml.Unmarshal(bz, &ws); err != nil {
return err
}
ws.Directories = append(ws.Directories, vendorRelPath)
f, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0o644)
if err != nil {
return err
}
defer f.Close()
enc := yaml.NewEncoder(f)
defer enc.Close()
if err = enc.Encode(ws); err != nil {
return err
}
g.opts.ev.Send(
fmt.Sprintf("New Buf vendored dependency added: %s", colors.Name(vendorRelPath)),
events.Icon(icons.OK),
)
return nil
}
func filterCosmosSDKModule(versions []gomodule.Version) (gomodule.Version, bool) {
for _, v := range versions {
if cosmosver.CosmosSDKModulePathPattern.MatchString(v.Path) {
return v, true
}
}
return gomodule.Version{}, false
}