-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
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
Add brew shellenv - see #4780 #4849
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, thanks @jamescostian and sorry for all the back and forth! Is this sufficient for your use-case @sjackman? If so, mind ✅
@@ -0,0 +1,7 @@ | |||
describe "brew shellenv", :integration_test do | |||
it "doesn't fail" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you assert_match
on e.g. the HOMEBREW_PREFIX/bin
being present?
Looks good to me. Thanks, James! The one minor test change suggested by make would be good, to check that stdout contains |
After this is merge into Homebrew/brew and Linuxbrew/brew, we can update the installation instructions. |
Just added that to the test 👍 |
Merged. Thanks for your contribution to Homebrew, James! 🏆 |
There's two ways that ~/.linuxbrew/bin/brew shellenv >>~/.profile or echo 'eval $(~/.linuxbrew/bin/brew shellenv)' >>~/.profile I have a slight preference for the latter using |
Running |
Agreed. It's 0.9s in my Docker instance. ⏱ |
So the installation instructions should be… eval $(~/.linuxbrew/bin/brew shellenv)
brew shellenv >>~/.profile |
@sjackman My recommendation would be to advise people put Another possible extension for this script would be exporting e.g. Nice work @jamescostian! |
Is that so that we can change the output of I suspect that implementing this script in shell rather than Ruby would execute quite a bit faster. If it's going in |
Yep 👍
It would indeed. Given it only relies on |
@jamescostian Would you be interested in submitting a PR to rewrite |
Sure, the bash rewrite is pretty simple: echo "export PATH=\"$HOMEBREW_PREFIX/bin:$HOMEBREW_PREFIX/sbin:\$PATH\""
echo "export MANPATH=\"$HOMEBREW_PREFIX/share/man:\$MANPATH\""
echo "export INFOPATH=\"$HOMEBREW_PREFIX/share/info:\$INFOPATH\""
echo "export HOMEBREW_PREFIX=\"$HOMEBREW_PREFIX\"" But there are two issues:
That execution time seems to be impacted most from this line and this line. They both talk to
--- a/bin/brew
+++ b/bin/brew
@@ -65,7 +65,7 @@ do
done
# test-bot does environment filtering itself
-if [[ -z "$HOMEBREW_NO_ENV_FILTERING" && "$1" != "test-bot" ]]
+if [[ -z "$HOMEBREW_NO_ENV_FILTERING" && "$1" != "test-bot" && "$1" != "shellenv" ]]
then
PATH="/usr/bin:/bin:/usr/sbin:/sbin"
@@ -82,6 +82,11 @@ then
done
exec /usr/bin/env -i "${FILTERED_ENV[@]}" /bin/bash "$HOMEBREW_LIBRARY/Homebrew/brew.sh" "$@"
+elif [[ "$1" == "shellenv" ]]
+then
+ echo "export PATH=\"$HOMEBREW_PREFIX/bin:$HOMEBREW_PREFIX/sbin:\$PATH\""
+ echo "export MANPATH=\"$HOMEBREW_PREFIX/share/man:\$MANPATH\""
+ echo "export INFOPATH=\"$HOMEBREW_PREFIX/share/info:\$INFOPATH\""
+ echo "export HOMEBREW_PREFIX=\"$HOMEBREW_PREFIX\""
else
source "$HOMEBREW_LIBRARY/Homebrew/brew.sh"
fi |
Update: here's a much less ugly approach (or at least, it's already been used elsewhere in the code): --- a/Library/Homebrew/brew.sh
+++ b/Library/Homebrew/brew.sh
@@ -17,6 +17,10 @@ case "$*" in
--prefix) echo "$HOMEBREW_PREFIX"; exit 0 ;;
--cellar) echo "$HOMEBREW_CELLAR"; exit 0 ;;
--repository|--repo) echo "$HOMEBREW_REPOSITORY"; exit 0 ;;
+ shellenv) echo "export PATH=\"$HOMEBREW_PREFIX/bin:$HOMEBREW_PREFIX/sbin:\$PATH\"";
+ echo "export MANPATH=\"$HOMEBREW_PREFIX/share/man:\$MANPATH\"";
+ echo "export INFOPATH=\"$HOMEBREW_PREFIX/share/info:\$INFOPATH\"";
+ echo "export HOMEBREW_PREFIX=\"$HOMEBREW_PREFIX\"";
+ exit 0 ;;
esac
# A depth of 1 means this command was directly invoked by a user. This approach also brings the time down to .01s |
I have a mild preference for the following style over using backslashes to escape. echo "export PATH='$HOMEBREW_PREFIX/bin:$HOMEBREW_PREFIX/sbin'":'"$PATH"' I'll defer to Mike on your question regarding speed. |
You need to put your code inside a function On my machine there's still a 3.5x speedup by moving this from Ruby to shell ( |
brew style
with your changes locally?brew tests
with your changes locally?Adds
brew shellenv
based on conversation in #4780