Skip to content

Instantly share code, notes, and snippets.

@casperisfine
Created June 15, 2022 15:49
Show Gist options
  • Save casperisfine/a68716bd23c8ca6a9744b67ddf48ae2a to your computer and use it in GitHub Desktop.
Save casperisfine/a68716bd23c8ca6a9744b67ddf48ae2a to your computer and use it in GitHub Desktop.
#!/bin/sh
set -eu
AGGRESSIVE=0
PARAMS=""
while [ "$#" -gt 0 ]; do
case "$1" in
--aggressive)
AGGRESSIVE=1
shift
;;
-*)
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
# set positional arguments in their proper place
eval set -- "$PARAMS"
bundler_root="${1}"
if command -v du > /dev/null 2>&1; then
echo "Cache size before pruning:"
du -sh "${bundler_root}"
fi
# Another cache; no value once all gems are installed
rm -rf "${bundler_root}"/ruby/*/cache
# git dirs of installed gems are useless once we no longer have to update them
rm -rf "${bundler_root}"/ruby/*/bundler/gems/*/.git
if command -v xargs > /dev/null 2>&1; then
if [ "${AGGRESSIVE}" -eq 1 ]; then
# Normally gems should include their own test suite. But some gems are misconfigured
# and git gems are not filtered
# We exclude any path containing `/lib/` to avoid false positives like `rack-test/lib/rack/test`
#
# However it can happen that some gems expose a shared test suite, so this pruning is optional.
find "${bundler_root}" -type d \( \
-name 'spec' \
-o -name 'test' \
-o -name 'tests' \
\) | grep -v '\/lib\/' | xargs -n 100 rm -rf
fi
# Clean various source file of native gems
find "${bundler_root}" -type f \( \
-iname '*.a' \
-o -iname '*.o' \
-o -iname '*.h' \
-o -iname '*.c' \
-o -iname '*.d' \
-o -iname '*.hpp' \
-o -iname '*.cpp' \
-o -name 'mkmf.log' \
-o -name 'gem_make.out' \
\) -print0 | xargs -0 -n 100 rm -f
fi
if command -v du > /dev/null 2>&1; then
echo "Cache size after pruning:"
du -sh "${bundler_root}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment