forked from ForceCLI/force
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aura.go
207 lines (184 loc) · 5.58 KB
/
aura.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// Brief comment to fire commit
var cmdAura = &Command{
Usage: "aura",
Short: "force aura push -resourcepath=<filepath>",
Long: `
The aura command needs context to work. If you execute "aura get"
it will create a folder structure that provides the context for
aura components on disk.
The aura components will be created in "metadata/aurabundles/<componentname>"
relative to the current working directory and a .manifest file will be
created that associates components and their artifacts with their ids in
the database.
To create a new component (application, evt or component), create a new
folder under "aura". Then create a new file in your new folder. You
must follow a naming convention for your files to enable proper definition
of the component type.
Naming convention <compnentName><artifact type>.<file type extension>
Examples: metadata
aura
MyApp
MyAppApplication.app
MyAppStyle.css
MyList
MyComponent.cmp
MyComponentHelper.js
MyComponentStyle.css
force aura push -f <fullFilePath> -b <bundle name>
force aura create -t=<entity type> <entityName>
force aura delete -f=<fullFilePath>
force aura list
`,
}
func init() {
cmdAura.Run = runAura
cmdAura.Flag.Var(&resourcepath, "p", "fully qualified file name for entity")
cmdAura.Flag.Var(&resourcepath, "f", "fully qualified file name for entity")
cmdAura.Flag.StringVar(&metadataType, "entitytype", "", "fully qualified file name for entity")
cmdAura.Flag.StringVar(&auraentityname, "entityname", "", "fully qualified file name for entity")
cmdAura.Flag.StringVar(&metadataType, "t", "", "fully qualified file name for entity")
cmdAura.Flag.StringVar(&auraentityname, "n", "", "fully qualified file name for entity")
}
var (
auraentityname string
)
func runAura(cmd *Command, args []string) {
if err := cmd.Flag.Parse(args[0:]); err != nil {
os.Exit(2)
}
force, _ := ActiveForce()
subcommand := args[0]
// Sublime hack - the way sublime passes parameters seems to
// break the flag parsing by sending a single element array
// for the args. ARGH!!!
if strings.HasPrefix(subcommand, "delete ") || strings.HasPrefix(subcommand, "push ") {
what := strings.Split(subcommand, " ")
if err := cmd.Flag.Parse(what[1:]); err != nil {
ErrorAndExit(err.Error())
}
subcommand = what[0]
} else {
if err := cmd.Flag.Parse(args[1:]); err != nil {
ErrorAndExit(err.Error())
}
}
switch strings.ToLower(subcommand) {
case "create":
/*if *auraentitytype == "" || *auraentityname == "" {
fmt.Println("Must specify entity type and name")
os.Exit(2)
}*/
ErrorAndExit("force aura create not yet implemented")
case "delete":
runDeleteAura()
case "list":
bundles, err := force.GetAuraBundlesList()
if err != nil {
ErrorAndExit(err.Error())
}
for _, bundle := range bundles.Records {
fmt.Println(bundle["DeveloperName"])
}
case "push":
// absPath, _ := filepath.Abs(resourcepath[0])
runPushAura(cmd, resourcepath)
}
}
func runDeleteAura() {
absPath, _ := filepath.Abs(resourcepath[0])
//resourcepath = absPath
if InAuraBundlesFolder(absPath) {
info, err := os.Stat(absPath)
if err != nil {
ErrorAndExit(err.Error())
}
manifest, err := GetManifest(absPath)
isBundle := false
if info.IsDir() {
force, _ := ActiveForce()
manifest, err = GetManifest(filepath.Join(absPath, ".manifest"))
bid := ""
if err != nil { // Could not find a manifest, use bundle name
// Try to look up the bundle by name
b, err := force.GetAuraBundleByName(filepath.Base(absPath))
if err != nil {
ErrorAndExit(err.Error())
} else {
if len(b.Records) == 0 {
ErrorAndExit(fmt.Sprintf("No bundle definition named %q", filepath.Base(absPath)))
} else {
bid = b.Records[0]["Id"].(string)
}
}
} else {
bid = manifest.Id
}
err = force.DeleteToolingRecord("AuraDefinitionBundle", bid)
if err != nil {
ErrorAndExit(err.Error())
}
// Now walk the bundle and remove all the atrifacts
filepath.Walk(absPath, func(path string, inf os.FileInfo, err error) error {
os.Remove(path)
return nil
})
os.Remove(absPath)
fmt.Println("Bundle ", filepath.Base(absPath), " deleted.")
return
}
for key := range manifest.Files {
mfile := manifest.Files[key].FileName
cfile := absPath
if !filepath.IsAbs(mfile) {
cfile = filepath.Base(cfile)
}
if isBundle {
if !filepath.IsAbs(mfile) {
cfile = filepath.Join(absPath, mfile)
} else {
cfile = mfile
deleteAuraDefinition(manifest, key)
}
} else {
if mfile == cfile {
deleteAuraDefinition(manifest, key)
return
}
}
}
if isBundle {
// Need to remove the bundle using the id in the manifest
deleteAuraDefinitionBundle(manifest)
}
}
}
func deleteAuraDefinitionBundle(manifest BundleManifest) {
force, err := ActiveForce()
err = force.DeleteToolingRecord("AuraDefinitionBundle", manifest.Id)
if err != nil {
ErrorAndExit(err.Error())
}
os.Remove(filepath.Join(resourcepath[0], ".manifest"))
os.Remove(resourcepath[0])
}
func deleteAuraDefinition(manifest BundleManifest, key int) {
force, err := ActiveForce()
err = force.DeleteToolingRecord("AuraDefinition", manifest.Files[key].ComponentId)
if err != nil {
ErrorAndExit(err.Error())
}
fname := manifest.Files[key].FileName
os.Remove(fname)
manifest.Files = append(manifest.Files[:key], manifest.Files[key+1:]...)
bmBody, _ := json.Marshal(manifest)
ioutil.WriteFile(filepath.Join(filepath.Dir(fname), ".manifest"), bmBody, 0644)
}