|
2 | 2 |
|
3 | 3 | source src/check_os.sh |
4 | 4 |
|
| 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 | + |
5 | 39 | 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" |
15 | 58 | } |
16 | 59 |
|
17 | 60 | function generate_checksum() { |
| 61 | + local out=$1 |
| 62 | + |
18 | 63 | if [[ "$_OS" == "Windows" ]]; then |
19 | 64 | return |
20 | 65 | fi |
21 | 66 |
|
22 | | - local file=$1 |
23 | 67 | if [[ "$_OS" == "OSX" ]]; then |
24 | | - checksum=$(shasum -a 256 "$file") |
| 68 | + checksum=$(shasum -a 256 "$out") |
25 | 69 | elif [[ "$_OS" == "Linux" ]]; then |
26 | | - checksum=$(sha256sum "$file") |
| 70 | + checksum=$(sha256sum "$out") |
27 | 71 | fi |
28 | 72 |
|
29 | | - echo "$checksum" > bin/checksum |
| 73 | + echo "$checksum" > "$(dirname "$out")/checksum" |
30 | 74 | echo "$checksum" |
31 | 75 | } |
32 | 76 |
|
| 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 | + |
33 | 93 | ######################## |
34 | 94 | ######### MAIN ######### |
35 | 95 | ######################## |
36 | 96 |
|
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" |
39 | 113 |
|
40 | | -generate_bin "$output_file" |
41 | | -generate_checksum "$output_file" |
| 114 | +build "$OUT" |
42 | 115 |
|
43 | | -echo "⚡️Build completed⚡️" |
| 116 | +if [[ $SHOULD_VERIFY_BUILD == true ]]; then |
| 117 | + verify_build "$OUT" |
| 118 | +fi |
0 commit comments