forked from ForceCLI/force
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packagebuilder.go
363 lines (320 loc) · 10.3 KB
/
packagebuilder.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
package main
import (
"encoding/xml"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)
// Structs for XML building
type Package struct {
Xmlns string `xml:"xmlns,attr"`
Types []MetaType `xml:"types"`
Version string `xml:"version"`
}
type MetaType struct {
Members []string `xml:"members"`
Name string `xml:"name"`
}
func createPackage() Package {
return Package{
Version: strings.TrimPrefix(apiVersion, "v"),
Xmlns: "http://soap.sforce.com/2006/04/metadata",
}
}
type metapath struct {
path string
name string
hasFolder bool
onlyFolder bool
extension string
}
var metapaths = []metapath{
metapath{path: "actionLinkGroupTemplates", name: "ActionLinkGroupTemplate"},
metapath{path: "analyticSnapshots", name: "AnalyticSnapshot"},
metapath{path: "applications", name: "CustomApplication"},
metapath{path: "appMenus", name: "AppMenu"},
metapath{path: "approvalProcesses", name: "ApprovalProcess"},
metapath{path: "assignmentRules", name: "AssignmentRules"},
metapath{path: "authproviders", name: "AuthProvider"},
metapath{path: "aura", name: "AuraDefinitionBundle", hasFolder: true, onlyFolder: true},
metapath{path: "autoResponseRules", name: "AutoResponseRules"},
metapath{path: "callCenters", name: "CallCenter"},
metapath{path: "cachePartitions", name: "PlatformCachePartition"},
metapath{path: "certs", name: "Certificate"},
metapath{path: "channelLayouts", name: "ChannelLayout"},
metapath{path: "classes", name: "ApexClass"},
metapath{path: "communities", name: "Community"},
metapath{path: "components", name: "ApexComponent"},
metapath{path: "connectedApps", name: "ConnectedApp"},
metapath{path: "corsWhitelistOrigins", name: "CorsWhitelistOrigin"},
metapath{path: "customApplicationComponents", name: "CustomApplicationComponent"},
metapath{path: "customMetadata", name: "CustomMetadata"},
metapath{path: "customPermissions", name: "CustomPermission"},
metapath{path: "dashboards", name: "Dashboard", hasFolder: true},
metapath{path: "dataSources", name: "ExternalDataSource"},
metapath{path: "datacategorygroups", name: "DataCategoryGroup"},
metapath{path: "delegateGroups", name: "DelegateGroup"},
metapath{path: "documents", name: "Document", hasFolder: true},
metapath{path: "EmbeddedServiceConfig", name: "EmbeddedServiceConfig"},
metapath{path: "email", name: "EmailTemplate", hasFolder: true},
metapath{path: "escalationRules", name: "EscalationRules"},
metapath{path: "feedFilters", name: "CustomFeedFilter"},
metapath{path: "flexipages", name: "FlexiPage"},
metapath{path: "flowDefinitions", name: "FlowDefinition"},
metapath{path: "flows", name: "Flow"},
metapath{path: "globalPicklists", name: "GlobalPicklist"},
metapath{path: "groups", name: "Group"},
metapath{path: "homePageComponents", name: "HomePageComponent"},
metapath{path: "homePageLayouts", name: "HomePageLayout"},
metapath{path: "installedPackages", name: "InstalledPackage"},
metapath{path: "labels", name: "CustomLabels"},
metapath{path: "layouts", name: "Layout"},
metapath{path: "LeadConvertSettings", name: "LeadConvertSettings"},
metapath{path: "letterhead", name: "Letterhead"},
metapath{path: "matchingRules", name: "MatchingRules"},
metapath{path: "namedCredentials", name: "NamedCredential"},
metapath{path: "objects", name: "CustomObject"},
metapath{path: "objectTranslations", name: "CustomObjectTranslation"},
metapath{path: "pages", name: "ApexPage"},
metapath{path: "pathAssistants", name: "PathAssistant"},
metapath{path: "permissionsets", name: "PermissionSet"},
metapath{path: "postTemplates", name: "PostTemplate"},
metapath{path: "profiles", name: "Profile", extension: ".profile"},
metapath{path: "postTemplates", name: "PostTemplate"},
metapath{path: "postTemplates", name: "PostTemplate"},
metapath{path: "profiles", name: "Profile"},
metapath{path: "queues", name: "Queue"},
metapath{path: "quickActions", name: "QuickAction"},
metapath{path: "remoteSiteSettings", name: "RemoteSiteSetting"},
metapath{path: "reports", name: "Report", hasFolder: true},
metapath{path: "reportTypes", name: "ReportType"},
metapath{path: "roles", name: "Role"},
metapath{path: "scontrols", name: "Scontrol"},
metapath{path: "settings", name: "Settings"},
metapath{path: "sharingRules", name: "SharingRules"},
metapath{path: "siteDotComSites", name: "SiteDotCom"},
metapath{path: "sites", name: "CustomSite"},
metapath{path: "staticresources", name: "StaticResource"},
metapath{path: "synonymDictionaries", name: "SynonymDictionary"},
metapath{path: "tabs", name: "CustomTab"},
metapath{path: "triggers", name: "ApexTrigger"},
metapath{path: "weblinks", name: "CustomPageWebLink"},
metapath{path: "workflows", name: "Workflow"},
metapath{path: "cspTrustedSites", name: "CspTrustedSite"},
}
type PackageBuilder struct {
IsPush bool
Metadata map[string]MetaType
Files ForceMetadataFiles
}
func NewPushBuilder() PackageBuilder {
pb := PackageBuilder{IsPush: true}
pb.Metadata = make(map[string]MetaType)
pb.Files = make(ForceMetadataFiles)
return pb
}
func NewFetchBuilder() PackageBuilder {
pb := PackageBuilder{IsPush: false}
pb.Metadata = make(map[string]MetaType)
pb.Files = make(ForceMetadataFiles)
return pb
}
// Build and return package.xml
func (pb PackageBuilder) PackageXml() []byte {
p := createPackage()
for _, metaType := range pb.Metadata {
p.Types = append(p.Types, metaType)
}
byteXml, _ := xml.MarshalIndent(p, "", " ")
byteXml = append([]byte(xml.Header), byteXml...)
//if err := ioutil.WriteFile("mypackage.xml", byteXml, 0644); err != nil {
//ErrorAndExit(err.Error())
//}
return byteXml
}
// Returns the full ForceMetadataFiles container
func (pb *PackageBuilder) ForceMetadataFiles() ForceMetadataFiles {
pb.Files["package.xml"] = pb.PackageXml()
return pb.Files
}
// Returns the source file path for a given metadata file path.
func MetaPathToSourcePath(mpath string) (spath string) {
spath = strings.TrimSuffix(mpath, "-meta.xml")
if spath == mpath {
return
}
_, err := os.Stat(spath)
if err != nil {
spath = mpath
}
return
}
// Add a file to the builder
func (pb *PackageBuilder) AddFile(fpath string) (fname string, err error) {
fpath, err = filepath.Abs(fpath)
if err != nil {
return
}
_, err = os.Stat(fpath)
if err != nil {
return
}
isDestructiveChanges, err := regexp.MatchString("destructiveChanges(Pre|Post)?"+regexp.QuoteMeta(".")+"xml", fpath)
if err != nil {
return
}
fpath = MetaPathToSourcePath(fpath)
metaName, fname := getMetaTypeFromPath(fpath)
if !isDestructiveChanges && !strings.HasSuffix(fpath, "-meta.xml") {
pb.AddMetaToPackage(metaName, fname)
}
// If it's a push, we want to actually add the files
if pb.IsPush {
if isDestructiveChanges {
err = pb.addDestructiveChanges(fpath)
} else {
err = pb.addFileToWorkingDir(metaName, fpath)
}
}
return
}
// Adds the file to a temp directory for deploy
func (pb *PackageBuilder) addFileToWorkingDir(metaName string, fpath string) (err error) {
// Get relative dir from source
srcDir := filepath.Dir(filepath.Dir(fpath))
for _, mp := range metapaths {
if metaName == mp.name && mp.hasFolder {
srcDir = filepath.Dir(srcDir)
}
}
frel, _ := filepath.Rel(srcDir, fpath)
// Try to find meta file
hasMeta := true
fmeta := fpath + "-meta.xml"
fmetarel := ""
if _, err = os.Stat(fmeta); err != nil {
if os.IsNotExist(err) {
hasMeta = false
} else {
// Has error
return
}
} else {
// Should be present since we worked back to srcDir
fmetarel, _ = filepath.Rel(srcDir, fmeta)
}
fdata, err := ioutil.ReadFile(fpath)
if err != nil {
return
}
pb.Files[frel] = fdata
if hasMeta {
fdata, err = ioutil.ReadFile(fmeta)
pb.Files[fmetarel] = fdata
return
}
return
}
func (pb *PackageBuilder) addDestructiveChanges(fpath string) (err error) {
fdata, err := ioutil.ReadFile(fpath)
if err != nil {
return
}
frel, _ := filepath.Rel(filepath.Dir(fpath), fpath)
pb.Files[frel] = fdata
return
}
func (pb *PackageBuilder) contains(members []string, name string) bool {
for _, a := range members {
if a == name {
return true
}
}
return false
}
// Adds a metadata name to the pending package
func (pb *PackageBuilder) AddMetaToPackage(metaName string, name string) {
mt := pb.Metadata[metaName]
if mt.Name == "" {
mt.Name = metaName
}
if !pb.contains(mt.Members, name) {
mt.Members = append(mt.Members, name)
pb.Metadata[metaName] = mt
}
}
// Gets metadata type name and target name from a file path
func getMetaTypeFromPath(fpath string) (metaName string, name string) {
fpath, err := filepath.Abs(fpath)
if err != nil {
ErrorAndExit("Cound not find " + fpath)
}
if _, err := os.Stat(fpath); err != nil {
ErrorAndExit("Cound not open " + fpath)
}
// Get the metadata type and name for the file
metaName, fileName := getMetaForPath(fpath)
name = strings.TrimSuffix(fileName, filepath.Ext(fileName))
//name = strings.TrimSuffix(name, filepath.Ext(name))
return
}
// Gets partial path based on a meta type name
func getPathForMeta(metaname string) string {
for _, mp := range metapaths {
if strings.EqualFold(mp.name, metaname) {
return mp.path
}
}
// Unknown, so use metaname
return metaname
}
func findMetapathForFile(file string) (path metapath) {
parentDir := filepath.Dir(file)
parentName := filepath.Base(parentDir)
grandparentName := filepath.Base(filepath.Dir(parentDir))
fileExtension := filepath.Ext(file)
for _, mp := range metapaths {
if mp.hasFolder && grandparentName == mp.path {
return mp
}
if mp.path == parentName {
return mp
}
}
// Hmm, maybe we can use the extension to determine the type
for _, mp := range metapaths {
if mp.extension == fileExtension {
return mp
}
}
return
}
// Gets meta type and name based on a path
func getMetaForPath(path string) (metaName string, objectName string) {
parentDir := filepath.Dir(path)
parentName := filepath.Base(parentDir)
grandparentName := filepath.Base(filepath.Dir(parentDir))
fileName := filepath.Base(path)
for _, mp := range metapaths {
if mp.hasFolder && grandparentName == mp.path {
metaName = mp.name
if mp.onlyFolder {
objectName = parentName
} else {
objectName = parentName + "/" + fileName
}
return
}
if mp.path == parentName {
metaName = mp.name
objectName = fileName
return
}
}
// Unknown, so use path
metaName = parentName
objectName = fileName
return
}