Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set environment with brew shellenv #9

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions init.fish
Original file line number Diff line number Diff line change
@@ -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