forked from TypedDevs/bashunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·118 lines (94 loc) · 2.02 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
#!/bin/bash
source src/check_os.sh
function build() {
local out=$1
generate_bin "$out"
generate_checksum "$out"
echo "⚡️Build completed⚡️"
}
function verify_build() {
local out=$1
echo "Verifying build ⏱️"
"$out" tests \
--simple \
--log-junit "bin/log-junit.xml" \
--report-html "bin/report.html" \
--stop-on-failure &
pid=$!
function cleanup() {
kill "$pid" 2>/dev/null
tput cnorm # Show the cursor
exit 1
}
trap cleanup SIGINT
spinner $pid
wait $pid
echo "✅ Build verified ✅ "
}
function generate_bin() {
local out=$1
local temp
temp="$(dirname "$out")/temp.sh"
echo '#!/bin/bash' > "$temp"
echo "Generating bashunit in the '$(dirname "$out")' folder..."
for file in src/*.sh; do
{
echo "# $file"
tail -n +2 "$file" >> "$temp"
echo ""
} >> "$temp"
done
cat bashunit >> "$temp"
grep -v '^source' "$temp" > "$out"
rm "$temp"
chmod u+x "$out"
}
function generate_checksum() {
local out=$1
if [[ "$_OS" == "Windows" ]]; then
return
fi
if [[ "$_OS" == "OSX" ]]; then
checksum=$(shasum -a 256 "$out")
elif [[ "$_OS" == "Linux" ]]; then
checksum=$(sha256sum "$out")
fi
echo "$checksum" > "$(dirname "$out")/checksum"
echo "$checksum"
}
function spinner() {
local pid=$1
local delay=0.1
local spinstr="|/-\\"
tput civis # Hide the cursor
printf "\r[%c] " " "
while kill -0 "$pid" 2>/dev/null; do
local temp=${spinstr#?}
printf "\r [%c] " "$spinstr"
local spinstr=$temp${spinstr%"$temp"}
sleep $delay
done
printf "\r \r" # Clear spinner
tput cnorm # Show the cursor
}
########################
######### MAIN #########
########################
DIR="bin"
SHOULD_VERIFY_BUILD=false
for arg in "$@"; do
case $arg in
-v|--verify)
SHOULD_VERIFY_BUILD=true
;;
*)
DIR=$arg
;;
esac
done
mkdir -p "$DIR"
OUT="$DIR/bashunit"
build "$OUT"
if [[ $SHOULD_VERIFY_BUILD == true ]]; then
verify_build "$OUT"
fi