Skip to content

Commit

Permalink
Merge pull request #364 from TypedDevs/refactor/improve-build
Browse files Browse the repository at this point in the history
Improve build
  • Loading branch information
Chemaclass authored Oct 12, 2024
2 parents 435cb11 + 7d7f3f3 commit 65ecbf2
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 51 deletions.
125 changes: 81 additions & 44 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,46 @@

source src/check_os.sh

BASHUNIT_ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
export BASHUNIT_ROOT_DIR

function build() {
local out=$1

generate_bin "$out"
generate_checksum "$out"
build::generate_bin "$out"
build::generate_checksum "$out"

echo "️Build completed⚡️"
echo "️ Build completed ⚡️"
}

function verify_build() {
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 &

pid=$!

function cleanup() {
kill "$pid" 2>/dev/null
tput cnorm # Show the cursor
exit 1
}
--stop-on-failure

trap cleanup SIGINT
spinner $pid
wait $pid
echo "✅ Build verified ✅ "
# shellcheck disable=SC2181
if [[ $? -eq 0 ]]; then
echo "✅ Build verified ✅"
fi
}

function generate_bin() {
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 src/*.sh; do
{
echo "# $file"
tail -n +2 "$file" >> "$temp"
echo ""
} >> "$temp"

for file in $(build::dependencies); do
build::process_file "$file" "$temp"
done

cat bashunit >> "$temp"
Expand All @@ -57,39 +50,83 @@ function generate_bin() {
chmod u+x "$out"
}

function generate_checksum() {
# 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

if [[ "$_OS" == "OSX" ]]; then
# Use a single command for both macOS and Linux
if command -v shasum &>/dev/null; then
checksum=$(shasum -a 256 "$out")
elif [[ "$_OS" == "Linux" ]]; then
else
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 #########
########################
Expand All @@ -114,5 +151,5 @@ OUT="$DIR/bashunit"
build "$OUT"

if [[ $SHOULD_VERIFY_BUILD == true ]]; then
verify_build "$OUT"
build::verify "$OUT"
fi
7 changes: 0 additions & 7 deletions tests/unit/debug_test.sh

This file was deleted.

0 comments on commit 65ecbf2

Please sign in to comment.