109 lines
3.6 KiB
Fish
109 lines
3.6 KiB
Fish
function fbrew --description "Fuzzy homebrew"
|
|
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
|
|
end
|
|
|
|
function _fbrew_install
|
|
set --local inst_pkgs $argv
|
|
|
|
if test (count $inst_pkgs) -eq 0
|
|
set inst_pkgs (brew search | eval "fzf --prompt='[brew:install] '")
|
|
end
|
|
|
|
if test (count $inst_pkgs) -gt 0
|
|
echo "Installing: $inst_pkgs"
|
|
brew install $inst_pkgs
|
|
end
|
|
end
|
|
|
|
function _fbrew_uninstall
|
|
set --local uninst_pkgs $argv
|
|
|
|
# Don't uninstall these packages
|
|
set --local locked_pkgs node python
|
|
|
|
if test (count $uninst_pkgs) -eq 0
|
|
set uninst_pkgs (brew leaves | eval "fzf --prompt='[brew:uninstall] '")
|
|
end
|
|
|
|
if test (count $uninst_pkgs) -gt 0
|
|
echo "Determining package dependencies..."
|
|
|
|
# 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_final) -gt 0
|
|
echo "Uninstalling: $uninst_pkgs_final"
|
|
brew uninstall $uninst_pkgs_final
|
|
end
|
|
|
|
if test (count $uninst_pkgs_leave) -gt 0
|
|
echo "Will not uninstall: $uninst_pkgs_leave"
|
|
end
|
|
end
|
|
end
|