forked from ignite/cli
/
generate.go
178 lines (142 loc) · 4.13 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
package chain
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/notional-labs/tinyport/tinyport/pkg/cosmosanalysis/module"
"github.com/notional-labs/tinyport/tinyport/pkg/cosmosgen"
"github.com/notional-labs/tinyport/tinyport/pkg/giturl"
)
const (
defaultVuexPath = "vue/src/store"
defaultDartPath = "flutter/lib"
defaultOpenAPIPath = "docs/static/openapi.yml"
)
type generateOptions struct {
isGoEnabled bool
isVuexEnabled bool
isDartEnabled bool
isOpenAPIEnabled bool
}
// GenerateTarget is a target to generate code for from proto files.
type GenerateTarget func(*generateOptions)
// GenerateGo enables generating proto based Go code needed for the chain's source code.
func GenerateGo() GenerateTarget {
return func(o *generateOptions) {
o.isGoEnabled = true
}
}
// GenerateVuex enables generating proto based Vuex store.
func GenerateVuex() GenerateTarget {
return func(o *generateOptions) {
o.isVuexEnabled = true
}
}
// GenerateDart enables generating Dart client.
func GenerateDart() GenerateTarget {
return func(o *generateOptions) {
o.isDartEnabled = true
}
}
// GenerateOpenAPI enables generating OpenAPI spec for your chain.
func GenerateOpenAPI() GenerateTarget {
return func(o *generateOptions) {
o.isOpenAPIEnabled = true
}
}
func (c *Chain) generateAll(ctx context.Context) error {
conf, err := c.Config()
if err != nil {
return err
}
var additionalTargets []GenerateTarget
if conf.Client.Vuex.Path != "" {
additionalTargets = append(additionalTargets, GenerateVuex())
}
if conf.Client.Dart.Path != "" {
additionalTargets = append(additionalTargets, GenerateDart())
}
if conf.Client.OpenAPI.Path != "" {
additionalTargets = append(additionalTargets, GenerateOpenAPI())
}
return c.Generate(ctx, GenerateGo(), additionalTargets...)
}
// Generate makes code generation from proto files for given target and additionalTargets.
func (c *Chain) Generate(
ctx context.Context,
target GenerateTarget,
additionalTargets ...GenerateTarget,
) error {
var targetOptions generateOptions
for _, apply := range append(additionalTargets, target) {
apply(&targetOptions)
}
conf, err := c.Config()
if err != nil {
return err
}
if err := cosmosgen.InstallDependencies(ctx, c.app.Path); err != nil {
return err
}
fmt.Fprintln(c.stdLog().out, "🛠️ Building proto...")
options := []cosmosgen.Option{
cosmosgen.IncludeDirs(conf.Build.Proto.ThirdPartyPaths),
}
if targetOptions.isGoEnabled {
options = append(options, cosmosgen.WithGoGeneration(c.app.ImportPath))
}
enableThirdPartyModuleCodegen := !c.protoBuiltAtLeastOnce && c.options.isThirdPartyModuleCodegenEnabled
// generate Vuex code as well if it is enabled.
if targetOptions.isVuexEnabled {
vuexPath := conf.Client.Vuex.Path
if vuexPath == "" {
vuexPath = defaultVuexPath
}
storeRootPath := filepath.Join(c.app.Path, vuexPath, "generated")
if err := os.MkdirAll(storeRootPath, 0766); err != nil {
return err
}
options = append(options,
cosmosgen.WithVuexGeneration(
enableThirdPartyModuleCodegen,
func(m module.Module) string {
parsedGitURL, _ := giturl.Parse(m.Pkg.GoImportName)
return filepath.Join(storeRootPath, parsedGitURL.UserAndRepo(), m.Pkg.Name, "module")
},
storeRootPath,
),
)
}
if targetOptions.isDartEnabled {
dartPath := conf.Client.Dart.Path
if dartPath == "" {
dartPath = defaultDartPath
}
rootPath := filepath.Join(c.app.Path, dartPath, "generated")
if err := os.MkdirAll(rootPath, 0766); err != nil {
return err
}
options = append(options,
cosmosgen.WithDartGeneration(
enableThirdPartyModuleCodegen,
func(m module.Module) string {
return filepath.Join(rootPath, m.Pkg.Name, "module")
},
rootPath,
),
)
}
if targetOptions.isOpenAPIEnabled {
openAPIPath := conf.Client.OpenAPI.Path
if openAPIPath == "" {
openAPIPath = defaultOpenAPIPath
}
options = append(options, cosmosgen.WithOpenAPIGeneration(openAPIPath))
}
if err := cosmosgen.Generate(ctx, c.app.Path, conf.Build.Proto.Path, options...); err != nil {
return &CannotBuildAppError{err}
}
c.protoBuiltAtLeastOnce = true
return nil
}