BlueGit

ict rusty-teams

Files

rusty-teams / scripts / prune-target.sh

prune-target.sh

prune-target.sh 1.4 KiB Download Raw
1#!/usr/bin/env bash
2# Shrink the shared cargo target dir: keep only the newest variant of each linked binary, drop old incremental sessions.
3# Usage: scripts/prune-target.sh [target-dir, default $CARGO_TARGET_DIR] [incremental max age in days, default 1]
4set -euo pipefail
5targetDir="${1:-${CARGO_TARGET_DIR:?set CARGO_TARGET_DIR or pass the target dir}}"
6incrementalMaxAgeDays="${2:-1}"
7cd "$targetDir"
8sizeBefore=$(du -s --block-size=1G . | cut -f1)
9
10for profileDir in debug release x86_64-pc-windows-msvc/debug x86_64-pc-windows-msvc/release; do
11 for subDir in deps examples; do
12 binaryDir="$profileDir/$subDir"
13 [ -d "$binaryDir" ] || continue
14 find "$binaryDir" -maxdepth 1 -type f -executable ! -name '*.so' ! -name '*.d' ! -name '*.rlib' ! -name '*.rmeta' -printf '%T@ %p\n' |
15 sort -rn |
16 awk '{ name = $2; sub(/-[0-9a-f]{16}(\.exe)?$/, "", name); extension = $2; sub(/.*-[0-9a-f]{16}/, "", extension); if (seen[name extension]++) print $2 }' |
17 while read -r staleBinary; do
18 rm -f "$staleBinary" "${staleBinary%.exe}.pdb" "$staleBinary.dwp"
19 done
20 done
21 for incrementalDir in "$profileDir"/incremental*; do
22 [ -d "$incrementalDir" ] || continue
23 find "$incrementalDir" -mindepth 1 -maxdepth 1 -type d -mtime +"$incrementalMaxAgeDays" -exec rm -rf {} +
24 done
25done
26
27echo "target dir: ${sizeBefore}G -> $(du -s --block-size=1G . | cut -f1)G"