Created
May 9, 2020 21:18
-
-
Save gtklocker/5b6fe5d781c414410874bae3cee473d9 to your computer and use it in GitHub Desktop.
Recursively remove homebrew packages
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
#!/bin/zsh | |
set -euo pipefail | |
local preall=$(brew list -1) | |
local predeps=$(brew deps --installed) | |
local preleaves=$(brew leaves) | |
recur() { | |
local pkg=$1 | |
local deps=$(echo "$predeps"|grep "^$pkg:"|cut -d':' -f2|xargs|tr ' ' '\n') | |
echo "> Zapping $pkg..." | |
brew uninstall "$pkg" || { | |
if echo "$preall"|grep -q "^$pkg$"; then | |
echo "> Package depended on by another package, skipping." | |
return | |
fi | |
} | |
echo "Dependencies: $(echo "$deps"|tr '\n' ',')" | |
test -z "$deps" || { | |
echo $deps|while read dep; do | |
if ! echo $preleaves|grep -q $dep; then | |
recur "$dep" | |
else | |
echo "> Skipping $dep as it was a leaf before." | |
fi | |
done | |
} | |
} | |
recur "$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment