-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.common
1980 lines (1680 loc) · 51.7 KB
/
.common
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
#!/bin/bash
# ................................................
# Commons 1.2.0-dev 2021-diasgc
# Definition constants and functions
# ................................................
function trap_sigint_common {
tput cnorm
cd "$(dirname $0)"
local l
[ -f "${log_file}" ] && l=" Log available at ${log_file}\n"
echo -e "\n\n${CY1} trap_sigint@common: Interrupted by user${C0}\n${l}\n"
trap - SIGINT
exit 1
}
shopt -s extglob
trap trap_sigint_common SIGINT
vsh='1.2.0-main'
#b="\u25ba"
b="\u2605"
c="\u2714"
# box chars
BX0="\u2502" BX1="\u251C" BX2="\u250C" BX3="\u2514" BX4="\u252C" BX5="\u2500"
BXH="${BX1}${BX5}${BX4}${BX5}${CY1}" BXL="${BX0} ${BX1}${BX5}${CY2}" BXE="${BX0} ${BX3}${BX5}${CY1}"
# def theme colors
C0="\e[0m" CW="\e[97m" CD="\e[90m" CW2="\e[38;5;234m" CW3="\e[38;5;238m" CW4="\e[38;5;242m" CW5="\e[38;5;246m" CW6="\e[38;5;250m"
CR0="\e[31m" CR1="\e[91m" CR2="\e[38;5;52m" CR3="\e[38;5;88m" CR4="\e[38;5;124m" CR5="\e[38;5;160m" CR6="\e[38;5;196m"
CY0="\e[33m" CY1="\e[93m" CY2="\e[38;5;58m" CY3="\e[38;5;94m" CY4="\e[38;5;136m" CY5="\e[38;5;178m" CY6="\e[38;5;220m"
CG0="\e[32m" CG1="\e[92m" CG2="\e[38;5;40m" CG3="\e[38;5;46m" CG4="\e[38;5;82m" CG5="\e[38;5;118m" CG6="\e[38;5;154m"
CC0="\e[36m" CC1="\e[96m" CC2="\e[38;5;49m" CC3="\e[38;5;85m" CC4="\e[38;5;122m" CC5="\e[38;5;123m" CC6="\e[38;5;195m"
CB0="\e[34m" CB1="\e[94m" CB2="\e[38;5;26m" CB3="\e[38;5;69m" CB4="\e[38;5;111m" CB5="\e[38;5;152m" CB6="\e[38;5;153m"
CM0="\e[35m" CM1="\e[95m" CM2="\e[38;5;54m" CM3="\e[38;5;91m" CM4="\e[38;5;126m" CM5="\e[38;5;162m" CM6="\e[38;5;198m"
CO0="\e[38;5;130m" CO1="\e[38;5;166m" CO2="\e[38;5;202m" CO3="\e[38;5;208m" CO4="\e[38;5;214m" CO5="\e[38;5;220m" CO6="\e[38;5;223m"
CF0="\e[38;5;53m" CF1="\e[38;5;89m" CF2="\e[38;5;125m" CF3="\e[38;5;161m" CF4="\e[38;5;197m" CF5="\e[38;5;211m" CF6="\e[38;5;219m"
CL0="\e[38;5;34m" CL1="\e[38;5;72m" CL2="\e[38;5;114m" CL3="\e[38;5;120m" CL4="\e[38;5;156m" CL5="\e[38;5;192m" CL6="\e[38;5;230m"
# theme colors
CT0=$CM0 CT1=$CM1 CS0=$CR0 CS1=$CR1 SSB=$C0
# Logger
logtime_start=
export dir_root=$(pwd)
: "${shell_dstack:=}"
case $(uname -v) in
*Ubuntu*)
apt_install='apt -qq install -y'
apt_search='apt search'
apt_show='apt-cache show'
export PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;95m\]\u@\e[0;95m\h\[\033[00m\]:\[\033[01;96m\]\w\[\033[00m\]\$ '
;;
*) exit_err "Unknown $(uname -v) os, please provide package-install command in .common (line 48)";;
esac
# functions
# splash and banner
function splash {
echo
printf "${CB6}THE ▀▄ ▄▀ █▀▀▄ █ █ ▀█▀ █ █▀▀▄\n"
printf "${CB1} ▄▀▄ ▀▀ █▀▀▄ █ █ █ █ █ █\n"
printf "${CB0} ▀ ▀ ▀▀▀ ▀▀▀▀ ▀▀▀ ▀▀▀ ▀▀▀ \n\n"
printf "${CW} A cross complile build system\n\n${C0}"
}
function show_banner {
echo -ne "\n\n${CW}Cross Build scripts ${vsh} for Linux${C0}\n"
[ -n "$(command -v lsb_release)" ] && echo -ne "$(lsb_release -sd) "
if [ -n "$(uname -r | grep 'microsoft')" ];then
echo -ne "WSL2 "
elif [ -n "$(uname -r | grep 'Microsoft')" ];then
echo -ne "WSL "
fi
echo -e "$(uname -o) $(uname -m) ${C0} kernel $(uname -r)"
echo -e "${CW}$(hwinfoCountCoresReadable) $(hwinfoProcessor)${C0}"
is_running=true
}
function usage {
cat ${dir_root}/README.md
}
# utils
# usage bool2int <condition>
# returns 1 for true and 0 for false
function bool2int {
$1 && echo 1 || echo 0
}
# usage bool2str <condition> <string-true> <string-false>
# returns string-true for true and string-false for false
function bool2str {
${1} && echo "${2}" || echo "${3}"
}
function safe_cd {
cd "${1}" || exit_err "Could not find directory ${1}"
}
function safe_rm {
while [ ${1} ]; do
if [ -d "${1}" ]; then
rm -rf "${1}" 2>/dev/null || exit_err "Could not delete dir ${1}"
elif [ -f "${1}" ]; then
rm -f "${1}" 2>/dev/null || exit_err "Could not delete file ${1}"
fi
shift
done
}
function safe_mkdir {
while [ ${1} ]; do
if [ ! -d "${1}" ]; then
mkdir -p "${1}" 2>/dev/null || exit_err "Could not create dir ${1}"
fi
shift
done
}
function request_cmd {
local c1=${1}
local c2=${2}
test -z "${c2}" && c2=${c1}
if [ -z $(command -v ${c1}) ]; then
${sudo} ${apt_install} ${1} -y >/dev/null 2>&1
fi
#test -z $(command -v ${c1}) && apt_install ${c2}
return 0
}
function apt_install {
while [ "$1" != "" ];do
#echo -ne " ${CT0}install $1${C0}"
#echo -n " "
stty size | {
read y x
echo -ne "${CY1}"
tput sc
tput cup "$((y - 1))" 0
$sudo apt -qq install ${1} -y >/dev/null 2>&1
echo -ne "${C0}"
tput rc
}
#echo -ne "${C0} ok"
shift
done
}
function do_quietly {
local var="${1}"; shift
echo -ne "${CD}${var}${C0}"
echo -e "\n$(date +"%T"): $@" >> "${log_file}"
"$@" >/dev/null 2>&1
logok "${var}"
}
function do_log {
local var="${1}"; shift
echo -ne "${CD}${var}${C0}"
log_this $@
logok "${var}"
}
function do_progress {
local var="${1}" && shift
echo -ne "${CD}${var}"
echo -e "\n$(date +"%T"): $@" >> "${log_file}"
("$@" |& tee -a ${log_file} | prt_progress) || exit_err "in ${var}:\n\n...\n$(tail -n5 ${log_file})${C0}"
logok "${var}"
}
function err {
if [ -n "${logtime_start}" ]; then
# consider use of ${EPOCHREALTIME/.} instead
logtime_end=$(date +%s)
echo -e "${CR1} fail ${CR0}[$(secs2time $((${logtime_end}-${logtime_start})))]${C0}\n"
fi
if [ -f "${log_file}" ];then
if [ -f "${dir_build}/CMakeFiles/CMakeError.log" ];then
echo -e "\n\n${dir_build}/CMakeFiles/CMakeError.log:\n" >> "${log_file}"
cat "${dir_build}/CMakeFiles/CMakeError.log" >> "${log_file}"
fi
echo -ne "${CD}${ind}${CY1}Open log? [Y|n]:${C0}" && read openlog
[ "${openlog}" != "n" ] && nano ${log_file}
fi
echo
exit 1
}
function exit_err {
echo -e "${CR1} Error: ${CR0}${1}${C0}\n\n"
if [ -f "${log_file}" ];then
if [ -f "${dir_build}/CMakeFiles/CMakeError.log" ];then
echo -e "\n\n${dir_build}/CMakeFiles/CMakeError.log:\n" >> "${log_file}"
cat "${dir_build}/CMakeFiles/CMakeError.log" >> "${log_file}"
fi
echo -ne "${CD}${ind}${CY1}Open log? [Y|n]:${C0}" && read openlog
[ "${openlog}" != "n" ] && nano "${log_file}"
fi
exit 1
}
function exit_info {
echo -e "${CY1} Info: ${CY0}${1}${C0}\n\n"
exit 0
}
function log {
echo -ne "$CD$@$C0"
}
function logf {
echo -e "\n$(date +"%T"): $@" >> "${log_file}"
}
function logok {
echo -ne "\e[${#1}D${CT0}${1}${C0} "
}
# usage logver /path/to/pkgconfigfile.pc
function logver {
if [ -f ${1} ]; then
echo -ne "${CT1}version $(pkg-config --modversion ${1})${C0}"
else
echo -ne "${CS0} missing ${1} ${C0}"
fi
}
function log_vars {
local t;
while [ -n "$1" ]; do
echo "$1=${!1}" >>"${log_file}" 2>&1
shift
done
echo >>"${log_file}"
}
function log_info {
echo -e "${CD}${ind}${CT0}${1}${C0} "
}
function log_start {
logtime_start=$(date +%s)
local timefmt='+%H:%M'
[ ${eta} -lt 60 ] && timefmt='+%H:%M:%S'
if ! ${inline}; then
echo -ne "${CD}${inde}${C0}$(date ${timefmt})"
[ ${eta} -gt 0 ] && echo -ne "-${CW}$(date ${timefmt} --date="${eta} seconds")"
printf " ${CT1}%-10s ${CT1}%-21s${CD} " ${lib} ${arch}
fi
echo $(date) >"${log_file}"
}
function log_end {
if ! ${inline} && [ -n "${logtime_start}" ]; then
logtime_end=$(date +%s)
local secs=$((${logtime_end}-${logtime_start}))
eta=$(secs2time ${secs})
echo -e "${CT1} done ${CD}in ${eta}"
eta="$((${secs}*$(nproc)))"
fi
}
function log_this {
echo -e "\n$(date +"%T"): $@" >> "${log_file}"
"$@" 2>> "${log_file}" 1>> "${log_file}" || err
logok
}
function log_progress {
printf "%-6s"
local sln
IFS=$'\n'
while read -r ln; do
# $has_logfile && \
printf "%-6s: %s\n" $(time_elapsed) $ln >>${logfile}
str_contains $ln 'error: ' && printf $CR1
sln=$(grep -oP '\d+%' <<< $ln)
[ -n "$sln" ] && printf "\e[5D%-5s" $sln
done
printf "\e[6D"
unset IFS
}
function log_buildvars {
printf "Build system: ${build_system}\nConfig args:\n" >>${log_file}
printf '%s\n' "${cfg_args[@]}" >>${log_file}
}
function print_vars {
while test -n "${1}"; do
printf "${CC0}%-20s: ${C0}%s\n" " ${1}" ${!1}
shift
done
}
function prt_progress {
local sln
local grp
tput civis
printf "build_system: %10s; make: %7s" ${build_system} ${MAKE_EXECUTABLE} >>${log_file}
if [ "${build_system}" == "cmake" ] && [ "$(basename ${MAKE_EXECUTABLE})" == "make" ]; then
printf "%-6s"
while read -r ln; do
str_contains "${ln}" 'error: ' && printf ${CR1}
str_contains "${ln}" 'warning: ' && printf ${CY6}
sln=$(grep -oP '\d+%' <<<${ln})
[ -n "$sln" ] && printf "\e[5D%-5s" ${sln}
done
printf "\e[6D"
elif [ "$(basename ${MAKE_EXECUTABLE})" == "ninja" ]; then
printf "%-12s"
while read -r ln; do
str_contains "${ln}" 'error: ' && printf ${CR1}
sln=$(grep -oP '\[\d+/\d+\]' <<< ${ln})
[ -n "${sln}" ] && printf "\e[11D%-11s" ${sln}
done
printf "\e[12D"
else
printf "%-6s"
while read -r ln; do
sln+="." # ·
[ ${#sln} -eq 5 ] && sln=''
[ -n "$sln" ] && printf "\e[5D%-5s" $sln
done
printf "\e[6D"
fi
tput cnorm
}
function secs2time {
[ $(($1/60%60)) -eq 0 ] && printf '%ds' $(($1%60)) || printf '%dm %ds' $(($1/60%60)) $(($1%60))
}
# usage: sourceforge_json <projectname>
sourceforge_json(){
curl -qsL "https://sourceforge.net/projects/${1}/best_release.json"
}
# MENUS SECTION
function menu_skip_options {
while [ -n "${1}" ] || [ -n "${1##--*}" ]; do
case "${1}" in
dl|git|src) skip_dl=true;;
esac
shift
done
}
color_table(){
local i=16
while [ $i -lt 52 ]; do
j=$i
k=0
until [ $k == 7 ]; do
printf "\e[38;5;%dm\u25cf %03d " ${j} ${j}
j=$(((j+36)%256))
k=$((k+1))
done
i=$((i+1))
printf '\r\n'
done
}
color_vars(){
local v
local box=
for c in R Y G C B M O F L; do
for i in 0 1 2 3 4 5 6; do
v="C${c}${i}"
printf "${!v}\u2605${v}\u2605 "
done
printf "\n"
done
echo
}
hwinfoCountCoresReadable(){
case $(nproc) in
"1") echo "Single-Core";;
"2") echo "Dual-Core";;
"4") echo "Quad-Core";;
"6") echo "Hexa-Core";;
"8") echo "Octa-Core";;
*) echo "$(nproc)-Core";;
esac
}
hwinfoProcessor(){
local i=$(cat /proc/cpuinfo | grep -Po '^.odel.*: \K(.*)' | tail -n1)
[ -z "$i" ] && i=$(cat /proc/cpuinfo | grep -Po '^.ardware.*: \K(.*)' | tail -n1)
[ -z "$i" ] && i=Unknown
echo $i
}
banner(){
echo -ne "\n\n ${CW}${1} for X-Build ${vsh}${C0}\n "
echo -ne "Running on $(uname -m) $(uname -o) "
[ -n $(command -v lsb_release) ] && echo -ne "$(lsb_release -sd) "
if [ -n "$(uname -r | grep 'microsoft')" ];then
echo -ne "${CD}WSL2 "
elif [ -n "$(uname -r | grep 'Microsoft')" ];then
echo -ne "${CD}WSL "
else
echo -ne "${CD} "
fi
echo -e "$(uname -r)${C0}"
echo -e "${CW} $(hwinfoCountCoresReadable) $(hwinfoProcessor)${C0}"
}
function fn_defined {
[ -n "${1}" ] && [ "$(type -t $1)" = 'function' ]
}
function fn_undef {
unset -f $1
}
function fn_log {
test -z "${1}" && ${2} || do_log "${1}" ${2}
unset -f $2
}
function heredoc_extract {
local file=$1
local tag=$2
awk '/^<<'"$tag"'/{flag=1; next} /^'"$tag"'/{flag=0} flag' $file
}
function heredoc_remove {
local file=$1
local tag=$2
sed -i '/'"<<${tag}"'/,/'"${tag}"'/d' $file
}
function heredoc_append {
local file=$1
local tag=$2
local string=$3
echo -e "<<$tag\n$string\n$tag" >>$file
}
array_join(){
local IFS="${1}" && shift
echo "$@"
}
#usage: str_concat 'sep' [arg_list]
str_concat(){
local IFS="$1" && shift
echo "$*"
}
# usage: str_contains str1 substr2
str_contains(){
[ -z "${1##*${2}*}" ]
}
# usage: str_starts str1 substr2
str_starts(){
[ "${1#${2}}" != "${1}" ]
}
str_ends(){
[ "${1%${2}}" != "${1}" ]
}
str_lowercase(){
echo ${1} | tr '[:upper:]' '[:lower:]'
}
str_uppercase(){
echo ${1} | tr '[:lower:]' '[:upper:]'
}
defvar(){
local k
while [ -n "${1}" ]; do
k=${1%=*}
[ -z ${k+x} ] && eval $k=${1#*=}
done
}
shell_dstack=
pushdir(){
case $SHELL in
*bash) pushd $1 >/dev/null 2>&1;;
*) pushvar_f shell_dstack $1; cd $1;;
esac
}
popdir(){
case $SHELL in
*bash) popd >/dev/null 2>&1;;
*) local d=$(popvar_f shell_dstack); [ -n "$d" ] && cd $d;;
esac
}
# usage: set_bool <condition> <value_true> <value_false>
set_bool(){
$1 && echo $2 || echo $3
}
inc_tab(){
export indent=$((indent+2))
export inds="${ind}┬─"
export inde="${ind}└─"
export ind+="│ "
#echo "inctab: $indent $ind............"
}
dec_tab(){
export indent=$((indent-2))
[ ${#ind} -gt 1 ] && export ind="${ind::-2}"
#echo "dectab: $indent $ind............"
}
# exclusive variable add substrings left var_xaddl <var> <substrings...>
# appends substrings to the left/start of var iff main doesnt contains substring
var_xaddl(){
local v=$1; shift
while [ -n "$1" ];do
[ -z "${!v##*${1}*}" ] || eval $v=\"${1} ${!v}\"
shift
done
}
# exclusive variable add substrings right var_xaddl <var> <substrings...>
# appends substrings to the right/end of var variable iff main doesnt contains substring
var_xaddr(){
local v=$1; shift
while [ -n "$1" ];do
[ -z "${!v##*${1}*}" ] || eval $v=\"${!v} ${1} \"
shift
done
}
var_addr(){
local v=$1; shift; eval $v=\"${!v} $@\"
}
var_addl(){
local v=$1; shift; eval $v=\"$@ ${!v}\"
}
# vercomp v1 v2 returns 0: v1=v2, 1: v1>v2, 2: v1<v2
vercomp () {
test "$1" = "$2" && return 0
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)); do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++)); do
test -z "${ver2[i]}" && ver2[i]=0
if ((10#${ver1[i]} > 10#${ver2[i]})); then return 1; fi
if ((10#${ver1[i]} < 10#${ver2[i]})); then return 2; fi
done
return 0
}
rm_quiet(){
rm -rf $@ 2>&1 >/dev/null
}
rm_log(){
local log=$1; shift
[ -f "${log}" ] && [ -n "$@" ] && rm -rf $@ 2>&1 >>${log}
}
time_elapsed(){
if [ -n "${log_timestart}" ] && [ ${log_timestart} -gt 0 ];then
local dt=$(($(date +%s) - ${log_timestart}))
printf "+%-10s" $(date -u -d @${dt} +"%m:%S")
else
printf " --:-- "
fi
}
url_dom(){
echo "$1" | awk -F/ '{print $3}'
}
# xbuild functions
function check_pkg_deb {
for p in $@; do
test -n $(apt search $p 2> /dev/null| grep -o "${p}") && echo "${p}" && break
done
}
function pkg_github_info {
if [ -z "${lic}" ] || [ -z "${dsc}" ];then
local gitjson="$(git_api_tojson ${src})"
if [ -n "${gitjson}" ];then
#echo -ne "${CY1}${b}${C0}"
: "${lic:=$(jq -r .licence <<<${gitjson})}"
: "${dsc:=$(jq -r .description <<<${gitjson})}"
fi
fi
}
#usage xbuild --new <github_url>
function generate_script {
test -z "${1}" && exit_err "Usage: xbuild --new <git-url>"
local giturl="${1/\.git}"
local libname="${giturl##*/}" && libname="${libname,,}"
local scriptname="${dir_root}/scripts/${libname}"
local jsonf
cp "${dir_root}/utils/script.template" ${scriptname}
chmod +x "${scriptname}"
sed -i 's,@src@,'${giturl}',g;s,@lib@,'${libname}',g' ${scriptname}
local pkgname="$(check_pkg_deb lib${libname//lib/}-dev ${libname}.-dev ${libname})"
sed -i 's,@pkg_deb@,'${pkgname}',g' ${scriptname}
jsonf="$(git_api_tojson ${giturl})"
if [ -n "${jsonf}" ]; then
test -f "${dir_root}/builds/${libname}.info" || echo ${jsonf} >"${dir_root}/builds/${libname}.info"
case ${giturl} in
*github*)
dsc="$(jq -r .description <<<${jsonf})"
lic="$(jq -r .license.name <<<${jsonf})"
;;
*gitlab*)
dsc="$(jq -r .description <<<${jsonf})"
lic="$(jq -r .license.name <<<${jsonf})"
url="$(jq -r .web_url <<<${jsonf})"
dev_bra="$(jg -r .default_branch <<<${jsonf})"
;;
esac
fi
test -n "${dsc}" && dsc="${dsc%%\. *}" && sed -i 's/@desc@/'"${dsc}"'/g' ${scriptname}
test -n "${lic}" && sed -i 's/@lic@/'"${lic}"'/g' ${scriptname}
test -n "${url}" && sed -i 's/@url@/'"${url}"'/g' ${scriptname}
test -n "${dev_bra}" && sed -i 's/@dev_bra@/'"${dev_bra}"'/g' ${scriptname}
exit_info "Done. New script generated: \e[4m${scriptname}\e[24m${C0}\n\n"
}
function github_create_info_json {
local paths=( ${1//\// } )
local url="https://api.github.com/repos/${paths[2]}/${paths[3]}"
local json="${dir_root}/builds/${libname}.info"
wget -q ${url} -O "${json}"
echo ${json}
}
function git_api_tojson {
local s=$(sed 's/https:\/\///g; s/\// /g; s/\.git$//' <<<"${1}")
set -- $s
case ${1} in
github*) echo -e "$(curl https://api.github.com/repos/${2}/${3} 2>/dev/null)";;
gitlab*) echo -e "$(curl -XGET -H "Content-Type: application/json" "https://${1}/api/v4/projects/${2}%2F${3}" 2>/dev/null)";;
*) echo;;
esac
}
function get_source {
# test internet connection
wget -q --spider http://google.com
[ ! $? -eq 0 ] && exit_err 'No Internet Connection. Aborting...'
# check whether to custom get source
if fn_defined 'source_get'; then
fn_log 'get' source_get
unset source_get
return 0
fi
# check if source is already a tarball
case "${src}" in
*.tar.gz) curl_targz "${src}" "${lib}" && return 0;;
*.tar.*|*.zip) curl_tar "${src}" "${lib}" && return 0;;
esac
# git sub command requires git to apply
test -n "${sub}" && src_rel=false
if ${src_rel}; then
if fn_defined 'on_src_release'; then
on_src_release
else
case "${src}" in
*github.com*) github_get_latest_tarball "${src}" "${lib}" && return 0;;
*gitlab.com*) gitlab_get_latest_tarball "${src}" "${lib}" && return 0;;
*googlesource.com*) googlesource_get_latest_tarball "${src}" "${lib}" && return 0;;
*.git) git_clone ${src} ${lib} ${src_opt} && return 0;;
esac
fi
elif [ -n "${git}" ]; then
git_clone "${git}" "${lib}" "${src_opt}"
return 0
elif [ -n "${svn}" ]; then
svn_clone "${svn}" "${lib}"
return 0
elif [ -n "${hg}" ]; then
hg_clone "${hg}" "${lib}"
return 0
fi
case "${src}" in
*.tar.*|*.tgz) curl_tar "${src}" "${lib}";;
*.git|*git*) git_clone "${src}" "${lib}" "${src_opt}";;
*svn.*) svn_clone "${src}" "${lib}";;
*) exit_err "Unknown source type for ${src}";;
esac
}
function github_jinfo {
local id="$(sed 's,https://github\.com/,,;s,\.git$,,' <<<${1})"
}
# usage: github_set_src_release <git_url>
# git_url: https://github.com/user/repo.git
# prefix: prefix for name of source file (default none)
# ext: extension for source file (default tar.gz)
function github_set_src_release {
local id="$(sed 's,https://github\.com/,,;s,\.git$,,' <<<${1})"
local json="$(curl -s https://api.github.com/repos/${id}/releases/latest)"
test -z "${json}" && return 0
local tarball_url=$(jq -r .tarball_url <<<${json})
if [ "${tarball_url}" = "null" ]; then
json=$(curl -sL "https://api.github.com/repos/${s}/tags")
tarball_url=$(jq -r .[0].tarball_url <<<${json})
if [ "${tarball_url}" = "null" ]; then
git_clone ${src} ${lib} ${src_opt}
return 0
fi
vrs=$(jq -r .[0].name <<<${json})
else
vrs=$(jq -r .tag_name <<<${json})
fi
dir_src="${dir_sources}/${2}"
local tag="get ${vrs}"
echo -ne "${CD}${tag}${C0}"
test -d "${dir_src}" && rm -rf "${dir_src}"
mkdir "${dir_src}"
curl -sL ${tarball_url} -w %{url_effective} 2>>"${log_file}" | tar -xz -C ${dir_src} --strip-components=1 2>>"${log_file}"
logok "${tag}"
}
# git_clone <source> <libname/destfolder> <options>
function git_clone {
local t="git"
echo -ne "${CD}${t}"
logf git clone --progress --verbose ${src} ${lib} ${src_opt}
git clone --progress --verbose ${src} ${lib} ${src_opt} |& tr '\r' '\n' | prt_git_progress
logok "${t}"
test -d "${lib}" && cd ${lib} || err
if [ -n "${bra}" ]; then
do_log ${bra} git checkout ${bra}
unset bra
elif [ -n "${vrs}" ]; then
do_log ${vrs} git checkout tags/${vrs}
fi
if [ -n "${sub}" ];then
do_log 'sub' git ${sub} |& tr '\r' '\n' | prt_git_progress
fi
vrs="$(git tag -l | tail -n1)"
echo -ne "${CT0}[${vrs}]${CD} "
cd ..
}
function prt_git_progress {
tput civis
printf "%-8s"
local s0
local s1
local n1=0
while read -r ln; do
if ! [ "${ln%\'...}" == "${ln}" ]; then
printf "\e[7D"
printf "%-40s\e[40D"
s1=${ln#*\'}
s1=${s1##*/}
s1=${s1%\'...}
printf "\e[${n1}D"
n1=${#s1}
[ ${n1} -gt 0 ] && printf " %-${n1}s" $s1
printf "%-6s"
else
s0=$(grep -oP '\d+%' <<< $ln)
[ -n "$s0" ] && printf "\e[5D%-5s" $s0
fi
done
[ $n1 -gt 0 ] && printf "\e[${n1}D"
printf "\e[7D"
tput cnorm
}
function svn_clone {
request_cmd svn subversion || err
do_log 'svn' svn checkout $1 $2
}
function hg_clone {
request_cmd hg mercurial || err
do_log 'clone' hg clone $1 $2
}
# usage: github_get_latest_tarball <git-url> <dest-dir>
# result: from the git-url, set vrs to the latest release
# and downloads + extracts tarball to dest-dir
function github_get_latest_tarball {
local s="$(sed 's,https://github\.com/,,;s,\.git$,,' <<<${1})"
local json="$(curl -s https://api.github.com/repos/${s}/releases/latest)"
local tarball_url=$(jq -r .tarball_url <<<${json})
if [ "${tarball_url}" = "null" ]; then
json=$(curl -sL "https://api.github.com/repos/${s}/tags")
tarball_url=$(jq -r .[0].tarball_url <<<${json})
if [ "${tarball_url}" = "null" ]; then
git_clone ${src} ${lib} ${src_opt}
return 0
fi
vrs=$(jq -r .[0].name <<<${json})
else
vrs=$(jq -r .tag_name <<<${json})
fi
dir_src="${dir_sources}/${2}"
local tag="get ${vrs}"
echo -ne "${CD}${tag}${C0}"
test -d "${dir_src}" && rm -rf "${dir_src}"
mkdir "${dir_src}"
curl -sL ${tarball_url} -w %{url_effective} 2>>"${log_file}" | tar -xz -C ${dir_src} --strip-components=1 2>>"${log_file}"
logok "${tag}"
}
function gitlab_get_latest_tarball {
vrs="$(git_version_remote ${src})"
src="${src%%\.git}/-/archive/${v}/${vrs}.tar.gz"
curl_tar ${src} ${lib}
}
function googlesource_get_latest_tarball {
vrs="$(git_version_remote ${1})"
src="${src%%\.git}/+archive/${vrs}.tar.gz"
curl_tar "${src}" "${lib}"
}
#usage tar_version <url-dir> <prefix>
function tar_version {
curl -sL "${1}" | grep -oP "${2}[0-9\.]+.tar.gz" | sed "s,${2},,;s,.tar.gz,," | sort -rV | head -n1
}
function curl_tar {
local tag="get"
local args=
dir_src="${dir_sources}/${2}"
echo -ne "${CD}${tag}${C0}"
echo "$(date): $@" >> "${log_file}"
case "${1}" in
*.tar.lz) request_cmd 'lzip' && args='--lzip -xv';;
*.tar.gz|*.tgz) args="-xz";;
*.tar.xz) args="-xJ";;
*.tar.bz2) args="-xj";;
esac
test -d "${dir_src}" && rm -rf "${dir_src}"
mkdir "${dir_src}"
curl_tar_simple ${1} ${args}
logok "${tag}"
}
function curl_tar_simple {
local args=(${2:=-xz})
if ${tar_stripcomponents}; then
args+=('-C' "${dir_src}" '--strip-components=1')
else
args+=("--one-top-level=${dir_src}")
fi
curl -sL "${1}" 2>>"${log_file}" | tar ${args[@]} 2>>"${log_file}"
}
function curl_targz {
local tag="get"
dir_src="${dir_sources}/${lib}"
echo -ne "${CD}${tag}${C0}"
echo "$(date): $@" >> "${log_file}"
test -d "${dir_src}" && rm -rf "${dir_src}"
mkdir "${dir_src}"
if ${tar_stripcomponents}; then
curl -sL "${1}" 2>>"${log_file}" | tar -xz -C ${dir_src} --strip-components=1 2>>"${log_file}"
else
curl -sL "${1}" 2>>"${log_file}" | tar -xz --one-top-level=${dir_src} 2>>"${log_file}"
fi
logok "${tag}"
}
function wget_pkg_tgz {
local tag="$(basename ${1})"
tag="pkg: ${tag//_${arch}.tag.gz/}"
printf "${CD}%s${C0}" ${tag}
printf "\n\n$(date): wget/untar %-50s to %s" $1 $2 >>${log_file}
wget -qO- ${1} 2>>${log_file} | tar --transform 's/^dbt2-0.37.50.3/dbt2/' -xvz -C ${2} >>${log_file} 2>&1 || err
echo -e "\n\n" >>${log_file}
logok $tag
}
# git_latest_tgz <git-url>
function git_latest_tgz {
local src="${1}"
case "${src}" in
*github.*)
local dst="$(sed 's,.git$,,' <<<${src})"
local url=$(curl -ILs -o /dev/null -w %{url_effective} "${dst}/releases/latest")
local file=$(curl -s ${url} | grep -Po '(?<=>)[^<]*' | grep -Po '.*tar.gz$')
echo "${url}/${file}"
;;
*gitlab.*|*code.videolan.org*)
local v="$(git_version_remote ${src})"
echo "${src%%\.git}/-/archive/${v}/${lib}-${v}.tar.gz"
;;
*.googlesource.*)
local tag="$(git -c 'versionsort.suffix=-' ls-remote --refs --sort='v:refname' ${src} | tail -n1 | cut -f1)"
echo "${src}/+archive/${tag}.tar.gz"
;;
esac
}
function git_version_local(){
pushd ${dir_src} > /dev/null
local v=$(git describe --tags)
local out=$(grep -oP '[0-9]+\.[0-9]+\.[0-9]+' <<<${v})
test -z "${out}" && out=$(grep -oP '[0-9]+\.[0-9]+' <<<${v})
test -z "${out}" && out=${v}
echo "${out}"
popd > /dev/null
}
function git_version_remote {
local out="$(git ls-remote --tags --refs --sort="v:refname" ${1} 2>/dev/null | tail -n1)"
echo "${out##*\/}"
}
# usage github_latest_release <user/repo id> or <git url>
function github_latest_release {