Move some brew.rb logic to bin/brew.

This commit is contained in:
Mike McQuaid 2016-01-10 19:35:16 +00:00
parent b01ce41164
commit 6f91b429ce
2 changed files with 71 additions and 56 deletions

View File

@ -4,11 +4,6 @@ std_trap = trap("INT") { exit! 130 } # no backtrace thanks
HOMEBREW_BREW_FILE = ENV["HOMEBREW_BREW_FILE"]
if ARGV == %w[--prefix]
puts File.dirname(File.dirname(HOMEBREW_BREW_FILE))
exit 0
end
require "pathname"
HOMEBREW_LIBRARY_PATH = Pathname.new(__FILE__).realpath.parent.join("Homebrew")
$:.unshift(HOMEBREW_LIBRARY_PATH.to_s)
@ -22,38 +17,6 @@ elsif ARGV.first == "-v"
ARGV << ARGV.shift
end
if OS.mac?
# Check for bad xcode-select before other checks, because `doctor` and
# many other things will hang. Note that this bug was fixed in 10.9
if MacOS.version < :mavericks && MacOS.active_developer_dir == "/"
odie <<-EOS.undent
Your xcode-select path is currently set to '/'.
This causes the `xcrun` tool to hang, and can render Homebrew unusable.
If you are using Xcode, you should:
sudo xcode-select -switch /Applications/Xcode.app
Otherwise, you should:
sudo rm -rf /usr/share/xcode-select
EOS
end
# Check for user agreement of the Xcode license before permitting
# any other brew usage to continue. This prevents the situation where
# people are instructed to "please re-run as root via sudo" on brew commands.
# The check can only fail when Xcode is installed & the active developer dir.
if MacOS::Xcode.installed? && `/usr/bin/xcrun clang 2>&1` =~ /license/ && !$?.success?
odie <<-EOS.undent
You have not agreed to the Xcode license. Please resolve this by running:
sudo xcodebuild -license
EOS
end
end
case HOMEBREW_PREFIX.to_s
when "/", "/usr"
# it may work, but I only see pain this route and don't want to support it
abort "Cowardly refusing to continue at this prefix: #{HOMEBREW_PREFIX}"
end
if OS.mac? && MacOS.version < "10.6"
abort <<-EOABORT.undent
Homebrew requires Snow Leopard or higher. For Tiger and Leopard support, see:
@ -61,10 +24,6 @@ if OS.mac? && MacOS.version < "10.6"
EOABORT
end
# Many Pathname operations use getwd when they shouldn't, and then throw
# odd exceptions. Reduce our support burden by showing a user-friendly error.
Dir.getwd rescue abort "The current working directory doesn't exist, cannot proceed."
def require?(path)
require path
rescue LoadError => e
@ -94,20 +53,6 @@ begin
cmd = HOMEBREW_INTERNAL_COMMAND_ALIASES.fetch(cmd, cmd)
sudo_check = %w[ install reinstall postinstall link pin unpin
update upgrade create migrate tap switch ]
if sudo_check.include? cmd
if Process.uid.zero? && !File.stat(HOMEBREW_BREW_FILE).uid.zero?
raise <<-EOS.undent
Cowardly refusing to `sudo brew #{cmd}`
You can use brew with sudo, but only if the brew executable is owned by root.
However, this is both not recommended and completely unsupported so do so at
your own risk.
EOS
end
end
# Add contributed commands to PATH before checking.
Dir["#{HOMEBREW_LIBRARY}/Taps/*/*/cmd"].each do |tap_cmd_dir|
ENV["PATH"] += "#{File::PATH_SEPARATOR}#{tap_cmd_dir}"

View File

@ -37,6 +37,19 @@ else
HOMEBREW_CELLAR="$HOMEBREW_REPOSITORY/Cellar"
fi
case "$*" in
--prefix) echo "$HOMEBREW_PREFIX"; exit 0 ;;
--cellar) echo "$HOMEBREW_CELLAR"; exit 0 ;;
--repository|--repo) echo "$HOMEBREW_REPOSITORY"; exit 0 ;;
esac
if [ "$HOMEBREW_PREFIX" = "/" ] || [ "$HOMEBREW_PREFIX" = "/usr" ]
then
# it may work, but I only see pain this route and don't want to support it
echo "Cowardly refusing to continue at this prefix: $HOMEBREW_PREFIX" >&2
exit 1
fi
# Users may have these set, pointing the system Ruby
# at non-system gem paths
unset GEM_HOME
@ -47,9 +60,14 @@ then
unset HOMEBREW_RUBY_PATH
fi
if [ "$(uname -s)" = "Darwin" ]
then
HOMEBREW_OSX="1"
fi
if [ -z "$HOMEBREW_RUBY_PATH" ]
then
if [ "$(uname -s)" = "Darwin" ]
if [ -n "$HOMEBREW_OSX" ]
then
HOMEBREW_RUBY_PATH="/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby"
else
@ -64,6 +82,42 @@ export HOMEBREW_REPOSITORY
export HOMEBREW_LIBRARY
export HOMEBREW_CELLAR
if [ -n "$HOMEBREW_OSX" ]
then
if [ "$('xcode-select' --print-path)" = "/" ]
then
cat >&2 <<EOS
Your xcode-select path is currently set to '/'.
This causes the 'xcrun' tool to hang, and can render Homebrew unusable.
If you are using Xcode, you should:
sudo xcode-select -switch /Applications/Xcode.app
Otherwise, you should:
sudo rm -rf /usr/share/xcode-select
EOS
exit 1
fi
XCRUN_OUTPUT="$(/usr/bin/xcrun clang 2>&1)"
XCRUN_STATUS="$?"
if [ "$XCRUN_STATUS" -ne 0 ] && [[ "$XCRUN_OUTPUT" = *license* ]]
then
cat >&2 <<EOS
You have not agreed to the Xcode license. Please resolve this by running:
sudo xcodebuild -license
EOS
exit 1
fi
fi
# Many Pathname operations use getwd when they shouldn't, and then throw
# odd exceptions. Reduce our support burden by showing a user-friendly error.
if ! [ -d "$(pwd)" ]
then
echo "The current working directory doesn't exist, cannot proceed." >&2
exit 1
fi
for i in "$@"
do
if [[ "$1" = -v ]]
@ -72,10 +126,26 @@ do
set -- "$@" -v
fi
[[ "$i" =~ ^- ]] && continue
HOMEBREW_COMMAND="$i"
HOMEBREW_BASH_COMMAND="$HOMEBREW_LIBRARY/Homebrew/cmd/$i.sh"
break
done
if [ "$(id -u)" = "0" ] && [ "$(stat -f%u "$HOMEBREW_BREW_FILE")" != "0" ]
then
case "$HOMEBREW_COMMAND" in
install|reinstall|postinstall|link|pin|unpin|update|update-bash|upgrade|create|migrate|tap|switch)
cat >&2 <<EOS
Cowardly refusing to 'sudo brew $HOMEBREW_COMMAND'
You can use brew with sudo, but only if the brew executable is owned by root.
However, this is both not recommended and completely unsupported so do so at
your own risk.
EOS
exit 1
;;
esac
fi
if [ -n "$HOMEBREW_BASH_COMMAND" ] && [ -x "$HOMEBREW_BASH_COMMAND" ]
then
# source rather than executing directly to ensure the entire file is read into