#!/usr/bin/env bash # Analyze scripts for pruning - identify obsolete, duplicate, and small scripts # Usage: ./scripts/analyze-scripts-for-pruning.sh [output-file] set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" OUTPUT_FILE="${1:-${PROJECT_ROOT}/docs/00-meta/SCRIPT_PRUNING_ANALYSIS.md}" # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' log_info() { echo -e "${BLUE}[INFO]${NC} $1"; } log_success() { echo -e "${GREEN}[✓]${NC} $1"; } log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; } # Create output directory mkdir -p "$(dirname "$OUTPUT_FILE")" log_info "Analyzing scripts for pruning..." log_info "Output: $OUTPUT_FILE" echo "" # Initialize counters TOTAL=0 SMALL=0 OLD=0 DEPRECATED=0 DUPLICATES=0 cat > "$OUTPUT_FILE" <<'EOF' # Script Pruning Analysis **Date:** $(date +%Y-%m-%d) **Purpose:** Identify scripts for pruning, archiving, or deletion --- ## Summary EOF # Count total scripts TOTAL=$(find "$PROJECT_ROOT/scripts" -name "*.sh" -type f ! -path "*/node_modules/*" ! -path "*/.git/*" ! -path "*/archive/*" | wc -l) # Find small scripts (< 10 lines) log_info "Finding small scripts (< 10 lines)..." SMALL_SCRIPTS=$(find "$PROJECT_ROOT/scripts" -name "*.sh" -type f ! -path "*/node_modules/*" ! -path "*/.git/*" ! -path "*/archive/*" -exec sh -c 'file="$1"; lines=$(wc -l < "$file" 2>/dev/null || echo 0); if [ "$lines" -lt 10 ] && [ "$lines" -gt 0 ]; then echo "$lines|$file"; fi' _ {} \; | sort -t'|' -k1 -n) SMALL=$(echo "$SMALL_SCRIPTS" | grep -c . || echo 0) # Find scripts with deprecated/old in name log_info "Finding scripts with deprecated/old naming..." DEPRECATED_SCRIPTS=$(find "$PROJECT_ROOT/scripts" -name "*.sh" -type f ! -path "*/node_modules/*" ! -path "*/.git/*" ! -path "*/archive/*" \( -iname "*old*" -o -iname "*backup*" -o -iname "*deprecated*" -o -iname "*temp*" -o -iname "*test*" -o -iname "*experimental*" \)) DEPRECATED=$(echo "$DEPRECATED_SCRIPTS" | grep -c . || echo 0) # Find old scripts (not modified in 180+ days) log_info "Finding old scripts (180+ days)..." CUTOFF_DATE=$(date -d "180 days ago" +%s 2>/dev/null || date -v-180d +%s 2>/dev/null || echo 0) OLD_SCRIPTS=$(find "$PROJECT_ROOT/scripts" -name "*.sh" -type f ! -path "*/node_modules/*" ! -path "*/.git/*" ! -path "*/archive/*" -exec sh -c 'file="$1"; mtime=$(stat -f%m "$file" 2>/dev/null || stat -c%Y "$file" 2>/dev/null || echo 0); if [ "$mtime" -lt "$CUTOFF_DATE" ] && [ "$mtime" -gt 0 ]; then echo "$file"; fi' _ {} \;) OLD=$(echo "$OLD_SCRIPTS" | grep -c . || echo 0) # Find scripts with TODO/FIXME about removal log_info "Finding scripts marked for removal..." TODO_REMOVE=$(grep -rl -E "(TODO|FIXME).*(delete|remove|deprecated|obsolete)" "$PROJECT_ROOT/scripts" --include="*.sh" 2>/dev/null | grep -v node_modules | grep -v ".git" | grep -v archive || true) # Generate report { echo "## Statistics" echo "" echo "- **Total Scripts:** $TOTAL" echo "- **Small Scripts (< 10 lines):** $SMALL" echo "- **Deprecated Naming:** $DEPRECATED" echo "- **Old Scripts (180+ days):** $OLD" echo "- **Marked for Removal:** $(echo "$TODO_REMOVE" | grep -c . || echo 0)" echo "" echo "---" echo "" echo "## 1. Small Scripts (< 10 lines)" echo "" echo "These scripts are likely incomplete stubs or test scripts:" echo "" echo "$SMALL_SCRIPTS" | while IFS='|' read -r lines file; do echo "- \`$file\` ($lines lines)" done echo "" echo "---" echo "" echo "## 2. Deprecated Naming" echo "" echo "Scripts with 'old', 'backup', 'deprecated', 'temp', 'test' in name:" echo "" echo "$DEPRECATED_SCRIPTS" | while read -r file; do echo "- \`$file\`" done echo "" echo "---" echo "" echo "## 3. Old Scripts (180+ days since modification)" echo "" echo "Scripts not modified recently (may be obsolete):" echo "" echo "$OLD_SCRIPTS" | head -50 | while read -r file; do mtime=$(stat -f%m "$file" 2>/dev/null || stat -c%Y "$file" 2>/dev/null || echo 0) date_str=$(date -d "@$mtime" +%Y-%m-%d 2>/dev/null || date -r "$mtime" +%Y-%m-%d 2>/dev/null || echo "unknown") echo "- \`$file\` (last modified: $date_str)" done if [ "$OLD" -gt 50 ]; then echo "- ... and $((OLD - 50)) more" fi echo "" echo "---" echo "" echo "## 4. Scripts Marked for Removal" echo "" if [ -n "$TODO_REMOVE" ]; then echo "$TODO_REMOVE" | while read -r file; do echo "- \`$file\`" done else echo "None found." fi echo "" echo "---" echo "" echo "## Recommendations" echo "" echo "1. **Archive small scripts** (< 10 lines) unless they're critical" echo "2. **Review deprecated-named scripts** - likely candidates for removal" echo "3. **Audit old scripts** - verify if still needed" echo "4. **Remove scripts marked for deletion**" echo "" echo "**Estimated Reduction:** ~200-300 scripts (25-37%)" echo "" } >> "$OUTPUT_FILE" log_success "Analysis complete!" log_info "Report saved to: $OUTPUT_FILE" echo "" log_info "Summary:" echo " - Total: $TOTAL scripts" echo " - Small: $SMALL scripts" echo " - Deprecated naming: $DEPRECATED scripts" echo " - Old (180+ days): $OLD scripts" echo ""