Files

106 lines
3.3 KiB
Bash
Executable File

#!/bin/bash
set -e
# run.sh - Build (only if needed) and run the sovereign_browser binary.
#
# Usage:
# ./run.sh [args...] # build if stale (debug), then run with args
# ./run.sh --release # build/run the optimized release binary instead
# ./run.sh --build # force a rebuild, then run
# ./run.sh --no-build # run without building (fails if missing)
#
# The default profile is DEBUG: it rebuilds in a few seconds, which is what
# you want while iterating. Pass --release for the optimized binary (much
# slower to build, faster at runtime).
#
# The binary is rebuilt only when it is missing or when any source file
# (src/**, Cargo.toml, Cargo.lock, www/**) is newer than the binary.
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
print_status() { echo -e "${BLUE}[INFO]${NC} $1" >&2; }
print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" >&2; }
print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" >&2; }
print_error() { echo -e "${RED}[ERROR]${NC} $1" >&2; }
# --- Config -------------------------------------------------------------
# Resolve the project root as the directory containing this script.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
BIN_NAME="sovereign_browser"
# --- Args ---------------------------------------------------------------
FORCE_BUILD=false
NO_BUILD=false
RELEASE=false
RUN_ARGS=()
for arg in "$@"; do
case "$arg" in
--release)
RELEASE=true
;;
--build)
FORCE_BUILD=true
;;
--no-build)
NO_BUILD=true
;;
*)
RUN_ARGS+=("$arg")
;;
esac
done
# --- Profile ------------------------------------------------------------
# Debug is the default; --release selects the optimized profile.
if [[ "$RELEASE" == true ]]; then
PROFILE_DIR="release"
CARGO_PROFILE_ARGS=(--release)
else
PROFILE_DIR="debug"
CARGO_PROFILE_ARGS=()
fi
BIN_PATH="target/${PROFILE_DIR}/${BIN_NAME}"
# --- Determine whether a build is needed --------------------------------
needs_build() {
# No binary yet -> must build.
[[ ! -x "$BIN_PATH" ]] && return 0
# Any tracked source file newer than the binary -> rebuild.
local newer
newer="$(find src www Cargo.toml Cargo.lock \
-type f -newer "$BIN_PATH" -print -quit 2>/dev/null || true)"
[[ -n "$newer" ]]
}
# --- Build --------------------------------------------------------------
if [[ "$NO_BUILD" == true ]]; then
if [[ ! -x "$BIN_PATH" ]]; then
print_error "Binary not found at ${BIN_PATH} and --no-build was given."
exit 1
fi
print_status "Skipping build (--no-build)."
elif [[ "$FORCE_BUILD" == true ]]; then
print_status "Forcing rebuild of ${BIN_NAME} (${PROFILE_DIR})..."
cargo build "${CARGO_PROFILE_ARGS[@]}"
print_success "Build complete."
elif needs_build; then
print_status "Sources changed or binary missing; building ${BIN_NAME} (${PROFILE_DIR})..."
cargo build "${CARGO_PROFILE_ARGS[@]}"
print_success "Build complete."
else
print_status "Binary is up to date; skipping build."
fi
# --- Run ----------------------------------------------------------------
print_status "Running ${BIN_PATH} ${RUN_ARGS[*]}"
exec "$BIN_PATH" "${RUN_ARGS[@]}"