|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Uniform lifecycle controller for modules in Java.Web.Server.Telnet.Front.Java.21. |
| 3 | +# Source trees are never deleted by "remove"; only installed/build artifacts are removed. |
| 4 | +set -Eeuo pipefail |
| 5 | + |
| 6 | +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 7 | +MODULES_DIR="$ROOT/modules" |
| 8 | +ACTION="${1:-status}" |
| 9 | +TARGET="${2:-all}" |
| 10 | + |
| 11 | +usage() { |
| 12 | + cat <<'EOF' |
| 13 | +Usage: scripts/module-lifecycle.sh <install|update|verify|remove|status> [module|all] |
| 14 | +
|
| 15 | +install Build/prepare a module and deploy when its supported lifecycle exists. |
| 16 | +update Rebuild/redeploy a module using repository-local build mechanisms. |
| 17 | +verify Check source/build/deployment state without changing it. |
| 18 | +remove Stop/undeploy and remove generated artifacts; NEVER removes source. |
| 19 | +status Report lifecycle capabilities and current state. |
| 20 | +EOF |
| 21 | +} |
| 22 | + |
| 23 | +[[ -d "$MODULES_DIR" ]] || { echo "Missing modules directory: $MODULES_DIR" >&2; exit 1; } |
| 24 | + |
| 25 | +module_dirs() { |
| 26 | + if [[ "$TARGET" == "all" ]]; then |
| 27 | + find "$MODULES_DIR" -mindepth 1 -maxdepth 2 -type d -name source -printf '%h\n' 2>/dev/null | sort -u |
| 28 | + find "$MODULES_DIR" -mindepth 1 -maxdepth 1 -type d -printf '%p\n' | sort -u |
| 29 | + else |
| 30 | + [[ -d "$MODULES_DIR/$TARGET" ]] || { echo "Unknown module: $TARGET" >&2; exit 2; } |
| 31 | + printf '%s\n' "$MODULES_DIR/$TARGET" |
| 32 | + fi |
| 33 | +} |
| 34 | + |
| 35 | +run_optional() { |
| 36 | + local script="$1"; shift |
| 37 | + if [[ -x "$script" ]]; then |
| 38 | + "$script" "$@" |
| 39 | + elif [[ -f "$script" ]]; then |
| 40 | + bash "$script" "$@" |
| 41 | + else |
| 42 | + return 127 |
| 43 | + fi |
| 44 | +} |
| 45 | + |
| 46 | +install_one() { |
| 47 | + local m="$1" |
| 48 | + echo "==> INSTALL $m" |
| 49 | + if [[ -f "$m/install.sh" ]]; then run_optional "$m/install.sh"; return; fi |
| 50 | + if [[ -f "$m/source/Makefile" ]]; then make -C "$m/source"; return; fi |
| 51 | + if [[ -f "$m/Makefile" ]]; then make -C "$m"; return; fi |
| 52 | + if [[ -f "$m/pom.xml" ]] && command -v mvn >/dev/null; then mvn -f "$m/pom.xml" package; return; fi |
| 53 | + if [[ -f "$m/gradlew" ]]; then (cd "$m" && ./gradlew build); return; fi |
| 54 | + echo "No install/build hook for $(basename "$m"); source retained." |
| 55 | +} |
| 56 | + |
| 57 | +update_one() { |
| 58 | + local m="$1" |
| 59 | + echo "==> UPDATE $m" |
| 60 | + if [[ -f "$m/update.sh" ]]; then run_optional "$m/update.sh"; return; fi |
| 61 | + install_one "$m" |
| 62 | +} |
| 63 | + |
| 64 | +verify_one() { |
| 65 | + local m="$1" |
| 66 | + echo "==> VERIFY $m" |
| 67 | + if [[ -f "$m/verify.sh" ]]; then run_optional "$m/verify.sh"; return; fi |
| 68 | + if [[ -x "$m/start-backend.sh" || -f "$m/start-backend.sh" ]]; then echo "backend lifecycle: present"; else echo "backend lifecycle: absent"; fi |
| 69 | + if [[ -x "$m/shutdown-backend.sh" || -f "$m/shutdown-backend.sh" ]]; then echo "backend removal/stop hook: present"; else echo "backend removal/stop hook: absent"; fi |
| 70 | + if [[ -f "$m/source/Makefile" || -f "$m/Makefile" || -f "$m/pom.xml" || -f "$m/gradlew" ]]; then echo "build mechanism: present"; else echo "build mechanism: not detected"; fi |
| 71 | +} |
| 72 | + |
| 73 | +remove_one() { |
| 74 | + local m="$1" |
| 75 | + echo "==> REMOVE GENERATED ARTIFACTS $m" |
| 76 | + # Stop running services first when the module supplies the repository's hook. |
| 77 | + if [[ -f "$m/shutdown-backend.sh" ]]; then run_optional "$m/shutdown-backend.sh" || true; fi |
| 78 | + if [[ -f "$m/shutdown-frontend.sh" ]]; then run_optional "$m/shutdown-frontend.sh" || true; fi |
| 79 | + # Prefer a repository-defined uninstall hook. |
| 80 | + if [[ -f "$m/uninstall.sh" ]]; then run_optional "$m/uninstall.sh"; return; fi |
| 81 | + # Safe generated-output cleanup only. Never rm -rf the module/source tree. |
| 82 | + rm -rf "$m/out" "$m/build" "$m/target" 2>/dev/null || true |
| 83 | + find "$m" -maxdepth 2 -type f \( -name '*.class' -o -name '*.jar' \) -print -delete 2>/dev/null || true |
| 84 | + echo "Source preserved. Generated artifacts removed where recognized." |
| 85 | +} |
| 86 | + |
| 87 | +case "$ACTION" in |
| 88 | + install|update|verify|remove|status) ;; |
| 89 | + *) usage; exit 2;; |
| 90 | +esac |
| 91 | + |
| 92 | +while IFS= read -r module; do |
| 93 | + [[ -d "$module" ]] || continue |
| 94 | + case "$ACTION" in |
| 95 | + install) install_one "$module" ;; |
| 96 | + update) update_one "$module" ;; |
| 97 | + verify) verify_one "$module" ;; |
| 98 | + remove) remove_one "$module" ;; |
| 99 | + status) verify_one "$module" ;; |
| 100 | + esac |
| 101 | +done < <(module_dirs | awk '!seen[$0]++') |
0 commit comments