-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.sh
369 lines (352 loc) · 18 KB
/
build.sh
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
#/usr/bin/env bash
# -w sets workspace dir for build
# -s sets git source dir for build
# -e sets exclusions dir for build
# -b sets destination dir for build
# -d sets temporary destination dir for deletions in build
# -p sets prior commit for build (foundation of git diff command)
# -c sets current commit for build
# -t sets project type (sfdx or mdapi) for build
#default arg values
WSPACE="`pwd`"
ROOTDIR=.
SRCDIR=force-app/main/default
EXCLDIR=exclusions
BUILDDIR=staging
DESTROYDIR=toDelete
PRIORCOMMIT=HEAD~1
CURRCOMMIT=HEAD
PROJECTTYPE=sfdx
#read command line args
while getopts w:r:s:e:b:d:p:c:t: option
do
case "${option}"
in
w) WSPACE=${OPTARG};;
r) ROOTDIR=${OPTARG};;
s) SRCDIR=${OPTARG};;
e) EXCLDIR=${OPTARG};;
b) BUILDDIR=${OPTARG};;
d) DESTROYIR=${OPTARG};;
p) PRIORCOMMIT=${OPTARG};;
c) CURRCOMMIT=${OPTARG};;
t) PROJECTTYPE=${OPTARG};;
esac
done
echo Workspace: $WSPACE
echo Git Root Directory: $ROOTDIR
echo Source Directory: $SRCDIR
echo Excluded Directory: $EXCLDIR
echo Build Directory: $BUILDDIR
echo Temporary Destroy Directory: $DESTROYDIR
echo Prior Commit: $PRIORCOMMIT
echo Current Commit: $CURRCOMMIT
echo Project Type: $PROJECTTYPE
if [[ $PROJECTTYPE == "mdapi" ]]
then
SRCDIR=src
fi
cd "$WSPACE"
#echo Changing directoy to $WSPACE
if [ -d "$WSPACE/$BUILDDIR" ]
then
rm -rf $BUILDDIR
#echo Delete pre-existing $BUILDDIR directory
fi
mkdir $BUILDDIR
#echo Creating $BUILDDIR directory
if [ -d "$WSPACE/$DESTROYDIR" ]
then
rm -rf $DESTROYDIR
#echo Delete pre-existing $DESTROYDIR directory
fi
cd $ROOTDIR
#echo Changing directory to $ROOTDIR
if [ ! -z "$PRIORCOMMIT" ]
then
git diff --diff-filter=d -z --name-only $PRIORCOMMIT $CURRCOMMIT -- . ":!$EXCLDIR" -- . ":!*.*ignore" | xargs -0 -IREPLACE rsync -aR REPLACE "$WSPACE/$BUILDDIR/$ROOTDIR"
#echo Moving changed files to $BUILDDIR folder
IFS=$'\n';
if [ -d "$WSPACE/$BUILDDIR/$SRCDIR" ]
then
for f in $(find "$WSPACE/$BUILDDIR/$SRCDIR" -name '*-meta.xml')
do
BNAME=`basename $f`
BNAME="${BNAME//-meta.xml}"
DIR=`dirname $f`
DIR=${DIR##*$SRCDIR/}
if [[ $DIR == "aura/"* ]]
then
cp -Rpv "$WSPACE/$SRCDIR/$DIR" "$WSPACE/$BUILDDIR/$SRCDIR/aura/"
elif [[ $DIR == "lwc/"* ]]
then
cp -Rpv "$WSPACE/$SRCDIR/$DIR" "$WSPACE/$BUILDDIR/$SRCDIR/lwc/"
else
cp -pv "$WSPACE/$SRCDIR/$DIR/$BNAME" "$WSPACE/$BUILDDIR/$SRCDIR/$DIR/"
fi
done
#echo Moving all related components to changed metadata files and all related aura/lwc files within a changed bundle to $BUILDDIR folder
for f in $(find "$WSPACE/$BUILDDIR/$SRCDIR" -name '*.cls' -or -name '*.component' -or -name '*.page' -or -name '*.resource' -or -name '*.trigger' -or -name '*.app' -not -not -path '$SRCDIR/applications/*' -or -name '*.cmp' -or -name '*.design' -or -name '*.evt' -or -name '*.intf' -or -name '*.js' -or -name '*.svg' -or -name '*.css' -or -name '*.auradoc' -or -name '*.tokens' -or -name '*.html' -or -name '*.js-meta.xml' -or -name '*.js-meta.xml')
do
BNAME=`basename $f`
BNAME="$BNAME-meta.xml"
DIR=`dirname $f`
DIR=${DIR##*$SRCDIR/}
#echo $DIR
if [[ $DIR == "aura/"* ]]
then
cp -Rpv "$WSPACE/$SRCDIR/$DIR" "$WSPACE/$BUILDDIR/$SRCDIR/aura/"
elif [[ $DIR == "lwc/"* ]]
then
cp -Rpv "$WSPACE/$SRCDIR/$DIR" "$WSPACE/$BUILDDIR/$SRCDIR/lwc/"
else
cp -pv "$WSPACE/$SRCDIR/$DIR/$BNAME" "$WSPACE/$BUILDDIR/$SRCDIR/$DIR/"
fi
done
fi
#echo Moving all metadata files related to deleted components and all related aura/lwc files within a changed bundle to $BUILDDIR folder
if [ `git diff --diff-filter=D -z --name-only $PRIORCOMMIT $CURRCOMMIT` ]
then
mkdir $DESTROYDIR
#echo Creating $DESTROYDIR directory
rm -f deletedFiles.txt
#echo Deleting pre-existing deletedFiles.txt list
git diff --diff-filter=D -z --name-only $PRIORCOMMIT $CURRCOMMIT | xargs -0 -IDELFILE echo "DELFILE" >> deletedFiles.txt
#echo Creating new deletedFiles.txt list
IFS=$'\n';
while read CFILE
do
#echo Analyzing file `basename $CFILE`
BNAME=`basename $CFILE`
BNAME="${BNAME//-meta.xml}"
DIR=`dirname $CFILE`
DIR=${DIR##*$SRCDIR/}
#echo Directory is $DIR
if [[ -d "$SRCDIR/$DIR" && "$(ls -A $SRCDIR/$DIR)" && $DIR == "aura/"* ]]
then
#echo "Lightning component metadata has been deleted, but the component bundle still exists with files so the bundle should be incorporated for updating, not total deletion"
mkdir -p "$WSPACE/$BUILDDIR/$SRCDIR/aura/" && cp -Rpv "$WSPACE/$SRCDIR/$DIR" "$WSPACE/$BUILDDIR/$SRCDIR/aura/"
elif [[ -d "$SRCDIR/$DIR" && "$(ls -A $SRCDIR/$DIR)" && $DIR == "lwc/"* ]]
then
#echo "Lightning web component metadata has been deleted, but the component bundle still exists with files so the bundle should be incorporated for updating, not total deletion"
mkdir -p "$WSPACE/$BUILDDIR/$SRCDIR/lwc/" && cp -Rpv "$WSPACE/$SRCDIR/$DIR" "$WSPACE/$BUILDDIR/$SRCDIR/lwc/"
else
#echo "Either the metadata deleted is not part of a component bundle or the entire bundle has been deleted"
mkdir -p "$WSPACE/$DESTROYDIR/$SRCDIR/$DIR" && echo "'$BNAME is going to be deleted!'" > "$WSPACE/$DESTROYDIR/$SRCDIR/$DIR/$BNAME"
fi
done < deletedFiles.txt
else
echo "No metadata deleted"
fi
if [[ $PROJECTTYPE == "mdapi" ]]
then
read -d '' NEWPKGXML <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<Package>
</Package>
EOF
#echo ===PKGXML===
#echo $NEWPKGXML
#echo Creating new package.xml
#echo $NEWPKGXML > "$WSPACE/$BUILDDIR/$SRCDIR/package.xml"
declare -a elementArray
IFS=$'\n'
for CFILE in $(find "$WSPACE/$BUILDDIR")
do
#echo Analyzing file `basename $CFILE`
case "$CFILE"
in
*.cls*) TYPENAME="ApexClass";;
*.page*) TYPENAME="ApexPage";;
*.component*) TYPENAME="ApexComponent";;
*.trigger*) TYPENAME="ApexTrigger";;
**/aura/**/*.app*) TYPENAME="AuraDefinitionBundle";;
**/aura/**/*.cmp*) TYPENAME="AuraDefinitionBundle";;
**/aura/**/*.design*) TYPENAME="AuraDefinitionBundle";;
**/aura/**/*.evt*) TYPENAME="AuraDefinitionBundle";;
**/aura/**/*.intf*) TYPENAME="AuraDefinitionBundle";;
**/aura/**/*.tokens*) TYPENAME="AuraDefinitionBundle";;
**/lwc/**/*.css*) TYPENAME="LightningComponentBundle";;
**/lwc/**/*.html*) TYPENAME="LightningComponentBundle";;
**/lwc/**/*.js*) TYPENAME="LightningComponentBundle";;
**/lwc/**/*.js-meta.xml*) TYPENAME="LightningComponentBundle";;
**/lwc/**/*.svg*) TYPENAME="LightningComponentBundle";;
*.app*) TYPENAME="CustomApplication";;
*.labels*) TYPENAME="CustomLabels";;
*.object*) TYPENAME="CustomObject";;
**/customMetadata/*.md*) TYPENAME="CustomMetadata";;
*.tab*) TYPENAME="CustomTab";;
*.pagelayout*) TYPENAME="Layout";;
*.permissionset*) TYPENAME="PermissionSet";;
*.profile*) TYPENAME="Profile";;
*.remoteSite*) TYPENAME="RemoteSiteSettings";;
*.resource*) TYPENAME="StaticResource";;
*.workflow*) TYPENAME="Workflow";;
*) TYPENAME="UNKNOWN TYPE";;
esac
if [[ "$TYPENAME" != "UNKNOWN TYPE" ]]
then
ENTITY=$(basename "$CFILE")
ENTITY="${ENTITY%.*}"
ENTITY="${ENTITY//.*-meta*}"
#echo ENTITY NAME: $ENTITY
if grep -Fq "$TYPENAME" "$WSPACE/$BUILDDIR/$SRCDIR/package.xml"
then
if [[ ! " ${elementArray[@]} " =~ " $TYPENAME$ENTITY " ]]
then
#echo Generating new member for $ENTITY
xmlstarlet ed -L -s "/Package/types[name='$TYPENAME']" -t elem -n members -v "$ENTITY" "$WSPACE/$BUILDDIR/$SRCDIR/package.xml"
elementArray+=($TYPENAME$ENTITY)
fi
else
#echo Generating new $TYPENAME type
xmlstarlet ed -L -s /Package -t elem -n types -v "" "$WSPACE/$BUILDDIR/$SRCDIR/package.xml"
xmlstarlet ed -L -s '/Package/types[not(*)]' -t elem -n name -v "$TYPENAME" "$WSPACE/$BUILDDIR/$SRCDIR/package.xml"
#echo Generating new member for $ENTITY
xmlstarlet ed -L -s "/Package/types[name='$TYPENAME']" -t elem -n members -v "$ENTITY" "$WSPACE/$BUILDDIR/$SRCDIR/package.xml"
elementArray+=($TYPENAME$ENTITY)
fi
else
echo ERROR: UNKNOWN FILE TYPE $CFILE
fi
#echo ====UPDATED PACKAGE.XML====
cat "$WSPACE/$BUILDDIR/$SRCDIR/package.xml"
done
#echo Cleaning up Package.xml
xmlstarlet ed -L -s /Package -t elem -n version -v "45.0" "$WSPACE/$BUILDDIR/$SRCDIR/package.xml"
xmlstarlet ed -L -i /Package -t attr -n xmlns -v "http://soap.sforce.com/2006/04/metadata" "$WSPACE/$BUILDDIR/$SRCDIR/package.xml"
#echo ====FINAL PACKAGE.XML=====
#cat "$WSPACE/$BUILDDIR/$SRCDIR/package.xml"
fi
if [[ -d "$WSPACE/$DESTROYDIR" ]]
then
read -d '' NEWDESTROYXML <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<Package>
</Package>
EOF
#echo ===DESTROYXML===
#echo $NEWDESTROYXML
#echo Creating new destructiveChangesPost.xml
echo $NEWDESTROYXML > "$WSPACE/$DESTROYDIR/destructiveChangesPost.xml"
declare -a destroyElementArray
IFS=$'\n'
for CFILE in $(find "$WSPACE/$DESTROYDIR")
do
#echo Analyzing file `basename $CFILE`
case "$CFILE"
in
*.cls*) TYPENAME="ApexClass";;
*.page*) TYPENAME="ApexPage";;
*.component*) TYPENAME="ApexComponent";;
*.trigger*) TYPENAME="ApexTrigger";;
**/aura/**/*.app*) TYPENAME="AuraDefinitionBundle";;
**/aura/**/*.cmp*) TYPENAME="AuraDefinitionBundle";;
**/aura/**/*.design*) TYPENAME="AuraDefinitionBundle";;
**/aura/**/*.evt*) TYPENAME="AuraDefinitionBundle";;
**/aura/**/*.intf*) TYPENAME="AuraDefinitionBundle";;
**/aura/**/*.tokens*) TYPENAME="AuraDefinitionBundle";;
**/lwc/**/*.css*) TYPENAME="LightningComponentBundle";;
**/lwc/**/*.html*) TYPENAME="LightningComponentBundle";;
**/lwc/**/*.js*) TYPENAME="LightningComponentBundle";;
**/lwc/**/*.js-meta.xml*) TYPENAME="LightningComponentBundle";;
**/lwc/**/*.svg*) TYPENAME="LightningComponentBundle";;
*.app*) TYPENAME="CustomApplication";;
*.labels*) TYPENAME="CustomLabels";;
*.object*) TYPENAME="CustomObject";;
**/customMetadata/*.md*) TYPENAME="CustomMetadata";;
*.tab*) TYPENAME="CustomTab";;
*.pagelayout*) TYPENAME="Layout";;
*.remoteSite*) TYPENAME="RemoteSiteSettings";;
*.resource*) TYPENAME="StaticResource";;
*.workflow*) TYPENAME="Workflow";;
*) TYPENAME="UNKNOWN TYPE";;
esac
if [[ "$TYPENAME" != "UNKNOWN TYPE" ]]
then
ENTITY=$(basename "$CFILE")
ENTITY="${ENTITY%.*}"
ENTITY="${ENTITY//.*-meta*}"
#echo ENTITY NAME: $ENTITY
if grep -Fq "$TYPENAME" "$WSPACE/$DESTROYDIR/destructiveChangesPost.xml"
then
if [[ ! " ${destroyElementArray[@]} " =~ " $TYPENAME$ENTITY " ]]
then
#echo Generating new member for $ENTITY
xmlstarlet ed -L -s "/Package/types[name='$TYPENAME']" -t elem -n members -v "$ENTITY" "$WSPACE/$DESTROYDIR/destructiveChangesPost.xml"
destroyElementArray+=($TYPENAME$ENTITY)
fi
else
#echo Generating new $TYPENAME type
xmlstarlet ed -L -s /Package -t elem -n types -v "" "$WSPACE/$DESTROYDIR/destructiveChangesPost.xml"
xmlstarlet ed -L -s '/Package/types[not(*)]' -t elem -n name -v "$TYPENAME" "$WSPACE/$DESTROYDIR/destructiveChangesPost.xml"
#echo Generating new member for $ENTITY
xmlstarlet ed -L -s "/Package/types[name='$TYPENAME']" -t elem -n members -v "$ENTITY" "$WSPACE/$DESTROYDIR/destructiveChangesPost.xml"
destroyElementArray+=($TYPENAME$ENTITY)
fi
#else
#echo ERROR: UNKNOWN FILE TYPE $CFILE
fi
#echo ====UPDATED destructiveChangesPost.xml====
#cat "$WSPACE/$DESTROYDIR/destructiveChangesPost.xml"
done
#echo Cleaning up destructiveChangesPost.xml
xmlstarlet ed -L -s /Package -t elem -n version -v "45.0" "$WSPACE/$DESTROYDIR/destructiveChangesPost.xml"
xmlstarlet ed -L -i /Package -t attr -n xmlns -v "http://soap.sforce.com/2006/04/metadata" "$WSPACE/$DESTROYDIR/destructiveChangesPost.xml"
#echo ====FINAL destructiveChangesPost.xml=====
#cat "$WSPACE/$DESTROYDIR/destructiveChangesPost.xml"
else
RUNDELETE=0
fi
else
#echo No prior commit found, default Package.xml will be used
rsync -aRv --exclude="$SRCDIR/*.*ignore" --exclude="$SRCDIR/**/*.*ignore" --exclude="$SRCDIR/.git/" --exclude="$SRCDIR/**/.git/" --exclude="$SRCDIR/$EXCLDIR/" --exclude="$SRCDIR/**/$EXCLDIR/" $SRCDIR $BUILDDIR/
#echo Moving files to $BUILDDIR folder
fi
#echo Return to workspace directory
cd "$WSPACE"
if [[ $PROJECTTYPE == "mdapi" ]]
then
#Deploy to prod & run all tests
echo "Deploying to production & running all tests..."
sfdx force:mdapi:deploy -c -d $BUILDDIR -u ExtraCurricularDevHub -l RunLocalTests -w -1
else
#Convert to MDAPI format for deployment to prod
echo "Converting to MDAPI format..."
if [[ $RUNDELETE == 0 ]]
then
mkdir $DESTROYDIR
fi
if [ -d "$WSPACE/$BUILDDIR/force-app/" ]
then
sfdx force:source:convert -d deploy_prod -r $BUILDDIR/force-app/
fi
if [[ $RUNDELETE == 0 ]]
then
rm -rf $DESTROYDIR
fi
if [ -d "$WSPACE/$DESTROYDIR/force-app/" ]
then
sfdx force:source:convert -d $DESTROYDIR -r $DESTROYDIR/force-app/
fi
#Deploy to prod & run all tests
echo "Deploying to production & running all tests..."
if [ -d "$WSPACE/deploy_prod" ]
then
sfdx force:mdapi:deploy -d deploy_prod -u ExtraCurricularDevHub -l RunLocalTests -w -1
rm -rf deploy_prod
fi
if [[ $RUNDELETE != 0 ]]
then
rm -rf $DESTROYDIR/force-app/
read -d '' EMPTYPACKAGEXML <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<Package>
</Package>
EOF
echo $EMPTYPACKAGEXML > "$WSPACE/$DESTROYDIR/package.xml"
xmlstarlet ed -L -s /Package -t elem -n version -v "45.0" "$WSPACE/$DESTROYDIR/package.xml"
xmlstarlet ed -L -i /Package -t attr -n xmlns -v "http://soap.sforce.com/2006/04/metadata" "$WSPACE/$DESTROYDIR/package.xml"
sfdx force:mdapi:deploy -d $DESTROYDIR -u ExtraCurricularDevHub -l RunLocalTests -w -1
fi
fi