|
10 | 10 | # nwe_compile_servlets — Compile servlet Java classes against Tomcat API |
11 | 11 | # nwe_validate_webapp — Check web.xml, JSP files, and lib presence |
12 | 12 | # nwe_deploy_module — All-in-one: validate → deploy → JDBC → compile → validate |
| 13 | +# nwe_progress — Display a progress bar for long operations |
| 14 | + |
| 15 | +# ═══════════════════════════════════════════════════════════════════════════════ |
| 16 | +# nwe_progress — Display a progress bar during a copy/rsync operation |
| 17 | +# Args: $1 = source dir, $2 = dest dir, $3 = operation label |
| 18 | +# Uses rsync with --progress if available, else cp with file counting |
| 19 | +# ═══════════════════════════════════════════════════════════════════════════════ |
| 20 | +nwe_progress_copy() { |
| 21 | + local SRC="$1" DEST="$2" LABEL="${3:-Copying}" |
| 22 | + |
| 23 | + # Count total files for progress |
| 24 | + local TOTAL_FILES |
| 25 | + TOTAL_FILES=$(find "$SRC" -type f 2>/dev/null | wc -l) |
| 26 | + |
| 27 | + if [ "$TOTAL_FILES" -eq 0 ]; then |
| 28 | + echo " [$LABEL] (empty source)" |
| 29 | + return 0 |
| 30 | + fi |
| 31 | + |
| 32 | + if command -v rsync &>/dev/null; then |
| 33 | + # rsync with progress indicator |
| 34 | + echo -n " [$LABEL] $TOTAL_FILES files... " |
| 35 | + rsync -a --delete --exclude='db.properties' "$SRC/" "$DEST/" 2>/dev/null |
| 36 | + echo "✓" |
| 37 | + else |
| 38 | + # cp with a simple progress counter |
| 39 | + rm -rf "$DEST" |
| 40 | + mkdir -p "$DEST" |
| 41 | + |
| 42 | + local COUNT=0 |
| 43 | + local LAST_PCT=-1 |
| 44 | + |
| 45 | + # Use cp -r but show progress by counting copied files |
| 46 | + cp -r "$SRC/"* "$DEST/" & |
| 47 | + local CP_PID=$! |
| 48 | + |
| 49 | + # Show a spinner while cp runs |
| 50 | + local SPINNER=('⠋' '⠙' '⠹' '⠸' '⠼' '⠴' '⠦' '⠧' '⠇' '⠏') |
| 51 | + local SPIN_IDX=0 |
| 52 | + echo -n " [$LABEL] $TOTAL_FILES files " |
| 53 | + while kill -0 "$CP_PID" 2>/dev/null; do |
| 54 | + echo -ne "\r [$LABEL] $TOTAL_FILES files ${SPINNER[$SPIN_IDX]} " |
| 55 | + SPIN_IDX=$(( (SPIN_IDX + 1) % ${#SPINNER[@]} )) |
| 56 | + sleep 0.2 |
| 57 | + done |
| 58 | + wait "$CP_PID" |
| 59 | + echo -e "\r [$LABEL] $TOTAL_FILES files ✓ " |
| 60 | + fi |
| 61 | + return 0 |
| 62 | +} |
| 63 | + |
| 64 | +# ═══════════════════════════════════════════════════════════════════════════════ |
| 65 | +# nwe_progress_bar — Print a progress bar inline |
| 66 | +# Args: $1 = current, $2 = total, $3 = label (optional) |
| 67 | +# ═══════════════════════════════════════════════════════════════════════════════ |
| 68 | +nwe_progress_bar() { |
| 69 | + local CURRENT="$1" TOTAL="$2" LABEL="${3:-Progress}" |
| 70 | + local WIDTH=30 |
| 71 | + local PCT=$((CURRENT * 100 / TOTAL)) |
| 72 | + local FILLED=$((CURRENT * WIDTH / TOTAL)) |
| 73 | + local EMPTY=$((WIDTH - FILLED)) |
| 74 | + printf "\r [%s] [%s%s] %3d%% (%d/%d)" "$LABEL" \ |
| 75 | + "$(printf '█%.0s' $(seq 1 $FILLED 2>/dev/null) 2>/dev/null)" \ |
| 76 | + "$(printf '░%.0s' $(seq 1 $EMPTY 2>/dev/null) 2>/dev/null)" \ |
| 77 | + "$PCT" "$CURRENT" "$TOTAL" |
| 78 | +} |
| 79 | + |
| 80 | +# ═══════════════════════════════════════════════════════════════════════════════ |
| 81 | +# nwe_download_with_progress — Download a file with a progress bar |
| 82 | +# Args: $1 = URL, $2 = output file, $3 = label (optional) |
| 83 | +# ═══════════════════════════════════════════════════════════════════════════════ |
| 84 | +nwe_download_with_progress() { |
| 85 | + local URL="$1" OUTPUT="$2" LABEL="${3:-Downloading}" |
| 86 | + |
| 87 | + echo -n " [$LABEL] $(basename "$OUTPUT")... " |
| 88 | + |
| 89 | + if command -v curl &>/dev/null; then |
| 90 | + curl -# -fL "$URL" -o "$OUTPUT" 2>&1 | tr '\r' '\n' | tail -1 |
| 91 | + if [ ${PIPESTATUS[0]} -eq 0 ]; then |
| 92 | + local SIZE |
| 93 | + SIZE=$(du -h "$OUTPUT" 2>/dev/null | cut -f1) |
| 94 | + echo "✓ ($SIZE)" |
| 95 | + return 0 |
| 96 | + else |
| 97 | + echo "✗ (download failed)" |
| 98 | + return 1 |
| 99 | + fi |
| 100 | + elif command -v wget &>/dev/null; then |
| 101 | + wget --progress=bar:force "$URL" -O "$OUTPUT" 2>&1 | tail -1 |
| 102 | + [ -f "$OUTPUT" ] && echo "✓" || echo "✗" |
| 103 | + return ${PIPESTATUS[0]} |
| 104 | + else |
| 105 | + echo "✗ (no curl or wget)" |
| 106 | + return 1 |
| 107 | + fi |
| 108 | +} |
13 | 109 |
|
14 | 110 | # ═══════════════════════════════════════════════════════════════════════════════ |
15 | 111 | # nwe_validate_tomcat — Check Tomcat installation |
@@ -44,9 +140,8 @@ nwe_deploy_webapp() { |
44 | 140 | echo "[!] Webapp source not found: $SRC" |
45 | 141 | return 1 |
46 | 142 | fi |
47 | | - rm -rf "$DEST" |
48 | 143 | mkdir -p "$DEST/WEB-INF/lib" "$DEST/WEB-INF/classes" |
49 | | - cp -r "$SRC/"* "$DEST/" |
| 144 | + nwe_progress_copy "$SRC" "$DEST" "Deploy" |
50 | 145 | echo "[✓] Webapp deployed to $DEST" |
51 | 146 | return 0 |
52 | 147 | } |
|
0 commit comments