diff --git a/init.fish b/init.fish index 00c944c..e5fd0fe 100644 --- a/init.fish +++ b/init.fish @@ -1,24 +1,22 @@ -if type -q brew - set -l brew_paths /usr/local/bin /usr/bin /bin /usr/local/sbin /usr/sbin /sbin +if status is-login + # check $HOMEBREW_BIN and set $brew + set -q HOMEBREW_BIN + and set -l brew $HOMEBREW_BIN + or set -l brew /usr/local/bin/brew - # Append all existing brew paths to PATH - set -l existing_brew_paths - for brew_path in $brew_paths - if test -d $brew_path - set PATH $PATH $brew_path - set existing_brew_paths $existing_brew_paths $brew_path - end - end - - # Remove brew paths from tail to head that were not recently added - set -l number_of_paths_to_ignore (math (count $PATH) - (count $existing_brew_paths)) - for i in (seq (count $PATH))[-1..1] - if test $i -le $number_of_paths_to_ignore - if contains $PATH[$i] $brew_paths - set -e PATH[$i] - end - end - end -else - echo "Please install 'brew' first!" + # By default, `brew shellenv` sets $fish_user_paths as a global variable instead of directly + # setting $PATH. This behavior should be avoided and thus is overriden here, since setting + # a global $fish_user_paths will shadow the universal $fish_user_paths, which is supposed + # to be set manually by users by design. + if type -q $brew; and set cmd ($brew shellenv | sed 's/fish_user_paths/PATH/g') ^ /dev/null + # initialized brew shell environment if $brew is valid + eval $cmd + set -gx HOMEBREW_BIN $brew + set -gxp PATH + else + echo "Error! 'brew' not found at $brew." + echo "Please make sure Homebrew is installed and \$HOMEBREW_BIN is correctly set!" + echo "To install Homebrew, please check https://brew.sh" + echo "Note that setting \$HOMEBREW_BIN is optional if the default Homebrew installation path is used." + end >& 2 end