forked from BigWigsMods/packager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.sh
executable file
·3019 lines (2775 loc) · 107 KB
/
release.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# release.sh generates an addon zip file from a Git, SVN, or Mercurial checkout.
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>
# Global shellcheck excludes:
# SC2295: Expansions inside ${..} need to be quoted separately, otherwise they will match as a pattern.
# SC2030: Modification of var is local (to subshell caused by pipeline).
# SC2031: var was modified in a subshell. That change might be lost.
# SC2001: See if you can use ${variable//search/replace} instead.
# shellcheck disable=SC2295,SC2030,SC2031,SC2001
## USER OPTIONS
# Secrets for uploading
cf_token=
github_token=
wowi_token=
wago_token=
# Variables set via command-line options
slug=
addonid=
wagoid=
topdir=
releasedir=
overwrite=
nolib=
split=
line_ending="dos"
skip_copying=
skip_externals=
skip_localization=
skip_zipfile=
skip_upload=
skip_cf_upload=
pkgmeta_file=
game_version=
game_type=
file_type=
file_template="{package-name}-{project-version}{nolib}{classic}"
label_template="{project-version}{classic}{nolib}"
wowi_markup="bbcode"
## END USER OPTIONS
if [[ ${BASH_VERSINFO[0]} -lt 4 ]] || [[ ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -lt 3 ]]; then
echo "ERROR! bash version 4.3 or above is required. Your version is ${BASH_VERSION}." >&2
exit 1
fi
# Game versions for uploading
declare -A game_flavor=( ["retail"]="retail" ["classic"]="classic" ["bcc"]="bcc" ["mainline"]="retail" ["tbc"]="bcc" ["vanilla"]="classic" ["wrath"]="wrath" ["wotlkc"]="wrath" )
declare -A game_type_version=() # type -> version
declare -A game_type_interface=() # type -> toc
declare -A si_game_type_interface_all=() # type -> toc (last file)
declare -A si_game_type_interface=() # type -> game type toc (last file)
declare -A toc_interfaces=() # path -> all toc interface values (: delim)
declare -A toc_root_interface=() # path -> base interface value
declare -A toc_root_paths=() # path -> directory name
declare -a toc_paths=() # toc path in order of processing
# Script return code
exit_code=0
retry() {
local result=0
local count=1
local max=10
while [[ "$count" -le "$max" ]]; do
[[ "$result" -ne 0 ]] && {
echo -e "\033[01;31mRetrying (${count}/${max})\033[0m" >&2
}
"$@" && { result=0 && break; } || result="$?"
count="$((count + 1))"
sleep 3
done
return "$result"
}
# Escape a string for use in sed substitutions.
escape_substr() {
local s="$1"
s=${s//\\/\\\\}
s=${s//\//\\/}
s=${s//&/\\&}
echo "$s"
}
# File name templating
filename_filter() {
local classic alpha beta invalid="_"
[ -n "$skip_invalid" ] && invalid="&"
if [[ -n $game_type ]] && [[ "$game_type" != "retail" ]] && \
[[ "$game_type" != "classic" || "${si_project_version,,}" != *"-classic"* ]] &&\
[[ "$game_type" != "bcc" || "${si_project_version,,}" != *"-bcc"* ]] &&\
[[ "$game_type" != "wrath" || "${si_project_version,,}" != *"-wrath"* ]]
then
# only append the game type if the tag doesn't include it
classic="-$game_type"
fi
[ "$file_type" == "alpha" ] && alpha="-alpha"
[ "$file_type" == "beta" ] && beta="-beta"
sed \
-e "s/{package-name}/$( escape_substr "$package" )/g" \
-e "s/{project-revision}/$si_project_revision/g" \
-e "s/{project-hash}/$si_project_hash/g" \
-e "s/{project-abbreviated-hash}/$si_project_abbreviated_hash/g" \
-e "s/{project-author}/$( escape_substr "$si_project_author" )/g" \
-e "s/{project-date-iso}/$si_project_date_iso/g" \
-e "s/{project-date-integer}/$si_project_date_integer/g" \
-e "s/{project-timestamp}/$si_project_timestamp/g" \
-e "s/{project-version}/$( escape_substr "$si_project_version" )/g" \
-e "s/{game-type}/${game_type}/g" \
-e "s/{release-type}/${file_type}/g" \
-e "s/{alpha}/${alpha}/g" \
-e "s/{beta}/${beta}/g" \
-e "s/{nolib}/${nolib:+-nolib}/g" \
-e "s/{classic}/${classic}/g" \
-e "s/\([^A-Za-z0-9._-]\)/${invalid}/g" \
<<< "$1"
}
toc_filter() {
local keyword="$1"
local remove="$2"
if [ -z "$remove" ]; then
# "active" build type: remove comments (keep content), remove non-blocks (remove all)
sed \
-e "/#@\(end-\)\{0,1\}${keyword}@/d" \
-e "/#@non-${keyword}@/,/#@end-non-${keyword}@/d"
else
# "non" build type: remove blocks (remove content), uncomment non-blocks (remove tags)
sed \
-e "/#@${keyword}@/,/#@end-${keyword}@/d" \
-e "/#@non-${keyword}@/,/#@end-non-${keyword}@/s/^#[[:blank:]]\{1,\}//" \
-e "/#@\(end-\)\{0,1\}non-${keyword}@/d"
fi
}
toc_to_type() {
local toc_version="$1"
local -n game_type="$2" || return 1
case $toc_version in
11???) game_type="classic" ;;
20???) game_type="bcc" ;;
30???) game_type="wrath" ;;
*) game_type="retail"
esac
# return game_type
}
# Process command-line options
usage() {
cat <<-'EOF' >&2
Usage: release.sh [options]
-c Skip copying files into the package directory.
-d Skip uploading.
-e Skip checkout of external repositories.
-l Skip @localization@ keyword replacement.
-L Only do @localization@ keyword replacement (skip upload to CurseForge).
-o Keep existing package directory, overwriting its contents.
-s Create a stripped-down "nolib" package.
-S Create a package supporting multiple game types from a single TOC file.
-u Use Unix line-endings.
-z Skip zip file creation.
-t topdir Set top-level directory of checkout.
-r releasedir Set directory containing the package directory. Defaults to "$topdir/.release".
-p curse-id Set the project id used on CurseForge for localization and uploading. (Use 0 to unset the TOC value)
-w wowi-id Set the addon id used on WoWInterface for uploading. (Use 0 to unset the TOC value)
-a wago-id Set the project id used on Wago Addons for uploading. (Use 0 to unset the TOC value)
-g game-version Set the game version to use for uploading.
-m pkgmeta.yaml Set the pkgmeta file to use.
-n "{template}" Set the package zip file name and upload label. Use "-n help" for more info.
EOF
}
OPTIND=1
while getopts ":celLzusSop:dw:a:r:t:g:m:n:" opt; do
case $opt in
c) skip_copying="true" ;; # Skip copying files into the package directory
z) skip_zipfile="true" ;; # Skip creating a zip file
e) skip_externals="true" ;; # Skip checkout of external repositories
l) skip_localization="true" ;; # Skip @localization@ keyword replacement
L) skip_cf_upload="true" ;; # Skip uploading to CurseForge
d) skip_upload="true" ;; # Skip uploading
u) line_ending="unix" ;; # Use LF instead of CRLF as the line ending for all text files
o) overwrite="true" ;; # Don't delete existing directories in the release directory
p) slug="$OPTARG" ;; # Set CurseForge project id
w) addonid="$OPTARG" ;; # Set WoWInterface addon id
a) wagoid="$OPTARG" ;; # Set Wago Addons project id
r) releasedir="$OPTARG" ;; # Set the release directory
t) # Set the top-level directory of the checkout
if [ ! -d "$OPTARG" ]; then
echo "Invalid argument for option \"-t\" - Directory \"$OPTARG\" does not exist." >&2
usage
exit 1
fi
topdir="$OPTARG"
;;
s) # Create a nolib package without externals
nolib="true"
skip_externals="true"
;;
S) split="true" ;; # Split TOC
g) # Set the game type or version
OPTARG="${OPTARG,,}"
case "$OPTARG" in
retail|classic|bcc|wrath) game_type="$OPTARG" ;; # game_version from toc
mainline) game_type="retail" ;;
*)
# Set game version (x.y.z)
# Build game type set from the last value if a list
IFS=',' read -ra V <<< "$OPTARG"
for i in "${V[@]}"; do
if [[ ! "$i" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)[a-z]?$ ]]; then
echo "Invalid argument for option \"-g\" ($i)" >&2
usage
exit 1
fi
if [[ ${BASH_REMATCH[1]} == "1" ]]; then
game_type="classic"
elif [[ ${BASH_REMATCH[1]} == "2" ]]; then
game_type="bcc"
elif [[ ${BASH_REMATCH[1]} == "3" ]]; then
game_type="wrath"
else
game_type="retail"
fi
# Only one version per game type is allowed
if [ -n "${game_type_version[$game_type]}" ]; then
echo "Invalid argument for option \"-g\" (${game_type_version[$game_type]}, $i) - Only one version per game type is supported." >&2
usage
exit 1
fi
game_type_version[$game_type]="$i"
game_type_interface[$game_type]=$( printf "%d%02d%02d" "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}" )
done
if [[ ${#game_type_version[@]} -gt 1 ]]; then
game_type=
fi
game_version="$OPTARG"
esac
;;
m) # Set the pkgmeta file
if [ ! -f "$OPTARG" ]; then
echo "Invalid argument for option \"-m\" - File \"$OPTARG\" does not exist." >&2
usage
exit 1
fi
pkgmeta_file="$OPTARG"
;;
n) # Set the package file name
if [ "$OPTARG" = "help" ]; then
cat <<-'EOF' >&2
Usage: release.sh [options]
Set the package zip file name and upload file label. There are several string
substitutions you can use to include version control and build type infomation in
the file name and upload label.
The default file name is "{package-name}-{project-version}{nolib}{classic}".
The default upload label is "{project-version}{classic}{nolib}".
To set both, seperate with a ":", i.e, "{file template}:{label template}".
If either side of the ":" is blank, the default will be used.Not including a ":"
will set the file name template, leaving upload label as default.
Tokens: {package-name}{project-revision}{project-hash}{project-abbreviated-hash}
{project-author}{project-date-iso}{project-date-integer}{project-timestamp}
{project-version}{game-type}{release-type}
Flags: {alpha}{beta}{nolib}{classic}
Tokens are always replaced with their value. Flags are shown prefixed with a dash
depending on the build type.
EOF
exit 0
fi
if skip_invalid=true filename_filter "$OPTARG" | grep -q '[{}]'; then
tokens=$( skip_invalid=true filename_filter "$OPTARG" | sed -e '/^[^{]*{\|}[^{]*{\|}[^{]*/s//}{/g' -e 's/^}\({.*}\){$/\1/' )
echo "Invalid argument for option \"-n\" - Invalid substitutions: $tokens" >&2
exit 1
fi
file_template=${OPTARG%%:*}
if [ -z "$file_template" ]; then
file_template="{package-name}-{project-version}{nolib}{classic}"
fi
label_template=${OPTARG##*:}
if [ -z "$label_template" ]; then
label_template="{project-version}{classic}{nolib}"
fi
#"{package-name}-{project-version}{nolib}{classic}:{project-version}{classic}{nolib}"
;;
:)
echo "Option \"-$OPTARG\" requires an argument." >&2
usage
exit 1
;;
\?)
if [ "$OPTARG" = "?" ] || [ "$OPTARG" = "h" ]; then
usage
exit 0
fi
echo "Unknown option \"-$OPTARG\"" >&2
usage
exit 1
;;
esac
done
shift $((OPTIND - 1))
# Set $topdir to top-level directory of the checkout.
if [ -z "$topdir" ]; then
dir=$( pwd )
if [ -d "$dir/.git" ] || [ -d "$dir/.svn" ] || [ -d "$dir/.hg" ]; then
topdir=.
else
dir=${dir%/*}
topdir=".."
while [ -n "$dir" ]; do
if [ -d "$topdir/.git" ] || [ -d "$topdir/.svn" ] || [ -d "$topdir/.hg" ]; then
break
fi
dir=${dir%/*}
topdir="$topdir/.."
done
if [ ! -d "$topdir/.git" ] && [ ! -d "$topdir/.svn" ] && [ ! -d "$topdir/.hg" ]; then
echo "No Git, SVN, or Hg checkout found." >&2
exit 1
fi
fi
fi
# Handle folding sections in CI logs
start_group() { echo "$1"; }
end_group() { echo; }
# Check for Travis CI
if [ -n "$TRAVIS" ]; then
# Don't run the packager for pull requests
if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then
echo "Not packaging pull request."
exit 0
fi
if [ -z "$TRAVIS_TAG" ]; then
# Don't run the packager if there is a tag pending
check_tag=$( git -C "$topdir" tag --points-at HEAD )
if [ -n "$check_tag" ]; then
echo "Found future tag \"${check_tag}\", not packaging."
exit 0
fi
# Only package master, classic, or develop
if [ "$TRAVIS_BRANCH" != "master" ] && [ "$TRAVIS_BRANCH" != "classic" ] && [ "$TRAVIS_BRANCH" != "develop" ]; then
echo "Not packaging \"${TRAVIS_BRANCH}\"."
exit 0
fi
fi
# https://github.com/travis-ci/travis-build/tree/master/lib/travis/build/bash
start_group() {
echo -en "travis_fold:start:$2\\r\033[0K"
# release_timer_id="$(printf %08x $((RANDOM * RANDOM)))"
# release_timer_start_time="$(date -u +%s%N)"
# echo -en "travis_time:start:${release_timer_id}\\r\033[0K"
echo "$1"
}
end_group() {
# local release_timer_end_time="$(date -u +%s%N)"
# local duration=$((release_timer_end_time - release_timer_start_time))
# echo -en "\\ntravis_time:end:${release_timer_id}:start=${release_timer_start_time},finish=${release_timer_end_time},duration=${duration}\\r\033[0K"
echo -en "travis_fold:end:$1\\r\033[0K"
}
fi
# Check for GitHub Actions
if [[ -n $GITHUB_ACTIONS ]]; then
# Prevent duplicate builds from multiple pushes
if [[ $GITHUB_EVENT_NAME == "push" && $GITHUB_REF == "refs/heads"* ]]; then
check_tag=$( git -C "$topdir" tag --points-at HEAD )
if [[ -n $check_tag ]]; then
echo "Found future tag \"${check_tag}\", not packaging."
exit 0
fi
unset check_tag
fi
start_group() { echo "##[group]$1"; }
end_group() { echo "##[endgroup]"; }
fi
# Load secrets
if [ -f "$topdir/.env" ]; then
# shellcheck disable=SC1090,SC1091
. "$topdir/.env"
elif [ -f ".env" ]; then
# shellcheck disable=SC1091
. ".env"
fi
[ -z "$cf_token" ] && cf_token=$CF_API_KEY
[ -z "$github_token" ] && github_token=$GITHUB_OAUTH
[ -z "$wowi_token" ] && wowi_token=$WOWI_API_TOKEN
[ -z "$wago_token" ] && wago_token=$WAGO_API_TOKEN
# Set $releasedir to the directory which will contain the generated addon zipfile.
if [ -z "$releasedir" ]; then
releasedir="$topdir/.release"
fi
# Set $basedir to the basename of the checkout directory.
basedir=$( cd "$topdir" && pwd )
case $basedir in
/*/*) basedir=${basedir##/*/} ;;
/*) basedir=${basedir##/} ;;
esac
# Set $repository_type to "git" or "svn" or "hg".
repository_type=
if [ -d "$topdir/.git" ]; then
repository_type=git
elif [ -d "$topdir/.svn" ]; then
repository_type=svn
elif [ -d "$topdir/.hg" ]; then
repository_type=hg
else
echo "No Git, SVN, or Hg checkout found in \"$topdir\"." >&2
exit 1
fi
# $releasedir must be an absolute path or inside $topdir.
case $releasedir in
/*) ;;
$topdir/*) ;;
*)
echo "The release directory \"$releasedir\" must be an absolute path or inside \"$topdir\"." >&2
exit 1
;;
esac
# Create the staging directory.
mkdir -p "$releasedir" 2>/dev/null || {
echo "Unable to create the release directory \"$releasedir\"." >&2
exit 1
}
# Expand $topdir and $releasedir to their absolute paths for string comparisons later.
topdir=$( cd "$topdir" && pwd )
releasedir=$( cd "$releasedir" && pwd )
###
### set_info_<repo> returns the following information:
###
si_repo_type= # "git" or "svn" or "hg"
si_repo_dir= # the checkout directory
si_repo_url= # the checkout url
si_tag= # tag for HEAD
si_previous_tag= # previous tag
si_previous_revision= # [SVN|Hg] revision number for previous tag
si_project_revision= # Turns into the highest revision of the entire project in integer form, e.g. 1234, for SVN. Turns into the commit count for the project's hash for Git.
si_project_hash= # [Git|Hg] Turns into the hash of the entire project in hex form. e.g. 106c634df4b3dd4691bf24e148a23e9af35165ea
si_project_abbreviated_hash= # [Git|Hg] Turns into the abbreviated hash of the entire project in hex form. e.g. 106c63f
si_project_author= # Turns into the last author of the entire project. e.g. ckknight
si_project_date_iso= # Turns into the last changed date (by UTC) of the entire project in ISO 8601. e.g. 2008-05-01T12:34:56Z
si_project_date_integer= # Turns into the last changed date (by UTC) of the entire project in a readable integer fashion. e.g. 2008050123456
si_project_timestamp= # Turns into the last changed date (by UTC) of the entire project in POSIX timestamp. e.g. 1209663296
si_project_version= # Turns into an approximate version of the project. The tag name if on a tag, otherwise it's up to the repo. SVN returns something like "r1234", Git returns something like "v0.1-873fc1"
si_file_revision= # Turns into the current revision of the file in integer form, e.g. 1234, for SVN. Turns into the commit count for the file's hash for Git.
si_file_hash= # Turns into the hash of the file in hex form. e.g. 106c634df4b3dd4691bf24e148a23e9af35165ea
si_file_abbreviated_hash= # Turns into the abbreviated hash of the file in hex form. e.g. 106c63
si_file_author= # Turns into the last author of the file. e.g. ckknight
si_file_date_iso= # Turns into the last changed date (by UTC) of the file in ISO 8601. e.g. 2008-05-01T12:34:56Z
si_file_date_integer= # Turns into the last changed date (by UTC) of the file in a readable integer fashion. e.g. 20080501123456
si_file_timestamp= # Turns into the last changed date (by UTC) of the file in POSIX timestamp. e.g. 1209663296
si_build_timestamp=$( date "+%s" )
si_build_date=$( TZ='' printf "%(%Y-%m-%d)T" "$si_build_timestamp" )
si_build_date_iso=$( TZ='' printf "%(%Y-%m-%dT%H:%M:%SZ)T" "$si_build_timestamp" )
si_build_date_integer=$( TZ='' printf "%(%Y%m%d%H%M%S)T" "$si_build_timestamp" )
# SVN date helper function
strtotime() {
local value="$1" # datetime string
local format="$2" # strptime string
if [[ "$OSTYPE" == "darwin"* || "$OSTYPE" == *"bsd"* ]]; then # bsd
date -j -f "$format" "$value" "+%s" 2>/dev/null
else # gnu
date -d "$value" +%s 2>/dev/null
fi
}
set_info_git() {
si_repo_dir="$1"
si_repo_type="git"
si_repo_url=$( git -C "$si_repo_dir" remote get-url origin 2>/dev/null | sed -e 's/^git@\(.*\):/https:\/\/\1\//' )
if [ -z "$si_repo_url" ]; then # no origin so grab the first fetch url
si_repo_url=$( git -C "$si_repo_dir" remote -v | awk '/(fetch)/ { print $2; exit }' | sed -e 's/^git@\(.*\):/https:\/\/\1\//' )
fi
# Populate filter vars.
si_project_hash=$( git -C "$si_repo_dir" show --no-patch --format="%H" 2>/dev/null )
si_project_abbreviated_hash=$( git -C "$si_repo_dir" show --no-patch --abbrev=7 --format="%h" 2>/dev/null )
si_project_author=$( git -C "$si_repo_dir" show --no-patch --format="%an" 2>/dev/null )
si_project_timestamp=$( git -C "$si_repo_dir" show --no-patch --format="%at" 2>/dev/null )
si_project_date_iso=$( TZ='' printf "%(%Y-%m-%dT%H:%M:%SZ)T" "$si_project_timestamp" )
si_project_date_integer=$( TZ='' printf "%(%Y%m%d%H%M%S)T" "$si_project_timestamp" )
# XXX --depth limits rev-list :\ [ -s "$si_repo_dir/$(git rev-parse --git-dir)/shallow" ] && git -C "$si_repo_dir" fetch --quiet --unshallow --no-tags
# actions/checkout using clone --filter=blob:none instead of their current unholy abomination would be nice
si_project_revision=$( git -C "$si_repo_dir" rev-list --count "$si_project_hash" 2>/dev/null )
# Get the tag for the HEAD.
si_previous_tag=
si_previous_revision=
_si_tag=$( git -C "$si_repo_dir" describe --tags --always --abbrev=7 2>/dev/null )
si_tag=$( git -C "$si_repo_dir" describe --tags --always --abbrev=0 2>/dev/null )
# Set $si_project_version to the version number of HEAD. May be empty if there are no commits.
si_project_version=$si_tag
# The HEAD is not tagged if the HEAD is several commits past the most recent tag.
if [ "$si_tag" = "$si_project_hash" ]; then
# --abbrev=0 expands out the full sha if there was no previous tag
si_project_version=$_si_tag
si_previous_tag=
si_tag=
elif [ "$_si_tag" != "$si_tag" ]; then
# not on a tag
si_project_version=$( git -C "$si_repo_dir" describe --tags --abbrev=7 --exclude="*[Aa][Ll][Pp][Hh][Aa]*" 2>/dev/null )
si_previous_tag=$( git -C "$si_repo_dir" describe --tags --abbrev=0 --exclude="*[Aa][Ll][Pp][Hh][Aa]*" 2>/dev/null )
si_tag=
else # we're on a tag, just jump back one commit
if [[ ${si_tag,,} != *"beta"* && ${si_tag,,} != *"alpha"* ]]; then
# full release, ignore beta tags
si_previous_tag=$( git -C "$si_repo_dir" describe --tags --abbrev=0 --exclude="*[Aa][Ll][Pp][Hh][Aa]*" --exclude="*[Bb][Ee][Tt][Aa]*" HEAD~ 2>/dev/null )
else
si_previous_tag=$( git -C "$si_repo_dir" describe --tags --abbrev=0 --exclude="*[Aa][Ll][Pp][Hh][Aa]*" HEAD~ 2>/dev/null )
fi
fi
}
set_info_svn() {
si_repo_dir="$1"
si_repo_type="svn"
local _si_svninfo _si_url _si_revision _si_tag_line _si_tag _si_tag_from_revision _si_timestamp
_si_svninfo=$( retry svn info -r BASE "$si_repo_dir" 2>/dev/null )
if [[ -z "$_si_svninfo" ]]; then
echo "Unable to read svn repo at $si_repo_dir" >&2
exit 1
fi
si_repo_url=$( awk '/^Repository Root:/ { print $3; exit }' <<< "$_si_svninfo" )
_si_url=$( awk '/^URL:/ { print $2; exit }' <<< "$_si_svninfo" )
_si_revision=$( awk '/^Last Changed Rev:/ { print $NF; exit }' <<< "$_si_svninfo" )
case ${_si_url#"${si_repo_url}"/} in
tags/*)
# Extract the tag from the URL.
si_tag=${_si_url#"${si_repo_url}"/tags/}
si_tag=${si_tag%%/*}
si_project_revision="$_si_revision"
;;
*)
# Check if the latest tag matches the working copy revision (/trunk checkout instead of /tags)
_si_tag_line=$( retry svn log --verbose --limit 1 "$si_repo_url/tags" 2>/dev/null | awk '/^ A/ { print $0; exit }' )
_si_tag=$( echo "$_si_tag_line" | awk '/^ A/ { print $2 }' | awk -F/ '{ print $NF }' )
_si_tag_from_revision=$( echo "$_si_tag_line" | sed -e 's/^.*:\([0-9]\{1,\}\)).*$/\1/' ) # (from /project/trunk:N)
if [[ "$_si_tag_from_revision" == "$_si_revision" ]]; then
si_tag="$_si_tag"
si_project_revision=$( retry svn info "$si_repo_url/tags/$si_tag" 2>/dev/null | awk '/^Last Changed Rev:/ { print $NF; exit }' )
else
# Set $si_project_revision to the highest revision of the project at the checkout path
si_project_revision=$( svn info --recursive "$si_repo_dir" 2>/dev/null | awk '/^Last Changed Rev:/ { print $NF }' | sort -nr | head -n1 )
fi
;;
esac
if [[ -n "$si_tag" ]]; then
si_project_version="$si_tag"
else
si_project_version="r$si_project_revision"
fi
# Get the previous tag and it's revision
_si_tag=$( retry svn log --verbose --limit 1 "$si_repo_url/tags" -r $((si_project_revision - 1)):1 2>/dev/null | awk '/^ A/ { print $0; exit }' | awk '/^ A/ { print $2 }' | awk -F/ '{ print $NF }' )
if [[ -n "$_si_tag" ]]; then
si_previous_tag="$_si_tag"
si_previous_revision=$( retry svn info "$si_repo_url/tags/$_si_tag" 2>/dev/null | awk '/^Last Changed Rev:/ { print $NF; exit }' )
fi
# Populate filter vars.
si_project_author=$( awk '/^Last Changed Author:/ { print $0; exit }' <<< "$_si_svninfo" | cut -d" " -f4- )
_si_timestamp=$( awk '/^Last Changed Date:/ { print $4,$5; exit }' <<< "$_si_svninfo" )
si_project_timestamp=$( strtotime "$_si_timestamp" "%F %T" )
si_project_date_iso=$( TZ='' printf "%(%Y-%m-%dT%H:%M:%SZ)T" "$si_project_timestamp" )
si_project_date_integer=$( TZ='' printf "%(%Y%m%d%H%M%S)T" "$si_project_timestamp" )
# SVN repositories have no project hash.
si_project_hash=
si_project_abbreviated_hash=
}
set_info_hg() {
si_repo_dir="$1"
si_repo_type="hg"
si_repo_url=$( hg --cwd "$si_repo_dir" paths -q default )
if [ -z "$si_repo_url" ]; then # no default so grab the first path
si_repo_url=$( hg --cwd "$si_repo_dir" paths | awk '{ print $3; exit }' )
fi
# Populate filter vars.
si_project_hash=$( hg --cwd "$si_repo_dir" log -r . --template '{node}' 2>/dev/null )
si_project_abbreviated_hash=$( hg --cwd "$si_repo_dir" log -r . --template '{node|short}' 2>/dev/null )
si_project_author=$( hg --cwd "$si_repo_dir" log -r . --template '{author}' 2>/dev/null )
si_project_timestamp=$( hg --cwd "$si_repo_dir" log -r . --template '{date}' 2>/dev/null | cut -d. -f1 )
si_project_date_iso=$( TZ='' printf "%(%Y-%m-%dT%H:%M:%SZ)T" "$si_project_timestamp" )
si_project_date_integer=$( TZ='' printf "%(%Y%m%d%H%M%S)T" "$si_project_timestamp" )
si_project_revision=$( hg --cwd "$si_repo_dir" log -r . --template '{rev}' 2>/dev/null )
# Get tag info
si_tag=
# I'm just muddling through revsets, so there is probably a better way to do this
# Ignore tag commits, so v1.0-1 will package as v1.0
if [ "$( hg --cwd "$si_repo_dir" log -r '.-filelog(.hgtags)' --template '{rev}' 2>/dev/null )" == "" ]; then
_si_tip=$( hg --cwd "$si_repo_dir" log -r 'last(parents(.))' --template '{rev}' 2>/dev/null )
else
_si_tip=$( hg --cwd "$si_repo_dir" log -r . --template '{rev}' 2>/dev/null )
fi
si_previous_tag=$( hg --cwd "$si_repo_dir" log -r "$_si_tip" --template '{latesttag}' 2>/dev/null )
# si_project_version=$( hg --cwd "$si_repo_dir" log -r "$_si_tip" --template "{ ifeq(changessincelatesttag, 0, latesttag, '{latesttag}-{changessincelatesttag}-m{node|short}') }" 2>/dev/null ) # git style
si_project_version=$( hg --cwd "$si_repo_dir" log -r "$_si_tip" --template "{ ifeq(changessincelatesttag, 0, latesttag, 'r{rev}') }" 2>/dev/null ) # svn style
if [ "$si_previous_tag" = "$si_project_version" ]; then
# we're on a tag
si_tag=$si_previous_tag
si_previous_tag=$( hg --cwd "$si_repo_dir" log -r "last(parents($_si_tip))" --template '{latesttag}' 2>/dev/null )
fi
si_previous_revision=$( hg --cwd "$si_repo_dir" log -r "$si_previous_tag" --template '{rev}' 2>/dev/null )
}
set_info_file() {
local _si_file="$1"
if [[ $si_repo_type == "git" ]]; then
local _si_file_dir=${_si_file%/*} # set the working directory to file's directory to allow for submodule file info
_si_file=${_si_file##*/} # just need the filename
# Populate filter vars from the last commit the file was included in.
si_file_author=$( git -C "$_si_file_dir" log --max-count=1 --format="%an" -- "$_si_file" 2>/dev/null )
si_file_timestamp=$( git -C "$_si_file_dir" log --max-count=1 --format="%at" -- "$_si_file" 2>/dev/null )
si_file_revision=$( git -C "$_si_file_dir" rev-list --count "$si_file_hash" 2>/dev/null ) # XXX checkout depth affects rev-list, see set_info_git
si_file_hash=$( git -C "$_si_file_dir" log --max-count=1 --format="%H" -- "$_si_file" 2>/dev/null )
si_file_abbreviated_hash=$( git -C "$_si_file_dir" log --max-count=1 --abbrev=7 --format="%h" -- "$_si_file" 2>/dev/null )
elif [[ $si_repo_type == "svn" ]]; then
local _sif_svninfo _si_timestamp
_sif_svninfo=$( svn info "$_si_file" 2>/dev/null )
if [[ -n $_sif_svninfo ]]; then
# Populate filter vars.
si_file_author=$( awk '/^Last Changed Author:/ { print $0; exit }' <<< "$_sif_svninfo" | cut -d" " -f4- )
_si_timestamp=$( awk '/^Last Changed Date:/ { print $4,$5,$6; exit }' <<< "$_sif_svninfo" )
si_file_timestamp=$( strtotime "$_si_timestamp" "%F %T %z" )
si_file_revision=$( awk '/^Last Changed Rev:/ { print $NF; exit }' <<< "$_sif_svninfo" )
# Use the file checksum.
si_file_hash=$( awk '/^Checksum:/ { print $2; exit }' <<< "$_sif_svninfo" )
si_file_abbreviated_hash=${si_file_hash:0:7}
else
si_file_author=
si_file_timestamp=
si_file_revision=
si_file_hash=
si_file_abbreviated_hash=
si_file_date_iso=
si_file_date_integer=
fi
elif [[ $si_repo_type == "hg" ]]; then
# Populate filter vars.
si_file_author=$( hg log --limit 1 --template '{author}' "$_si_file" 2>/dev/null )
si_file_timestamp=$( hg log --limit 1 --template '{date}' "$_si_file" 2>/dev/null | cut -d. -f1 )
si_file_revision=$( hg log --limit 1 --template '{rev}' "$_si_file" 2>/dev/null )
si_file_hash=$( hg log --limit 1 --template '{node}' "$_si_file" 2>/dev/null )
si_file_abbreviated_hash=$( hg log --limit 1 --template '{node|short}' "$_si_file" 2>/dev/null )
fi
if [[ -n $si_file_timestamp ]]; then
si_file_date_iso=$( TZ='' printf "%(%Y-%m-%dT%H:%M:%SZ)T" "$si_file_timestamp" )
si_file_date_integer=$( TZ='' printf "%(%Y%m%d%H%M%S)T" "$si_file_timestamp" )
fi
}
# Set some version info about the project
case $repository_type in
git) set_info_git "$topdir" ;;
svn) set_info_svn "$topdir" ;;
hg) set_info_hg "$topdir" ;;
esac
tag=$si_tag
project_version=$si_project_version
previous_version=$si_previous_tag
project_hash=$si_project_hash
project_revision=$si_project_revision
previous_revision=$si_previous_revision
project_timestamp=$si_project_timestamp
project_github_url=
project_github_slug=
if [[ "$si_repo_url" == "https://github.com"* ]]; then
project_github_url=${si_repo_url%.git}
project_github_slug=${project_github_url#https://github.com/}
fi
project_site=
# Automatic file type detection based on CurseForge rules
# 1) Untagged commits will be marked as an alpha.
# 2) Tagged commits will be marked as a release with the following exceptions:
# - If the tag contains the word "alpha", it will be marked as an alpha file.
# - If instead the tag contains the word "beta", it will be marked as a beta file.
if [ -n "$tag" ]; then
if [[ "${tag,,}" == *"alpha"* ]]; then
file_type="alpha"
elif [[ "${tag,,}" == *"beta"* ]]; then
file_type="beta"
else
file_type="release"
fi
else
file_type="alpha"
fi
# Bare carriage-return character.
carriage_return=$( printf "\r" )
# Returns 0 if $1 matches one of the colon-separated patterns in $2.
match_pattern() {
local _mp_file="$1"
local _mp_list="$2:"
local _mp_pattern
while [ -n "$_mp_list" ]; do
_mp_pattern=${_mp_list%%:*}
_mp_list=${_mp_list#*:}
# shellcheck disable=SC2254
case $_mp_file in
$_mp_pattern)
return 0
;;
esac
done
return 1
}
# Simple .pkgmeta YAML processor.
declare -A yaml_bool=( ["yes"]="yes" ["true"]="yes" ["on"]="yes" ["false"]="no" ["off"]="no" ["no"]="no" )
yaml_keyvalue() {
yaml_key=${1%%:*}
yaml_value=${1#"$yaml_key":}
yaml_value=${yaml_value#"${yaml_value%%[! ]*}"} # trim leading whitespace
if [[ -n "$yaml_value" && -n "${yaml_bool[${yaml_value,,}]}" ]]; then # normalize booleans
yaml_value="${yaml_bool[$yaml_value]}"
fi
yaml_value=${yaml_value#[\'\"]} # trim leading quotes
yaml_value=${yaml_value%[\'\"]} # trim trailing quotes
}
yaml_listitem() {
yaml_item=${1#-}
yaml_item=${yaml_item#"${yaml_item%%[! ]*}"} # trim leading whitespace
yaml_item=${yaml_item#[\'\"]} # trim leading quotes
yaml_item=${yaml_item%[\'\"]} # trim trailing quotes
}
###
### Process .pkgmeta to set variables used later in the script.
###
if [ -z "$pkgmeta_file" ]; then
pkgmeta_file="$topdir/.pkgmeta"
# CurseForge allows this so check for it
if [ ! -f "$pkgmeta_file" ] && [ -f "$topdir/pkgmeta.yaml" ]; then
pkgmeta_file="$topdir/pkgmeta.yaml"
fi
fi
# Variables set via .pkgmeta.
package=
project=
manual_changelog=
changelog=
changelog_markup="text"
enable_nolib_creation=
ignore=
license=
unchanged=
zip_root_dirs=()
nolib_exclude=
wowi_gen_changelog="true"
wowi_archive="true"
wowi_convert_changelog="true"
declare -A relations=()
parse_ignore() {
pkgmeta="$1"
[ -f "$pkgmeta" ] || return 1
checkpath="$topdir" # paths are relative to the topdir
copypath=""
if [ "$2" != "" ]; then
checkpath=$( dirname "$pkgmeta" )
copypath="$2/"
fi
yaml_eof=
while [ -z "$yaml_eof" ]; do
IFS='' read -r yaml_line || yaml_eof="true"
# Skip commented out lines.
if [[ $yaml_line =~ ^[[:space:]]*\# ]]; then
continue
fi
# Strip any trailing CR character.
yaml_line=${yaml_line%$carriage_return}
case $yaml_line in
[!\ ]*:*)
# Split $yaml_line into a $yaml_key, $yaml_value pair.
yaml_keyvalue "$yaml_line"
# Set the $pkgmeta_phase for stateful processing.
pkgmeta_phase=$yaml_key
;;
[\ ]*"- "*)
yaml_line=${yaml_line#"${yaml_line%%[! ]*}"} # trim leading whitespace
# Get the YAML list item.
yaml_listitem "$yaml_line"
if [[ "$pkgmeta_phase" == "ignore" || "$pkgmeta_phase" == "plain-copy" ]]; then
pattern=$yaml_item
if [ -d "$checkpath/$pattern" ]; then
pattern="$copypath$pattern/*"
elif [ ! -f "$checkpath/$pattern" ]; then
# doesn't exist so match both a file and a path
pattern="$copypath$pattern:$copypath$pattern/*"
else
pattern="$copypath$pattern"
fi
if [[ "$pkgmeta_phase" == "ignore" ]]; then
if [ -z "$ignore" ]; then
ignore="$pattern"
else
ignore="$ignore:$pattern"
fi
elif [[ "$pkgmeta_phase" == "plain-copy" ]]; then
if [ -z "$unchanged" ]; then
unchanged="$pattern"
else
unchanged="$unchanged:$pattern"
fi
fi
fi
;;
esac
done < "$pkgmeta"
}
if [ -f "$pkgmeta_file" ]; then
if grep -q $'^[ ]*\t\+[[:blank:]]*[[:graph:]]' "$pkgmeta_file"; then
# Try to cut down on some troubleshooting pain.
echo "ERROR! Your pkgmeta file contains a leading tab. Only spaces are allowed for indentation in YAML files." >&2
grep -n $'^[ ]*\t\+[[:blank:]]*[[:graph:]]' "$pkgmeta_file" | sed $'s/\t/^I/g'
exit 1
fi
yaml_eof=
while [ -z "$yaml_eof" ]; do
IFS='' read -r yaml_line || yaml_eof="true"
# Skip commented out lines.
if [[ $yaml_line =~ ^[[:space:]]*\# ]]; then
continue
fi
# Strip any trailing CR character.
yaml_line=${yaml_line%$carriage_return}
case $yaml_line in
[!\ ]*:*)
# Split $yaml_line into a $yaml_key, $yaml_value pair.
yaml_keyvalue "$yaml_line"
# Set the $pkgmeta_phase for stateful processing.
pkgmeta_phase=$yaml_key
case $yaml_key in
enable-nolib-creation)
if [ "$yaml_value" = "yes" ]; then
enable_nolib_creation="true"
fi
;;
enable-toc-creation)
if [ "$yaml_value" = "yes" ]; then
split="true"
fi
;;
license-output)
license=$yaml_value
;;
manual-changelog)
changelog=$yaml_value
manual_changelog="true"
;;
changelog-title)
project="$yaml_value"
;;
package-as)
package=$yaml_value
;;
wowi-create-changelog)
if [ "$yaml_value" = "no" ]; then
wowi_gen_changelog=
fi
;;
wowi-convert-changelog)
if [ "$yaml_value" = "no" ]; then
wowi_convert_changelog=
fi
;;
wowi-archive-previous)
if [ "$yaml_value" = "no" ]; then
wowi_archive=
fi
;;
esac
;;
" "*)
yaml_line=${yaml_line#"${yaml_line%%[! ]*}"} # trim leading whitespace
case $yaml_line in
"- "*)
# Get the YAML list item.
yaml_listitem "$yaml_line"
case $pkgmeta_phase in
tools-used)
relations["$yaml_item"]="tool"
;;
required-dependencies)
relations["$yaml_item"]="requiredDependency"
;;
optional-dependencies)
relations["$yaml_item"]="optionalDependency"
;;
embedded-libraries)
relations["$yaml_item"]="embeddedLibrary"
;;
esac
;;
*:*)
# Split $yaml_line into a $yaml_key, $yaml_value pair.
yaml_keyvalue "$yaml_line"
case $pkgmeta_phase in
manual-changelog)
case $yaml_key in
filename)
changelog=$yaml_value
manual_changelog="true"
;;
markup-type)
if [ "$yaml_value" = "markdown" ] || [ "$yaml_value" = "html" ]; then
changelog_markup=$yaml_value
else
changelog_markup="text"
fi
;;
esac
;;
move-folders)
# Save project root directories
if [[ $yaml_value != *"/"* ]]; then
_mf_path="${yaml_key#*/}" # strip the package name
toc_root_paths["$topdir/$_mf_path"]="$yaml_value"
fi
;;
esac
;;
esac
;;
esac