forked from TypedDevs/bashunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·201 lines (163 loc) · 4.24 KB
/
build.sh
File metadata and controls
executable file
·201 lines (163 loc) · 4.24 KB
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
#!/usr/bin/env 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
local out_dir
out_dir="$(dirname "$out")"
echo "Verifying build ⏱️"
BASHUNIT_BUILD_DIR="$out_dir" "$out" tests \
--simple \
--parallel \
--log-junit "$out_dir/log-junit.xml" \
--report-html "$out_dir/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 '#!/usr/bin/env 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"
# Embed the assertions.md docs into the binary
build::embed_docs "$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
local _absolute_path_pattern='^/'
if [[ ! "$sourced_file" =~ $_absolute_path_pattern ]]; 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/coverage.sh"
"src/clock.sh"
"src/state.sh"
"src/colors.sh"
"src/console_header.sh"
"src/console_results.sh"
"src/helpers.sh"
"src/test_title.sh"
"src/upgrade.sh"
"src/assertions.sh"
"src/reports.sh"
"src/runner.sh"
"src/benchmark.sh"
"src/init.sh"
"src/learn.sh"
"src/doc.sh"
"src/bashunit.sh"
"src/main.sh"
)
echo "${deps[@]}"
}
function build::embed_docs() {
local file=$1
local docs_file="docs/assertions.md"
local temp_file="${file}.tmp"
# Build the replacement content
{
# Print everything before the start marker (excluding the marker line)
local line_num
line_num=$(grep -n "# __BASHUNIT_EMBEDDED_DOCS_START__" "$file" | cut -d: -f1)
head -n "$((line_num - 1))" "$file"
# Print the heredoc with embedded docs
echo " cat <<'__BASHUNIT_DOCS_EOF__'"
cat "$docs_file"
echo "__BASHUNIT_DOCS_EOF__"
# Print everything after the end marker
sed -n '/# __BASHUNIT_EMBEDDED_DOCS_END__/,$p' "$file" | tail -n +2
} >"$temp_file"
mv "$temp_file" "$file"
chmod u+x "$file"
}
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
SHOULD_CLEANUP=false
for arg in "$@"; do
case $arg in
-v | --verify)
SHOULD_VERIFY_BUILD=true
;;
-c | --cleanup)
SHOULD_CLEANUP=true
;;
*)
DIR=$arg
;;
esac
done
mkdir -p "$DIR"
OUT="$DIR/bashunit"
build "$OUT"
if [[ $SHOULD_VERIFY_BUILD == true ]]; then
build::verify "$OUT"
fi
if [[ $SHOULD_CLEANUP == true ]]; then
echo "🧹 Cleaning up build directory: $DIR"
rm -rf "$DIR"
fi