Files
tigerbrew/bin/brew
T
Max Howell 7be9788a9f Don't look broken if CWD doesn't exist
Ruby throws if CWD doesn't exist, even when you aren't doing anything related to the CWD. Sucks. So at least show the user a friendly error message rather than looking broken.
2009-10-23 19:20:12 +01:00

275 lines
8.4 KiB
Ruby
Executable File

#!/usr/bin/ruby
# -*- coding: utf-8 -*-
# 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 homebrew_rubylib_path
lib_path = "/../../Library/Homebrew"
# we resolve off of Dir.getwd as otherwise the symlink gets resolved!
brew_path = if not File.symlink? __FILE__ or File.exist? Dir.getwd+'/'+__FILE__+lib_path
# standard 100% symlinked or non-symlinked installation
__FILE__
else
# non-standard installation -- just this script is symlinked
File.readlink(__FILE__)
end
return File.expand_path(brew_path+lib_path)
end
$:.unshift homebrew_rubylib_path
require 'global'
case ARGV.first
when '--cache'
puts HOMEBREW_CACHE
exit 0
when '-h', '--help', '--usage', '-?'
puts ARGV.usage
exit 0
when '--version'
puts HOMEBREW_VERSION
exit 0
when '-v'
if ARGV.length > 1
puts "Homebrew #{HOMEBREW_VERSION}"
# continue in verbose mode
ARGV << ARGV.shift
else
puts HOMEBREW_VERSION
exit 0
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 MACOS_VERSION < 10.5
abort "Homebrew requires Leopard or higher, but you could fork it and fix that..."
end
case Hardware.cpu_type when :ppc, :dunno
abort "Sorry, Homebrew does not support your computer's CPU architecture."
end
begin
require 'brew.h'
case ARGV.shift
when '--prefix'
puts HOMEBREW_PREFIX
when 'home', 'homepage'
if ARGV.named_empty?
exec "open", HOMEBREW_WWW
else
exec "open", *ARGV.formulae.collect {|f| f.homepage}
end
when 'ls', 'list'
if ARGV.flag? '--unbrewed'
dirs = HOMEBREW_PREFIX.children.select { |pn| pn.directory? }.collect { |pn| pn.basename.to_s }
dirs -= ['Library', 'Cellar', '.git']
Dir.chdir HOMEBREW_PREFIX
exec 'find', *dirs + %w[-type f ( ! -iname .ds_store ! -iname brew )]
elsif ARGV.named_empty?
ENV['CLICOLOR']=nil
exec 'ls', *ARGV.options<<HOMEBREW_CELLAR if HOMEBREW_CELLAR.exist?
elsif ARGV.verbose? or not $stdout.tty?
exec "find", *ARGV.kegs+%w[-not -type d -print]
else
ARGV.kegs.each { |keg| PrettyListing.new keg }
end
when 'search', '-S'
formulae = (HOMEBREW_PREFIX+'Library'+'Formula').children.sort.map{|f| f.basename('.rb') }
if ARGV.first =~ /^\/(.*)\/$/
puts_columns formulae.grep(Regexp.new($1))
else
puts_columns formulae.grep(/.*#{ARGV.first}.*/)
end
when 'edit'
if ARGV.named_empty?
# EDITOR isn't a good fit here, we need a GUI client that actually has
# a UI for projects, so apologies if this wasn't what you expected,
# please improve it! :)
exec 'mate', *Dir["#{HOMEBREW_PREFIX}/Library/*"]<<
"#{HOMEBREW_PREFIX}/bin/brew"<<
"#{HOMEBREW_PREFIX}/README.md"
else
exec_editor *ARGV.formulae.collect {|f| f.path}
end
when 'install'
if ARGV.named_empty?
puts "You must specify a formula. Search for available formulae with `brew search'."
exit 0
end
raise "Cannot write to #{HOMEBREW_PREFIX}" unless HOMEBREW_PREFIX.writable?
if ARGV.interactive? and ARGV.formulae.length > 1
# the reason for this is interactive mode is a little tricky to do
# with more than one formula, AND I can't think of a time where you'd
# want to do it anyway. If someone comes up with a legitimate use for
# this we will adapt the code. "But I might want it!" is not a
# legitimate use!
raise "Interactive mode can only be used with one formula argument"
end
warn_about_macports_or_fink # keep warning before dependency resolution
unless ARGV.force?
formulae = ARGV.formulae.reject do |f|
if f.installed?
message = "Formula already installed: #{f.prefix}"
if ARGV.formulae.length > 1
opoo message
else
puts message # if only one is being installed a warning looks severe
end
true
end
end
exit 0 if formulae.empty?
else
formulae = ARGV.formulae
end
unless ARGV.include? '--ignore-dependencies'
deps = []
formulae.each { |f| deps += expand_deps f }
formulae = deps.reject { |f| f.installed? }
if formulae.length > 1 and ARGV.interactive?
# because current code is a mess
raise "Please install the formula's dependencies before entering interactive mode"
end
end
require 'set'
done = Set.new
require 'beer_events'
watch_out_for_spill do
formulae.each do |f|
next if done.include? f.class
done << f.class
# 1. formulae can modify ENV, so we must ensure that each
# installation has a pristine ENV when it starts, forking now is
# the easiest way to do this
# 2. formulae have access to __END__ the only way to allow this is
# to make the formula script the executed script
pid=fork
if pid.nil?
exec '/usr/bin/ruby', '-I', homebrew_rubylib_path, '-rinstall', f.path, '--', *ARGV.options
else
Process.wait pid
end
#FIXME I don't think $? represents the exit code from the child fork…
exit! $? if $? != 0 # exception in other brew will be visible on screen
end
end
when 'up', 'update'
require 'update'
updater = RefreshBrew.new
old_revision = updater.current_revision
unless updater.update_from_masterbrew!
puts "Already up-to-date."
else
puts "Updated Homebrew from #{old_revision[0,8]} to #{updater.current_revision[0,8]}."
if updater.pending_formulae_changes?
ohai "The following formulae were updated:"
puts_columns updater.updated_formulae
else
puts "No formulae were updated." unless updater.pending_formulae_changes?
end
end
when 'ln', 'link'
ARGV.kegs.each {|keg| puts "#{keg.link} links created for #{keg}"}
when 'unlink'
ARGV.kegs.each {|keg| puts "#{keg.unlink} links removed for #{keg}"}
when 'rm', 'uninstall', 'remove'
ARGV.kegs.each do |keg|
puts "Uninstalling #{keg}..."
keg.uninstall
end
prune
when 'prune'
prune
# 'make' supported until 0.7 for historic reasons
when 'mk', 'make'
opoo "`brew make' has changed to `brew create'"
puts "This is because make can be confused with the `make' tool."
puts "brew make will continue working until Homebrew 0.7"
exec __FILE__, "create", *ARGV
when 'create'
if ARGV.include? '--macports'
exec "open", "http://www.macports.org/ports.php?by=name&substr=#{ARGV.next}"
else
exec_editor *ARGV.named.collect {|name| make name}
end
when 'diy', 'configure'
puts diy
when 'info', 'abv'
if ARGV.named_empty?
puts `ls #{HOMEBREW_CELLAR} | wc -l`.strip+" kegs, "+HOMEBREW_CELLAR.abv
elsif ARGV[0][0..6] == 'http://'
puts Pathname.new(ARGV.shift).version
else
ARGV.named.each {|name| info name}
end
when 'log'
Dir.chdir HOMEBREW_PREFIX
exec "git", "log", ARGV.formulae.first.path, *ARGV.options
else
puts ARGV.usage
end
rescue UsageError
onoe "Invalid usage"
puts ARGV.usage
exit 1
rescue SystemExit
ohai "Kernel.exit" if ARGV.verbose?
exit 1
rescue Interrupt => e
# puts # seemingly a newline is typical
# Above is now commented out because the system() call forks and then forks
# again, so there are two of "us" so we get two exceptions raising and thus
# two newlines, which buggers up the shell. FIXME!
exit 130
rescue SystemCallError, RuntimeError => e
if ARGV.debug?
onoe e.inspect
puts e.backtrace
else
onoe e
end
exit 1
rescue Exception => e
onoe "Homebrew has failed you :("
puts "Please report this bug at: #{HOMEBREW_WWW}"
puts "Please include the following information:"
ohai "Environment"
puts "Mac OS X: "+`sw_vers -productVersion`
ohai e.inspect
puts e.backtrace
exit 1
end