Last active
July 12, 2026 10:24
-
-
Save omid/86baa8713fcd2ab2d1238fb4aa4e045b to your computer and use it in GitHub Desktop.
Global Cargo Build Directory Setup & Cleanup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CARGO_HOME_DIR="${CARGO_HOME:-$HOME/.cargo}" | |
| CONFIG="$CARGO_HOME_DIR/config.toml" | |
| RESET='\033[0m' | |
| BOLD='\033[1m' | |
| RED='\033[1;31m' | |
| GREEN='\033[1;32m' | |
| YELLOW='\033[1;33m' | |
| CYAN='\033[1;36m' | |
| printf '\n\n\n' | |
| printf "${BOLD}${CYAN}Configuring Cargo shared build directory...${RESET}\n\n" | |
| mkdir -p "$CARGO_HOME_DIR" | |
| touch "$CONFIG" | |
| if awk ' | |
| /^[[:space:]]*\[/ { | |
| section = $0 | |
| sub(/[[:space:]]*#.*/, "", section) | |
| in_build = section ~ /^[[:space:]]*\[build\][[:space:]]*$/ | |
| } | |
| /^[[:space:]]*build\.build-dir[[:space:]]*=/ { | |
| found = 1 | |
| } | |
| in_build && /^[[:space:]]*build-dir[[:space:]]*=/ { | |
| found = 1 | |
| } | |
| END { | |
| exit(found ? 0 : 1) | |
| } | |
| ' "$CONFIG"; then | |
| HAD_BUILD_DIR=1 | |
| else | |
| HAD_BUILD_DIR=0 | |
| fi | |
| if (( HAD_BUILD_DIR == 0 )); then | |
| printf "${YELLOW}build-dir is not configured.${RESET}\n" | |
| BACKUP="$CONFIG.backup.$(date +%Y%m%d-%H%M%S)" | |
| cp -- "$CONFIG" "$BACKUP" | |
| printf "${CYAN}Backup created:${RESET} %s\n" "$BACKUP" | |
| printf "${CYAN}Adding shared build directory...${RESET}\n" | |
| # Remove any old global target-dir setting. | |
| awk ' | |
| /^[[:space:]]*\[/ { | |
| section = $0 | |
| sub(/[[:space:]]*#.*/, "", section) | |
| in_build = section ~ /^[[:space:]]*\[build\][[:space:]]*$/ | |
| } | |
| /^[[:space:]]*build\.target-dir[[:space:]]*=/ { | |
| next | |
| } | |
| in_build && /^[[:space:]]*target-dir[[:space:]]*=/ { | |
| next | |
| } | |
| { | |
| } | |
| ' "$CONFIG" > "$CONFIG.tmp" | |
| mv -- "$CONFIG.tmp" "$CONFIG" | |
| awk ' | |
| BEGIN { | |
| added = 0 | |
| } | |
| { | |
| line = $0 | |
| sub(/[[:space:]]*#.*/, "", line) | |
| if (!added && line ~ /^[[:space:]]*\[build\][[:space:]]*$/) { | |
| print "build-dir = \"{cargo-cache-home}/build\"" | |
| added = 1 | |
| } | |
| } | |
| END { | |
| if (!added) { | |
| print "" | |
| print "[build]" | |
| print "build-dir = \"{cargo-cache-home}/build\"" | |
| } | |
| } | |
| ' "$CONFIG" > "$CONFIG.tmp" | |
| mv -- "$CONFIG.tmp" "$CONFIG" | |
| printf "\n${YELLOW}Removing old Cargo target directories...${RESET}\n\n" | |
| BEFORE=$(df -P -B1 "$HOME" | awk 'NR == 2 { print $4 }') | |
| find "$HOME" \ | |
| -type d \ | |
| -name target \ | |
| \( \ | |
| -exec test -f '{}/CACHEDIR.TAG' \; \ | |
| -o -exec test -f '{}/.rustc_info.json' \; \ | |
| \) \ | |
| -prune \ | |
| -exec rm -rf -- {} + | |
| if [[ -d "$HOME/.cache/cargo-target" ]] && | |
| [[ -f "$HOME/.cache/cargo-target/CACHEDIR.TAG" || | |
| -f "$HOME/.cache/cargo-target/.rustc_info.json" ]]; then | |
| rm -rf -- "$HOME/.cache/cargo-target" | |
| fi | |
| sync | |
| AFTER=$(df -P -B1 "$HOME" | awk 'NR == 2 { print $4 }') | |
| SAVED=$((AFTER - BEFORE)) | |
| printf "${CYAN}Available before:${RESET} %s\n" \ | |
| "$(numfmt --to=iec-i --suffix=B -- "$BEFORE")" | |
| printf "${CYAN}Available after: ${RESET} %s\n" \ | |
| "$(numfmt --to=iec-i --suffix=B -- "$AFTER")" | |
| if (( SAVED >= 0 )); then | |
| printf "${GREEN}Space saved: %s${RESET}\n" \ | |
| "$(numfmt --to=iec-i --suffix=B -- "$SAVED")" | |
| else | |
| printf "${RED}Disk-space difference: %s${RESET}\n" \ | |
| "$(numfmt --to=iec-i --suffix=B -- "$SAVED")" | |
| fi | |
| else | |
| printf "${GREEN}build-dir is already configured.${RESET}\n" | |
| printf "${YELLOW}Configuration backup, modification, and cleanup skipped.${RESET}\n" | |
| fi | |
| printf "\n${BOLD}${CYAN}Cargo configuration:${RESET}\n\n" | |
| cat "$CONFIG" | |
| printf '\n' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment