|
| 1 | +#!/usr/bin/env bash |
| 2 | +# nwe-install.sh — NitroWebExpress installer |
| 3 | +# Compiles source (if needed), stages classes to out/, chmod's scripts. |
| 4 | +# Usage: bash bash/nwe-install.sh [--force] (--force recompiles even if up-to-date) |
| 5 | + |
| 6 | +set -euo pipefail |
| 7 | + |
| 8 | +ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 9 | +SRC="$ROOT/source" |
| 10 | +OUT="$ROOT/out" |
| 11 | +JAR="$ROOT/jars/mysql/mysql-connector-j-9.7.0.jar" |
| 12 | +FORCE=0 |
| 13 | +[[ "${1:-}" == "--force" ]] && FORCE=1 |
| 14 | + |
| 15 | +echo "=== NitroWebExpress Installer ===" |
| 16 | +echo "ROOT : $ROOT" |
| 17 | +echo "SRC : $SRC" |
| 18 | +echo "OUT : $OUT" |
| 19 | +echo "" |
| 20 | + |
| 21 | +# ── 1. chmod all scripts ────────────────────────────────────────────────────── |
| 22 | +echo "[1/3] Setting executable permissions on scripts..." |
| 23 | +find "$ROOT/bash" -name "*.sh" -exec chmod +x {} \; |
| 24 | +find "$ROOT/scripts" -name "*.sh" -exec chmod +x {} \; |
| 25 | +echo " Done." |
| 26 | + |
| 27 | +# ── 2. Compile if needed ────────────────────────────────────────────────────── |
| 28 | +echo "[2/3] Checking compilation status..." |
| 29 | + |
| 30 | +# Collect all .java source files |
| 31 | +mapfile -t SOURCES < <(find "$SRC" -name "*.java" | sort) |
| 32 | + |
| 33 | +if [[ ${#SOURCES[@]} -eq 0 ]]; then |
| 34 | + echo " No .java files found under $SRC — nothing to compile." |
| 35 | +else |
| 36 | + NEEDS_COMPILE=$FORCE |
| 37 | + |
| 38 | + if [[ $NEEDS_COMPILE -eq 0 ]]; then |
| 39 | + # Check if any .java is newer than its corresponding .class in out/ |
| 40 | + for java_file in "${SOURCES[@]}"; do |
| 41 | + rel="${java_file#$SRC/}" |
| 42 | + class_file="$OUT/${rel%.java}.class" |
| 43 | + if [[ ! -f "$class_file" || "$java_file" -nt "$class_file" ]]; then |
| 44 | + NEEDS_COMPILE=1 |
| 45 | + break |
| 46 | + fi |
| 47 | + done |
| 48 | + fi |
| 49 | + |
| 50 | + if [[ $NEEDS_COMPILE -eq 1 ]]; then |
| 51 | + echo " Compiling ${#SOURCES[@]} source files..." |
| 52 | + mkdir -p "$OUT" |
| 53 | + |
| 54 | + # Build source-path list for javac |
| 55 | + SOURCE_LIST=$(mktemp) |
| 56 | + printf '%s\n' "${SOURCES[@]}" > "$SOURCE_LIST" |
| 57 | + |
| 58 | + javac \ |
| 59 | + --release 21 \ |
| 60 | + -cp "$OUT:$JAR" \ |
| 61 | + -sourcepath "$SRC" \ |
| 62 | + -d "$OUT" \ |
| 63 | + "@$SOURCE_LIST" |
| 64 | + |
| 65 | + rm -f "$SOURCE_LIST" |
| 66 | + echo " Compilation successful." |
| 67 | + else |
| 68 | + echo " All classes are up-to-date — skipping compilation. (use --force to recompile)" |
| 69 | + fi |
| 70 | +fi |
| 71 | + |
| 72 | +# ── 3. Move any .class files left in source/ into out/ ──────────────────────── |
| 73 | +echo "[3/3] Staging any stray .class files from source/ to out/..." |
| 74 | +MOVED=0 |
| 75 | +while IFS= read -r -d '' class_file; do |
| 76 | + rel="${class_file#$SRC/}" |
| 77 | + dest="$OUT/$rel" |
| 78 | + mkdir -p "$(dirname "$dest")" |
| 79 | + mv "$class_file" "$dest" |
| 80 | + MOVED=$((MOVED + 1)) |
| 81 | +done < <(find "$SRC" -name "*.class" -print0) |
| 82 | +echo " Moved $MOVED file(s)." |
| 83 | + |
| 84 | +echo "" |
| 85 | +echo "=== Install complete. Run: bash scripts/startup.sh ===" |
0 commit comments