|
| 1 | +#!/bin/bash |
| 2 | +# Cleanup build artifacts to prevent target directory bloat |
| 3 | +# Usage: ./scripts/cleanup-build.sh [options] |
| 4 | +# Options: |
| 5 | +# --aggressive Remove more artifacts (docs, unused deps) |
| 6 | +# --dry-run Show what would be deleted without actually deleting |
| 7 | + |
| 8 | +set -euo pipefail |
| 9 | + |
| 10 | +AGGRESSIVE=false |
| 11 | +DRY_RUN=false |
| 12 | + |
| 13 | +while [[ $# -gt 0 ]]; do |
| 14 | + case $1 in |
| 15 | + --aggressive) |
| 16 | + AGGRESSIVE=true |
| 17 | + shift |
| 18 | + ;; |
| 19 | + --dry-run) |
| 20 | + DRY_RUN=true |
| 21 | + shift |
| 22 | + ;; |
| 23 | + *) |
| 24 | + echo "Unknown option: $1" |
| 25 | + echo "Usage: $0 [--aggressive] [--dry-run]" |
| 26 | + exit 1 |
| 27 | + ;; |
| 28 | + esac |
| 29 | +done |
| 30 | + |
| 31 | +cd "$(git rev-parse --show-toplevel)" || exit 1 |
| 32 | + |
| 33 | +echo "=== Terraphim Build Cleanup ===" |
| 34 | +echo "Mode: $([ "$DRY_RUN" = true ] && echo 'DRY RUN (no changes)' || echo 'ACTIVE')" |
| 35 | +echo "Aggressive: $AGGRESSIVE" |
| 36 | +echo "" |
| 37 | + |
| 38 | +# Calculate current size |
| 39 | +echo "Current target directory size:" |
| 40 | +du -sh target/ 2>/dev/null || echo "No target directory found" |
| 41 | +echo "" |
| 42 | + |
| 43 | +# Cleanup commands |
| 44 | +cleanup_commands=( |
| 45 | + "Remove old release builds older than 7 days: find target/release -name '*.rlib' -mtime +7 -delete" |
| 46 | + "Remove debug rlibs older than 3 days: find target/debug -name '*.rlib' -mtime +3 -delete" |
| 47 | + "Remove doc directory: rm -rf target/doc" |
| 48 | + "Remove incremental compilation: rm -rf target/*/incremental" |
| 49 | +) |
| 50 | + |
| 51 | +if [ "$AGGRESSIVE" = true ]; then |
| 52 | + cleanup_commands+=( |
| 53 | + "Remove all debug builds: cargo clean --debug" |
| 54 | + "Remove example builds: find target -name 'examples' -type d -exec rm -rf {} +" |
| 55 | + "Remove benchmark builds: find target -name 'benches' -type d -exec rm -rf {} +" |
| 56 | + ) |
| 57 | +fi |
| 58 | + |
| 59 | +# Execute cleanup |
| 60 | +for cmd in "${cleanup_commands[@]}"; do |
| 61 | + echo "Executing: $cmd" |
| 62 | + if [ "$DRY_RUN" = false ]; then |
| 63 | + eval "$cmd" 2>/dev/null || true |
| 64 | + fi |
| 65 | +done |
| 66 | + |
| 67 | +echo "" |
| 68 | + |
| 69 | +# Show sizes of large directories |
| 70 | +echo "=== Large directories in target ===" |
| 71 | + |
| 72 | +find target -maxdepth 2 -type d -exec du -sh {} \; 2>/dev/null | sort -rh | head -20 || true |
| 73 | + |
| 74 | +echo "" |
| 75 | + |
| 76 | +# Final size after cleanup |
| 77 | +echo "Target directory size after cleanup:" |
| 78 | +du -sh target/ 2>/dev/null || echo "No target directory found" |
| 79 | + |
| 80 | +echo "" |
| 81 | +if [ "$DRY_RUN" = false ]; then |
| 82 | + echo "Cleanup completed!" |
| 83 | + echo "Run with --dry-run to preview changes" |
| 84 | +else |
| 85 | + echo "Dry run completed. Run without --dry-run to execute cleanup." |
| 86 | +fi |
0 commit comments