Skip to content

Commit a21a341

Browse files
authored
Merge pull request #289 from TypedDevs/feat/improve-build
Improve build
2 parents dddb6c7 + f89a393 commit a21a341

File tree

2 files changed

+120
-43
lines changed

2 files changed

+120
-43
lines changed

build.sh

Lines changed: 93 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,117 @@
22

33
source src/check_os.sh
44

5+
function build() {
6+
local out=$1
7+
8+
generate_bin "$out"
9+
generate_checksum "$out"
10+
11+
echo "⚡️Build completed⚡️"
12+
}
13+
14+
function verify_build() {
15+
local out=$1
16+
17+
echo "Verifying build ⏱️"
18+
19+
"$out" tests \
20+
--simple \
21+
--log-junit "bin/log-junit.xml" \
22+
--report-html "bin/report.html" \
23+
--stop-on-failure &
24+
25+
pid=$!
26+
27+
function cleanup() {
28+
kill "$pid" 2>/dev/null
29+
tput cnorm # Show the cursor
30+
exit 1
31+
}
32+
33+
trap cleanup SIGINT
34+
spinner $pid
35+
wait $pid
36+
echo "✅ Build verified ✅ "
37+
}
38+
539
function generate_bin() {
6-
local output_file=$1
7-
echo '#!/usr/bin/env bash' > bin/temp.sh
8-
9-
echo "Generating bashunit in the 'bin' folder..."
10-
cat src/*.sh >> bin/temp.sh
11-
cat bashunit >> bin/temp.sh
12-
grep -v '^source' bin/temp.sh > "$output_file"
13-
rm bin/temp.sh
14-
chmod u+x "$output_file"
40+
local out=$1
41+
local temp
42+
temp="$(dirname "$out")/temp.sh"
43+
44+
echo '#!/bin/bash' > "$temp"
45+
echo "Generating bashunit in the '$(dirname "$out")' folder..."
46+
for file in src/*.sh; do
47+
{
48+
echo "# $file"
49+
tail -n +2 "$file" >> "$temp"
50+
echo ""
51+
} >> "$temp"
52+
done
53+
54+
cat bashunit >> "$temp"
55+
grep -v '^source' "$temp" > "$out"
56+
rm "$temp"
57+
chmod u+x "$out"
1558
}
1659

1760
function generate_checksum() {
61+
local out=$1
62+
1863
if [[ "$_OS" == "Windows" ]]; then
1964
return
2065
fi
2166

22-
local file=$1
2367
if [[ "$_OS" == "OSX" ]]; then
24-
checksum=$(shasum -a 256 "$file")
68+
checksum=$(shasum -a 256 "$out")
2569
elif [[ "$_OS" == "Linux" ]]; then
26-
checksum=$(sha256sum "$file")
70+
checksum=$(sha256sum "$out")
2771
fi
2872

29-
echo "$checksum" > bin/checksum
73+
echo "$checksum" > "$(dirname "$out")/checksum"
3074
echo "$checksum"
3175
}
3276

77+
function spinner() {
78+
local pid=$1
79+
local delay=0.1
80+
local spinstr="|/-\\"
81+
tput civis # Hide the cursor
82+
printf "\r[%c] " " "
83+
while kill -0 "$pid" 2>/dev/null; do
84+
local temp=${spinstr#?}
85+
printf "\r [%c] " "$spinstr"
86+
local spinstr=$temp${spinstr%"$temp"}
87+
sleep $delay
88+
done
89+
printf "\r \r" # Clear spinner
90+
tput cnorm # Show the cursor
91+
}
92+
3393
########################
3494
######### MAIN #########
3595
########################
3696

37-
mkdir -p bin
38-
output_file="bin/bashunit"
97+
DIR="bin"
98+
SHOULD_VERIFY_BUILD=true
99+
100+
for arg in "$@"; do
101+
case $arg in
102+
--ignore-verify)
103+
SHOULD_VERIFY_BUILD=false
104+
;;
105+
*)
106+
DIR=$arg
107+
;;
108+
esac
109+
done
110+
111+
mkdir -p "$DIR"
112+
OUT="$DIR/bashunit"
39113

40-
generate_bin "$output_file"
41-
generate_checksum "$output_file"
114+
build "$OUT"
42115

43-
echo "⚡️Build completed⚡️"
116+
if [[ $SHOULD_VERIFY_BUILD == true ]]; then
117+
verify_build "$OUT"
118+
fi
Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,59 @@
11
#!/bin/bash
22
set -euo pipefail
33

4+
TMP_DIR="tmp"
5+
TMP_BIN="$TMP_DIR/bashunit"
6+
47
function set_up() {
5-
./build.sh >/dev/null
8+
./build.sh "$TMP_DIR" --ignore-verify >/dev/null
69
LATEST_VERSION="$(helpers::get_latest_tag)"
710
TEST_ENV_FILE="tests/acceptance/fixtures/.env.default"
811
}
912

1013
function tear_down() {
11-
rm -f ./bin/bashunit
14+
rm -rf "$TMP_DIR"
1215
}
1316

1417
function test_do_not_upgrade_when_latest() {
1518
local output
16-
output="$(./bin/bashunit --upgrade)"
19+
output="$($TMP_BIN --upgrade)"
1720

1821
assert_equals "> You are already on latest version" "$output"
19-
assert_string_ends_with "$LATEST_VERSION" "$(./bin/bashunit --version --env "$TEST_ENV_FILE")"
22+
assert_string_ends_with "$LATEST_VERSION" "$($TMP_BIN --version --env "$TEST_ENV_FILE")"
2023
}
2124

2225
function test_upgrade_when_a_new_version_found() {
2326
sed -i -e \
2427
's/declare -r BASHUNIT_VERSION="[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}"/declare -r BASHUNIT_VERSION="0.1.0"/' \
25-
./bin/bashunit
28+
"$TMP_BIN"
2629

2730
if [[ $_OS == "OSX" ]]; then
28-
rm -f ./bin/bashunit-e
31+
rm -f "${TMP_BIN}-e"
2932
fi
3033

3134
local output
32-
output="$(./bin/bashunit --upgrade)"
35+
output="$($TMP_BIN --upgrade)"
3336

3437
assert_contains "> Upgrading bashunit to latest version" "$output"
3538
assert_contains "> bashunit upgraded successfully to latest version $LATEST_VERSION" "$output"
36-
assert_string_ends_with "$LATEST_VERSION" "$(./bin/bashunit --version --env "$TEST_ENV_FILE")"
39+
assert_string_ends_with "$LATEST_VERSION" "$($TMP_BIN --version --env "$TEST_ENV_FILE")"
3740
}
3841

3942
function test_do_not_update_on_consecutive_calls() {
40-
todo "enable this test when --upgrade is released"
41-
# sed -i -e \
42-
# 's/declare -r BASHUNIT_VERSION="[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}"/declare -r BASHUNIT_VERSION="0.1.0"/' \
43-
# ./bin/bashunit
44-
#
45-
# if [[ $_OS == "OSX" ]]; then
46-
# rm ./bin/bashunit-e
47-
# fi
48-
#
49-
# ./bin/bashunit --upgrade
50-
# ./bin/bashunit --version
51-
#
52-
# local output
53-
# output="$(./bin/bashunit --upgrade)"
54-
#
55-
# assert_equals "> You are already on latest version" "$output"
56-
# assert_string_ends_with "$LATEST_VERSION" "$(./bin/bashunit --version --env "$TEST_ENV_FILE")"
43+
sed -i -e \
44+
's/declare -r BASHUNIT_VERSION="[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}"/declare -r BASHUNIT_VERSION="0.1.0"/' \
45+
$TMP_BIN
46+
47+
if [[ $_OS == "OSX" ]]; then
48+
rm $TMP_BIN-e
49+
fi
50+
51+
$TMP_BIN --upgrade
52+
$TMP_BIN --version
53+
54+
local output
55+
output="$($TMP_BIN --upgrade)"
56+
57+
assert_equals "> You are already on latest version" "$output"
58+
assert_string_ends_with "$LATEST_VERSION" "$($TMP_BIN --version --env "$TEST_ENV_FILE")"
5759
}

0 commit comments

Comments
 (0)