zed/script/clear-target-dir-if-larger-than
Piotr Osiewicz c69a91baf8
ci: Clean workspace members more eagerly (#53427)
This relies on 1.94s --workspace option we've added to cargo

Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

Closes #ISSUE

Release Notes:

- N/A
2026-04-09 00:21:02 +02:00

35 lines
1 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euxo pipefail
if [[ $# -lt 1 || $# -gt 2 ]]; then
echo "usage: $0 <MAX_SIZE_IN_GB> [SMALL_CLEAN_SIZE_IN_GB]"
exit 1
fi
if ! [[ -d target ]]; then
echo "target directory does not exist yet"
exit 0
fi
max_size_gb=$1
small_clean_size_gb=${2:-}
if [[ -n "${small_clean_size_gb}" && ${small_clean_size_gb} -ge ${max_size_gb} ]]; then
echo "error: small clean threshold (${small_clean_size_gb}gb) must be smaller than max size (${max_size_gb}gb)"
exit 1
fi
current_size=$(du -s target | cut -f1)
current_size_gb=$(( ${current_size} / 1024 / 1024 ))
echo "target directory size: ${current_size_gb}gb. max size: ${max_size_gb}gb"
if [[ ${current_size_gb} -gt ${max_size_gb} ]]; then
echo "clearing target directory"
shopt -s dotglob
rm -rf target/*
elif [[ -n "${small_clean_size_gb}" && ${current_size_gb} -gt ${small_clean_size_gb} ]]; then
echo "running cargo clean --workspace (size above small clean threshold of ${small_clean_size_gb}gb)"
cargo clean --workspace
fi