config/fish/functions/fbrew.fish

119 lines
4.2 KiB
Fish

function fbrew --description "Fuzzy homebrew"
# Helper function for 'brew install'
function _fbrew_install
set --local inst_pkgs $argv
if test (count $inst_pkgs) -eq 0
set inst_pkgs (brew formulae | eval "fzf --multi --prompt='[brew:install] '")
end
if test (count $inst_pkgs) -gt 0
commandline -r "brew install $inst_pkgs"
commandline -f execute
end
functions --erase _fbrew_install
end
# Helper function for 'brew uninstall'
function _fbrew_uninstall
set --local uninst_pkgs $argv
# Don't uninstall these packages
set --local locked_pkgs node python python@3.8 python@3.9
if test (count $uninst_pkgs) -eq 0
set uninst_pkgs (brew leaves | eval "fzf --multi --prompt='[brew:uninstall] '")
end
if test (count $uninst_pkgs) -gt 0
echo "Reticulating splines..."
# Output is multiline with format "pkgname:" if no dependencies, otherwise "pkgname: dep1 dep2 dep3 ..."
set --local brew_deps_installed (string collect (brew deps --installed))
# Accumulate packages and their dependencies
set --local uninst_pkgs_all $uninst_pkgs
set --local locked_pkgs_all $locked_pkgs
for bdi in $brew_deps_installed
set --local pkg (string split ':' $bdi | head -n 1)
set --local deps (string split ' ' (string split ':' $bdi | tail -n +2))
if contains -- $pkg $uninst_pkgs
for dep in $deps
if test -n $dep
if not contains -- $dep $uninst_pkgs_all
set uninst_pkgs_all $uninst_pkgs_all $dep
end
end
end
end
if contains -- $pkg $locked_pkgs
for dep in $deps
if test -n $dep
if not contains -- $dep $locked_pkgs_all
set locked_pkgs_all $locked_pkgs_all $dep
end
end
end
end
end
# Don't uninstall dependencies of other installed packages
set --local uninst_pkgs_final $uninst_pkgs_all
for bdi in $brew_deps_installed
set --local pkg (string split ':' $bdi | head -n 1)
set --local deps (string split ' ' (string split ':' $bdi | tail -n +2))
if not contains -- $pkg $uninst_pkgs_all
for dep in $deps
if test -n $dep
if set --local index (contains --index -- $dep $uninst_pkgs_final)
set --erase uninst_pkgs_final[$index]
end
end
end
end
end
# Don't uninstall dependencies of locked packages
set --local uninst_pkgs_leave
for pkg in $locked_pkgs_all
if test -n $pkg
if set --local index (contains --index -- $pkg $uninst_pkgs_final)
set uninst_pkgs_leave $uninst_pkgs_leave $pkg
set --erase uninst_pkgs_final[$index]
end
end
end
if test (count $uninst_pkgs_leave) -gt 0
echo "Will not uninstall: $uninst_pkgs_leave"
end
if test (count $uninst_pkgs_final) -gt 0
commandline -r "brew uninstall $uninst_pkgs_final"
commandline -f execute
end
end
functions --erase _fbrew_uninstall
end
if test (count $argv) -eq 0
echo "Usage: fbrew <install|uninstall>"
return 1
end
switch $argv[1]
case i install
_fbrew_install $argv[2..-1]
case u uninstall
_fbrew_uninstall $argv[2..-1]
case '*'
echo "Unsupported command: $argv[1]"
end
functions --erase _fbrew_install
functions --erase _fbrew_uninstall
end