-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathcreate.go
355 lines (277 loc) · 7.06 KB
/
create.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
package api
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"os/exec"
"github.com/project-flogo/cli/common"
"github.com/project-flogo/cli/util"
)
var fileSampleEngineMain = filepath.Join("examples", "engine", "main.go")
func CreateProject(basePath, appName, appCfgPath, coreVersion string) (common.AppProject, error) {
var err error
var appJson string
if appCfgPath != "" {
if util.IsRemote(appCfgPath) {
appJson, err = util.LoadRemoteFile(appCfgPath)
if err != nil {
return nil, fmt.Errorf("unable to load remote app file '%s' - %s", appCfgPath, err.Error())
}
} else {
appJson, err = util.LoadLocalFile(appCfgPath)
if err != nil {
return nil, fmt.Errorf("unable to load app file '%s' - %s", appCfgPath, err.Error())
}
}
} else {
if len(appName) == 0 {
return nil, fmt.Errorf("app name not specified")
}
}
appName, err = getAppName(appName, appJson)
if err != nil {
return nil, err
}
fmt.Printf("Creating Flogo App: %s\n", appName)
appDir, err := createAppDirectory(basePath, appName)
if err != nil {
return nil, err
}
srcDir := filepath.Join(appDir, "src")
dm := util.NewDepManager(srcDir)
if Verbose() {
fmt.Printf("Setting up app directory: %s\n", appDir)
}
err = setupAppDirectory(dm, appDir, coreVersion)
if err != nil {
return nil, err
}
if Verbose() {
if appJson == "" {
fmt.Println("Adding sample flogo.json")
}
}
err = createAppJson(dm, appDir, appName, appJson)
if err != nil {
return nil, err
}
err = createMain(dm, appDir)
if err != nil {
return nil, err
}
project := NewAppProject(appDir)
if Verbose() {
fmt.Println("Importing Dependencies...")
}
err = importDependencies(project)
if err != nil {
return nil, err
}
if Verbose() {
fmt.Printf("Created App: %s\n", appName)
}
return project, nil
}
// createAppDirectory creates the flogo app directory
func createAppDirectory(basePath, appName string) (string, error) {
var err error
if basePath == "." {
basePath, err = os.Getwd()
if err != nil {
return "", err
}
}
appPath := filepath.Join(basePath, appName)
err = os.Mkdir(appPath, os.ModePerm)
if err != nil {
return "", err
}
return appPath, nil
}
//setupAppDirectory sets up the flogo app directory
func setupAppDirectory(dm util.DepManager, appPath, coreVersion string) error {
err := os.Mkdir(filepath.Join(appPath, dirBin), os.ModePerm)
if err != nil {
return err
}
srcDir := filepath.Join(appPath, dirSrc)
err = os.Mkdir(srcDir, os.ModePerm)
if err != nil {
return err
}
_, err = os.Create(filepath.Join(srcDir, fileImportsGo))
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Join(srcDir, fileImportsGo), []byte("package main\n"), 0644)
if err != nil {
return err
}
err = dm.Init()
if err != nil {
return err
}
flogoCoreImport := util.NewFlogoImport(flogoCoreRepo, "", coreVersion, "")
//todo get the actual version installed from the go.mod file
if coreVersion == "" {
fmt.Printf("Installing: %s@latest\n", flogoCoreImport.CanonicalImport())
} else {
fmt.Printf("Installing: %s\n", flogoCoreImport.CanonicalImport())
}
// add & fetch the core library
err = dm.AddDependency(flogoCoreImport)
if err != nil {
return err
}
return nil
}
// createAppJson create the flogo app json
func createAppJson(dm util.DepManager, appDir, appName, appJson string) error {
updatedJson, err := getAndUpdateAppJson(dm, appName, appJson)
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Join(appDir, fileFlogoJson), []byte(updatedJson), 0644)
if err != nil {
return err
}
return nil
}
// importDependencies import all dependencies
func importDependencies(project common.AppProject) error {
ai, err := util.GetAppImports(filepath.Join(project.Dir(), fileFlogoJson), project.DepManager(), true)
if err != nil {
return err
}
imports := ai.GetAllImports()
if len(imports) == 0 {
return nil
}
err = project.AddImports(true, false, imports...)
if err != nil {
return err
}
legacySupportRequired := false
for _, details := range ai.GetAllImportDetails() {
path, err := project.GetPath(details.Imp)
if err != nil {
return err
}
desc, err := util.GetContribDescriptor(path)
if err != nil {
return err
}
if desc != nil {
cType := desc.GetContribType()
if desc.IsLegacy {
legacySupportRequired = true
cType = "legacy " + desc.GetContribType()
err := CreateLegacyMetadata(path, desc.GetContribType(), details.Imp.GoImportPath())
if err != nil {
return err
}
}
fmt.Printf("Installed %s: %s\n", cType, details.Imp)
//instStr := fmt.Sprintf("Installed %s:", cType)
//fmt.Printf("%-20s %s\n", instStr, imp)
}
}
if Verbose() {
fmt.Printf("Tidying go mod...")
}
err = util.ExecCmd(exec.Command("go", "mod", "tidy"), project.SrcDir())
if err != nil {
fmt.Printf("Failed to clean deps: %s\n", err)
}
if legacySupportRequired {
err := InstallLegacySupport(project)
return err
}
return nil
}
func createMain(dm util.DepManager, appDir string) error {
flogoCoreImport, err := util.NewFlogoImportFromPath(flogoCoreRepo)
if err != nil {
return err
}
corePath, err := dm.GetPath(flogoCoreImport)
if err != nil {
return err
}
bytes, err := ioutil.ReadFile(filepath.Join(corePath, fileSampleEngineMain))
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Join(appDir, dirSrc, fileMainGo), bytes, 0644)
if err != nil {
return err
}
return nil
}
func getAndUpdateAppJson(dm util.DepManager, appName, appJson string) (string, error) {
if len(appJson) == 0 {
appJson = emptyFlogoJson
}
descriptor, err := util.ParseAppDescriptor(appJson)
if err != nil {
return "", err
}
if appName != "" {
// override the application name
altJson := strings.Replace(appJson, `"`+descriptor.Name+`"`, `"`+appName+`"`, 1)
altDescriptor, err := util.ParseAppDescriptor(altJson)
//see if we can get away with simple replace so we don't reorder the existing json
if err == nil && altDescriptor.Name == appName {
appJson = altJson
} else {
//simple replace didn't work so we have to unmarshal & re-marshal the supplied json
var appObj map[string]interface{}
err := json.Unmarshal([]byte(appJson), &appObj)
if err != nil {
return "", err
}
appObj["name"] = appName
updApp, err := json.MarshalIndent(appObj, "", " ")
if err != nil {
return "", err
}
appJson = string(updApp)
}
descriptor.Name = appName
} else {
appName = descriptor.Name
}
return appJson, nil
}
func getAppName(appName, appJson string) (string, error) {
if appJson != "" && appName == "" {
descriptor, err := util.ParseAppDescriptor(appJson)
if err != nil {
return "", err
}
return descriptor.Name, nil
}
return appName, nil
}
func GetTempDir() (string, error) {
tempDir, err := ioutil.TempDir("", "flogo")
if err != nil {
return "", err
}
return tempDir, nil
}
var emptyFlogoJson = `
{
"name": "{{.AppName}}",
"type": "flogo:app",
"version": "0.0.1",
"description": "My Flogo Application Description",
"appModel": "1.1.0",
"imports": [],
"triggers": [],
"resources":[]
}
`