forked from ForceCLI/force
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pushAura.go
303 lines (272 loc) · 8.64 KB
/
pushAura.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
var cmdPushAura = &Command{
Usage: "pushAura",
Short: "force pushAura -resourcepath=<filepath>",
Long: `
force pushAura -resourcepath <fullFilePath>
force pushAura -f=<fullFilePath>
`,
}
func init() {
cmdPushAura.Run = runPushAura
cmdPushAura.Flag.Var(&resourcepath, "f", "fully qualified file name for entity")
cmdPushAura.Flag.StringVar(&metadataType, "t", "", "Type of entity or bundle to create")
cmdPushAura.Flag.StringVar(&metadataType, "type", "", "Type of entity or bundle to create")
}
func runPushAura(cmd *Command, args []string) {
absPath, _ := filepath.Abs(args[0])
if _, err := os.Stat(absPath); os.IsNotExist(err) {
fmt.Println(err.Error())
ErrorAndExit("File does not exist\n" + absPath)
}
// Verify that the file is in an aura bundles folder
if !InAuraBundlesFolder(absPath) {
ErrorAndExit("File is not in an aura bundle folder (aura)")
}
// See if this is a directory
info, _ := os.Stat(absPath)
if info.IsDir() {
// If this is a path, then it is expected be a direct child of "metatdata/aura".
// If so, then we are going to push all the definitions in the bundle one at a time.
filepath.Walk(absPath, func(path string, inf os.FileInfo, err error) error {
info, err = os.Stat(filepath.Join(absPath, inf.Name()))
if err != nil {
fmt.Println(err.Error())
} else {
if info.IsDir() || inf.Name() == ".manifest" {
fmt.Println("\nSkip")
} else {
pushAuraComponent(filepath.Join(absPath, inf.Name()))
}
}
return nil
})
} else {
pushAuraComponent(absPath)
}
return
}
func pushAuraComponent(fname string) {
force, _ := ActiveForce()
// Check for manifest file
if _, err := os.Stat(filepath.Join(filepath.Dir(fname), ".manifest")); os.IsNotExist(err) {
// No manifest, but is in aurabundle folder, assume creating a new bundle with this file
// as the first artifact.
createNewAuraBundleAndDefinition(*force, fname)
} else {
// Got the manifest, let's update the artifact
fmt.Println("Updating")
updateAuraDefinition(*force, fname)
return
}
}
func isValidAuraExtension(fname string) bool {
var ext = strings.Trim(strings.ToLower(filepath.Ext(fname)), " ")
if ext == ".app" || ext == ".cmp" || ext == ".evt" || ext == ".intf" {
return true
} else {
ErrorAndExit("You need to create an application (.app) or component (.cmp) or and event (.evt) as the first item in your bundle.")
}
return false
}
func createNewAuraBundleAndDefinition(force Force, fname string) {
// Creating a new bundle. We need
// the name of the bundle (parent folder of file)
// the type of artifact (based on naming convention)
// the contents of the file
if isValidAuraExtension(fname) {
// Need the parent folder name to name the bundle
var bundleName = filepath.Base(filepath.Dir(fname))
// Create the manifext
var manifest BundleManifest
manifest.Name = bundleName
_, _ = getFormatByresourcepath(fname)
targetDirectory, mdbase = SetTargetDirectory(fname)
// Create a bundle defintion
bundle, err, emessages := force.CreateAuraBundle(bundleName)
if err != nil {
if emessages[0].ErrorCode == "DUPLICATE_VALUE" {
// Should look up the bundle and get it's id then update it.
FetchManifest(bundleName)
updateAuraDefinition(force, fname)
return
}
ErrorAndExit(err.Error())
} else {
manifest.Id = bundle.Id
component, err, emessages := createBundleEntity(manifest, force, fname)
if err != nil {
ErrorAndExit(err.Error(), emessages[0].ErrorCode)
}
createManifest(manifest, component, fname)
}
}
}
func SetTargetDirectory(fname string) (dir string, base string) {
// Need to get the parent of metadata, do this by walking up the path
done := false
for done == false {
base = filepath.Base(fname)
fname = filepath.Dir(fname)
if filepath.Base(fname) == "metadata" {
dir = filepath.Dir(fname)
done = true
}
}
//return strings.Split(fname, "/metadata/aura")[0]
return
}
func createBundleEntity(manifest BundleManifest, force Force, fname string) (component ForceCreateRecordResult, err error, emessages []ForceError) {
// create the bundle entity
format, deftype := getFormatByresourcepath(fname)
mbody, _ := readFile(fname)
component, err, emessages = force.CreateAuraComponent(map[string]string{"AuraDefinitionBundleId": manifest.Id, "DefType": deftype, "Format": format, "Source": mbody})
return
}
func createManifest(manifest BundleManifest, component ForceCreateRecordResult, fname string) {
cfile := ComponentFile{}
cfile.FileName = fname
cfile.ComponentId = component.Id
manifest.Files = append(manifest.Files, cfile)
bmBody, _ := json.Marshal(manifest)
ioutil.WriteFile(filepath.Join(filepath.Dir(fname), ".manifest"), bmBody, 0644)
return
}
func updateManifest(manifest BundleManifest, component ForceCreateRecordResult, fname string) {
cfile := ComponentFile{}
cfile.FileName = fname
cfile.ComponentId = component.Id
manifest.Files = append(manifest.Files, cfile)
bmBody, _ := json.Marshal(manifest)
ioutil.WriteFile(filepath.Join(filepath.Dir(fname), ".manifest"), bmBody, 0644)
return
}
func GetManifest(fname string) (manifest BundleManifest, err error) {
manifestname := filepath.Join(filepath.Dir(fname), ".manifest")
if _, err = os.Stat(manifestname); os.IsNotExist(err) {
return
}
mbody, _ := readFile(filepath.Join(filepath.Dir(fname), ".manifest"))
json.Unmarshal([]byte(mbody), &manifest)
return
}
func updateAuraDefinition(force Force, fname string) {
//Get the manifest
manifest, err := GetManifest(fname)
for i := range manifest.Files {
component := manifest.Files[i]
if filepath.Base(component.FileName) == filepath.Base(fname) {
//Here is where we make the call to send the update
mbody, _ := readFile(fname)
err := force.UpdateAuraComponent(map[string]string{"source": mbody}, component.ComponentId)
if err != nil {
ErrorAndExit(err.Error())
}
fmt.Printf("Aura definition updated: %s\n", filepath.Base(fname))
return
}
}
component, err, emessages := createBundleEntity(manifest, force, fname)
if err != nil {
ErrorAndExit(err.Error(), emessages[0].ErrorCode)
}
updateManifest(manifest, component, fname)
fmt.Println("New component in the bundle")
}
func getFormatByresourcepath(resourcepath string) (format string, defType string) {
var fname = strings.ToLower(resourcepath)
if strings.Contains(fname, "application.app") {
format = "XML"
defType = "APPLICATION"
} else if strings.Contains(fname, ".intf") {
format = "XML"
defType = "INTERFACE"
} else if strings.Contains(fname, "component.cmp") {
format = "XML"
defType = "COMPONENT"
} else if strings.Contains(fname, "event.evt") {
format = "XML"
defType = "EVENT"
} else if strings.Contains(fname, "controller.js") {
format = "JS"
defType = "CONTROLLER"
} else if strings.Contains(fname, "model.js") {
format = "JS"
defType = "MODEL"
} else if strings.Contains(fname, "helper.js") {
format = "JS"
defType = "HELPER"
} else if strings.Contains(fname, "renderer.js") {
format = "JS"
defType = "RENDERER"
} else if strings.Contains(fname, "style.css") {
format = "CSS"
defType = "STYLE"
} else {
if filepath.Ext(fname) == ".app" {
format = "XML"
defType = "APPLICATION"
} else if filepath.Ext(fname) == ".cmp" {
format = "XML"
defType = "COMPONENT"
} else if filepath.Ext(fname) == ".evt" {
format = "XML"
defType = "EVENT"
} else if filepath.Ext(fname) == ".design" {
format = "XML"
defType = "DESIGN"
} else if filepath.Ext(fname) == ".svg" {
format = "SVG"
defType = "SVG"
} else if filepath.Ext(fname) == ".css" {
format = "CSS"
defType = "STYLE"
} else if filepath.Ext(fname) == ".intf" {
format = "XML"
defType = "INTERFACE"
} else if filepath.Ext(fname) == ".auradoc" {
format = "XML"
defType = "DOCUMENTATION"
} else {
ErrorAndExit("Could not determine aura definition type.", fname)
}
}
return
}
func getDefinitionFormat(deftype string) (result string) {
switch strings.ToUpper(deftype) {
case "APPLICATION", "COMPONENT", "EVENT", "INTERFACE", "DOCUMENTATION", "DESIGN":
result = "XML"
case "CONTROLLER", "MODEL", "HELPER", "RENDERER":
result = "JS"
case "STYLE":
result = "CSS"
case "SVG":
result = "SVG"
}
return
}
func InAuraBundlesFolder(fname string) bool {
info, _ := os.Stat(fname)
if info.IsDir() {
return strings.HasSuffix(filepath.Dir(fname), filepath.FromSlash("aura"))
} else {
return strings.HasSuffix(filepath.Dir(filepath.Dir(fname)), filepath.FromSlash("aura"))
}
}
func readFile(resourcepath string) (body string, err error) {
data, err := ioutil.ReadFile(resourcepath)
if err != nil {
return
}
body = string(data)
return
}