-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
build.sh
executable file
·155 lines (124 loc) · 3.05 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
#!/bin/bash
source src/check_os.sh
BASHUNIT_ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
export BASHUNIT_ROOT_DIR
function build() {
local out=$1
build::generate_bin "$out"
build::generate_checksum "$out"
echo "⚡️ Build completed ⚡️"
}
function build::verify() {
local out=$1
echo "Verifying build ⏱️"
"$out" tests \
--simple \
--parallel \
--log-junit "bin/log-junit.xml" \
--report-html "bin/report.html" \
--stop-on-failure
# shellcheck disable=SC2181
if [[ $? -eq 0 ]]; then
echo "✅ Build verified ✅"
fi
}
function build::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 $(build::dependencies); do
build::process_file "$file" "$temp"
done
cat bashunit >> "$temp"
grep -v '^source' "$temp" > "$out"
rm "$temp"
chmod u+x "$out"
}
# Recursive function to process each file and any files it sources
function build::process_file() {
local file=$1
local temp=$2
{
echo "# $(basename "$file")"
tail -n +2 "$file" >> "$temp"
echo ""
} >> "$temp"
# Search for any 'source' lines in the current file
grep '^source ' "$file" | while read -r line; do
# Extract the path from the 'source' command
local sourced_file
sourced_file=$(echo "$line" | awk '{print $2}' | sed 's/^"//;s/"$//') # Remove any quotes
# Handle cases where the path uses $BASHUNIT_ROOT_DIR or other variables
sourced_file=$(eval echo "$sourced_file")
# Handle relative paths if necessary
if [[ ! "$sourced_file" =~ ^/ ]]; then
sourced_file="$(dirname "$file")/$sourced_file"
fi
# Recursively process the sourced file if it exists
if [[ -f "$sourced_file" ]]; then
build::process_file "$sourced_file" "$temp"
fi
done
}
function build::dependencies() {
deps=(
"src/check_os.sh"
"src/str.sh"
"src/globals.sh"
"src/dependencies.sh"
"src/io.sh"
"src/math.sh"
"src/parallel.sh"
"src/env.sh"
"src/clock.sh"
"src/state.sh"
"src/colors.sh"
"src/console_header.sh"
"src/console_results.sh"
"src/helpers.sh"
"src/upgrade.sh"
"src/assertions.sh"
"src/reports.sh"
"src/runner.sh"
"src/bashunit.sh"
"src/main.sh"
)
echo "${deps[@]}"
}
function build::generate_checksum() {
local out=$1
if [[ "$_OS" == "Windows" ]]; then
return
fi
# Use a single command for both macOS and Linux
if command -v shasum &>/dev/null; then
checksum=$(shasum -a 256 "$out")
else
checksum=$(sha256sum "$out")
fi
echo "$checksum" > "$(dirname "$out")/checksum"
echo "$checksum"
}
########################
######### 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
build::verify "$OUT"
fi