feat: enhance Laravel cache script with progress visualization and structured command execution

This commit is contained in:
2025-12-03 21:19:08 -05:00
parent d85c9a23dd
commit d996e424a1

View File

@@ -1,17 +1,92 @@
php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear
php artisan event:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
chown -R www-data:www-data storage bootstrap/cache
chmod -R 775 storage bootstrap/cache
php artisan package:discover --ansi
php artisan optimize
php artisan queue:work --daemon &
if command -v php >/dev/null 2>&1; then
php -r "if (function_exists('opcache_reset')) opcache_reset();"
fi
#!/usr/bin/env bash
set -euo pipefail
BAR_WIDTH=40
SLEEP_INTERVAL=0.04
CLEAR_LINE="\033[K"
GREEN="\033[32m"
RESET="\033[0m"
STEPS=(
"Clear caches"
"Rebuild caches"
"Reset ownership"
"Discover packages"
"Optimize app"
"Restart queue"
"Reset OPcache"
)
COMMANDS=(
"php artisan cache:clear && php artisan config:clear && php artisan route:clear && php artisan view:clear && php artisan event:clear"
"php artisan config:cache && php artisan route:cache && php artisan view:cache && php artisan event:cache"
"chown -R www-data:www-data storage bootstrap/cache && chmod -R 775 storage bootstrap/cache"
"php artisan package:discover --ansi"
"php artisan optimize"
"pkill -f 'artisan queue:work' >/dev/null 2>&1 || true; nohup php artisan queue:work --daemon --quiet >> /dev/null 2>&1 &"
"php -r 'if(function_exists(\"opcache_reset\")) opcache_reset();' 2>/dev/null || true"
)
draw_bar() {
local percent=$1
local width=$2
local text="$3"
local clean_text
clean_text=$(echo -ne "$text" | sed 's/\x1b\[[0-9;]*m//g')
[ ${#clean_text} -gt $width ] && clean_text="${clean_text:0:$width}"
local filled=$(( percent * width / 100 ))
[ $filled -gt $width ] && filled=$width
local text_start=$(( (width - ${#clean_text}) / 2 ))
local text_end=$(( text_start + ${#clean_text} ))
local bar=""
for ((i=0;i<width;i++)); do
if (( i >= text_start && i < text_end )); then
bar+="${clean_text:i-text_start:1}"
else
if (( i < filled )); then
bar+="#"
else
bar+="-"
fi
fi
done
printf "[%s] %3d%%" "$bar" "$percent"
}
run_step() {
local name="$1"
local command="$2"
local start="$3"
local end="$4"
bash -c "$command" > /dev/null 2>&1 &
local pid=$!
local percent=$start
while kill -0 "$pid" 2>/dev/null; do
[ "$percent" -lt "$end" ] && percent=$((percent + 1))
echo -ne "\r$(draw_bar $percent $BAR_WIDTH "$name")$CLEAR_LINE"
sleep "$SLEEP_INTERVAL"
done
wait "$pid"
echo -ne "\r$(draw_bar $end $BAR_WIDTH "$name")$CLEAR_LINE"
}
main() {
local total=${#STEPS[@]}
for i in "${!STEPS[@]}"; do
local start=$(( i * 100 / total ))
local end=$(( (i + 1) * 100 / total ))
run_step "${STEPS[$i]}" "${COMMANDS[$i]}" "$start" "$end"
done
echo -ne "\n${GREEN}Laravel cache workflow complete!${RESET}\n"
}
main