-
Notifications
You must be signed in to change notification settings - Fork 23
/
generate.go
384 lines (343 loc) · 10.4 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
package microgateway
import (
"bytes"
"fmt"
"github.com/project-flogo/core/support/service"
"go/parser"
"go/printer"
"go/token"
"os"
"regexp"
"strings"
"github.com/project-flogo/core/action"
"github.com/project-flogo/core/app"
"github.com/project-flogo/core/app/resource"
"github.com/project-flogo/core/data/expression/function"
"github.com/project-flogo/core/data/property"
"github.com/project-flogo/core/data/schema"
"github.com/project-flogo/core/support"
)
// VersionMaster is the master version
const VersionMaster = "master"
// Import is a package import
type Import struct {
Alias string
Import string
Version string
Port string
Used bool
}
// GetAlias gets the import alias and marks it as used
func (i *Import) GetAlias() string {
i.Used = true
return i.Alias
}
// Imports are the package imports
type Imports struct {
Imports []*Import
}
// Ensure looks up an import and adds it if it is missing
func (i *Imports) Ensure(path string, options ...string) *Import {
if strings.HasPrefix(path, "#") {
alias := strings.TrimPrefix(path, "#")
for _, port := range i.Imports {
if port.Alias == alias {
return port
}
}
panic(fmt.Errorf("ref %s not found", path))
}
for _, port := range i.Imports {
if port.Import == path {
return port
}
}
alias, version, relative := "", VersionMaster, ""
if len(options) > 0 {
if value := options[0]; value != "" {
alias = value
}
}
if len(options) > 1 {
if value := options[1]; value != "" {
version = value
}
}
if len(options) > 2 {
if value := options[2]; value != "" {
relative = value
}
}
if alias == "" {
parts := strings.Split(path+relative, "/")
alias = parts[len(parts)-1]
}
if alias != "_" {
for _, port := range i.Imports {
if port.Alias == alias {
alias = fmt.Sprintf("port%d", len(i.Imports))
break
}
}
}
port := &Import{
Alias: alias,
Import: path + relative,
Version: version,
Port: path,
}
i.Imports = append(i.Imports, port)
return port
}
type generator struct {
resManager *resource.Manager
imports Imports
}
func (g *generator) ResourceManager() *resource.Manager {
return g.resManager
}
func (g *generator) ServiceManager() *service.Manager {
return nil
}
func (g *generator) RuntimeSettings() map[string]interface{} {
return nil
}
var flogoImportPattern = regexp.MustCompile(`^(([^ ]*)[ ]+)?([^@:]*)@?([^:]*)?:?(.*)?$`) // extract import path even if there is an alias and/or a version
// Generator generates code for an action
type Generator interface {
Generate(settingsName string, imports *Imports, config *action.Config) (code string, err error)
}
// GenerateResource is used to determine if a resource is generated, defaults to true
type GenerateResource interface {
Generate() bool
}
// Generate generates flogo go API code
func Generate(config *app.Config, file string, modFile string) {
if config.Type != "flogo:app" {
panic("invalid app type")
}
app := generator{}
for _, anImport := range config.Imports {
matches := flogoImportPattern.FindStringSubmatch(anImport)
alias, ref, version, relative := matches[2], matches[3], matches[4], matches[5]
port := app.imports.Ensure(ref, alias, version, relative)
for _, typ := range [...]string{"activity", "action", "trigger", "function", "other"} {
err := support.RegisterAlias(typ, port.Alias, port.Import)
if err != nil {
panic(err)
}
}
function.SetPackageAlias(port.Import, port.Alias)
}
function.ResolveAliases()
for id, def := range config.Schemas {
_, err := schema.Register(id, def)
if err != nil {
panic(err)
}
}
schema.ResolveSchemas()
properties := make(map[string]interface{}, len(config.Properties))
for _, attr := range config.Properties {
properties[attr.Name()] = attr.Value()
}
propertyManager := property.NewManager(properties)
property.SetDefaultManager(propertyManager)
resources := make(map[string]*resource.Resource, len(config.Resources))
app.resManager = resource.NewManager(resources)
for _, actionFactory := range action.Factories() {
err := actionFactory.Initialize(&app)
if err != nil {
panic(err)
}
}
output := "/*\n"
output += fmt.Sprintf("* Name: %s\n", config.Name)
output += fmt.Sprintf("* Type: %s\n", config.Type)
output += fmt.Sprintf("* Version: %s\n", config.Version)
output += fmt.Sprintf("* Description: %s\n", config.Description)
output += fmt.Sprintf("* AppModel: %s\n", config.AppModel)
output += "*/\n\n"
errorCheck := func() {
output += "if err != nil {\n"
output += "panic(err)\n"
output += "}\n"
}
output += "func main() {\n"
output += "var err error\n"
port := app.imports.Ensure("github.com/project-flogo/core/api")
output += fmt.Sprintf("app := %s.NewApp()\n", port.GetAlias())
for i, resConfig := range config.Resources {
resType, err := resource.GetTypeFromID(resConfig.ID)
if err != nil {
panic(err)
}
loader, generate := resource.GetLoader(resType), true
if loader != nil {
res, err := loader.LoadResource(resConfig)
if err != nil {
panic(err)
}
if g, ok := loader.(GenerateResource); ok {
generate = g.Generate()
}
resources[resConfig.ID] = res
}
if generate {
port := app.imports.Ensure("encoding/json")
output += fmt.Sprintf("resource%d := %s.RawMessage(`%s`)\n", i, port.GetAlias(), string(resConfig.Data))
output += fmt.Sprintf("app.AddResource(\"%s\", resource%d)\n", resConfig.ID, i)
}
}
if len(config.Properties) > 0 {
port := app.imports.Ensure("github.com/project-flogo/core/data")
for _, property := range config.Properties {
output += fmt.Sprintf("app.AddProperty(\"%s\", %s.%s, %#v)\n", property.Name(), port.GetAlias(),
property.Type().Name(), property.Value())
}
}
if len(config.Channels) > 0 {
port := app.imports.Ensure("github.com/project-flogo/core/engine/channels")
for i, channel := range config.Channels {
if i == 0 {
output += fmt.Sprintf("name, buffSize := %s.Decode(\"%s\")\n", port.GetAlias(), channel)
} else {
output += fmt.Sprintf("name, buffSize = %s.Decode(\"%s\")\n", port.GetAlias(), channel)
}
output += fmt.Sprintf("_, err = %s.New(name, buffSize)\n", port.GetAlias())
errorCheck()
}
}
for i, act := range config.Actions {
port := app.imports.Ensure(act.Ref)
factory, settingsName := action.GetFactory(act.Ref), fmt.Sprintf("actionSettings%d", i)
if generator, ok := factory.(Generator); ok {
code, err := generator.Generate(settingsName, &app.imports, act)
if err != nil {
panic(err)
}
output += "\n"
output += code
output += "\n"
} else {
output += fmt.Sprintf("%s := %#v\n", settingsName, act.Settings)
}
output += fmt.Sprintf("err = app.AddAction(\"%s\", &%s.Action{}, %s)\n", act.Id, port.GetAlias(), settingsName)
errorCheck()
}
for i, trigger := range config.Triggers {
port := app.imports.Ensure(trigger.Ref)
output += fmt.Sprintf("trg%d := app.NewTrigger(&%s.Trigger{}, %#v)\n", i, port.GetAlias(), trigger.Settings)
for j, handler := range trigger.Handlers {
output += fmt.Sprintf("handler%d_%d, err := trg%d.NewHandler(%#v)\n", i, j, i, handler.Settings)
errorCheck()
for k, act := range handler.Actions {
if act.Id != "" {
output += fmt.Sprintf("action%d_%d_%d, err := handler%d_%d.NewAction(\"%s\")\n", i, j, k, i, j, act.Id)
} else {
port := app.imports.Ensure(act.Ref)
factory, settingsName := action.GetFactory(act.Ref), fmt.Sprintf("settings%d_%d_%d", i, j, k)
if generator, ok := factory.(Generator); ok {
code, err := generator.Generate(settingsName, &app.imports, act.Config)
if err != nil {
panic(err)
}
output += "\n"
output += code
output += "\n"
} else {
output += fmt.Sprintf("%s := %#v\n", settingsName, act.Settings)
}
output += fmt.Sprintf("action%d_%d_%d, err := handler%d_%d.NewAction(&%s.Action{}, %s)\n", i, j, k, i, j, port.GetAlias(), settingsName)
}
errorCheck()
if act.If != "" {
output += fmt.Sprintf("action%d_%d_%d.SetCondition(\"%s\")\n", i, j, k, act.If)
}
if length := len(act.Input); length > 0 {
mappings := make([]string, 0, length)
for key, value := range act.Input {
mappings = append(mappings, fmt.Sprintf("%s%v", key, value))
}
output += fmt.Sprintf("action%d_%d_%d.SetInputMappings(%#v...)\n", i, j, k, mappings)
}
if length := len(act.Output); length > 0 {
mappings := make([]string, 0, length)
for key, value := range act.Output {
mappings = append(mappings, fmt.Sprintf("%s%v", key, value))
}
output += fmt.Sprintf("action%d_%d_%d.SetOutputMappings(%#v...)\n", i, j, k, mappings)
}
output += fmt.Sprintf("_ = action%d_%d_%d\n", i, j, k)
}
output += fmt.Sprintf("_ = handler%d_%d\n", i, j)
}
output += fmt.Sprintf("_ = trg%d\n", i)
}
port = app.imports.Ensure("github.com/project-flogo/core/api")
output += fmt.Sprintf("e, err := %s.NewEngine(app)\n", port.GetAlias())
errorCheck()
port = app.imports.Ensure("github.com/project-flogo/core/engine")
output += fmt.Sprintf("%s.RunEngine(e)\n", port.GetAlias())
output += "}\n"
header := "package main\n\n"
header += "import (\n"
for _, port := range app.imports.Imports {
if port.Alias == "nil" {
continue
}
if port.Used {
header += fmt.Sprintf("%s \"%s\"\n", port.Alias, port.Import)
continue
}
header += fmt.Sprintf("_ \"%s\"\n", port.Import)
}
header += ")\n"
output = header + output
out, err := os.Create(file)
if err != nil {
panic(err)
}
defer out.Close()
buffer := bytes.NewBufferString(output)
fileSet := token.NewFileSet()
code, err := parser.ParseFile(fileSet, file, buffer, parser.ParseComments)
if err != nil {
buffer.WriteTo(out)
panic(fmt.Errorf("%v: %v", file, err))
}
formatter := printer.Config{Mode: printer.TabIndent | printer.UseSpaces, Tabwidth: 8}
err = formatter.Fprint(out, fileSet, code)
if err != nil {
buffer.WriteTo(out)
panic(fmt.Errorf("%v: %v", file, err))
}
if modFile != "" {
hasImports := false
if len(app.imports.Imports) > 0 {
for _, port := range app.imports.Imports {
if port.Version == VersionMaster {
continue
}
hasImports = true
break
}
}
mod, err := os.Create(modFile)
if err != nil {
panic(err)
}
defer mod.Close()
fmt.Fprintf(mod, "module main\n\n")
if hasImports {
fmt.Fprintf(mod, "require (\n")
for _, port := range app.imports.Imports {
if port.Version == VersionMaster {
continue
}
fmt.Fprintf(mod, "\t%s %s\n", port.Port, port.Version)
}
fmt.Fprintf(mod, ")\n")
}
}
}