aboutsummaryrefslogtreecommitdiffstats
path: root/bin/brew
blob: 47215866c6bb96499c58162b3996f2c7bcbc9fa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/ruby
$:.unshift ENV['RUBYLIB']=File.expand_path(__FILE__+'/../../Library/Homebrew')

require 'pathname+yeast'
require 'ARGV+yeast'
require 'utils'
require 'brew.h'

# TODO if whoami == root then use /Library/Caches/Homebrew instead
HOMEBREW_CACHE=Pathname.new("~/Library/Caches/Homebrew").expand_path
HOMEBREW_PREFIX=Pathname.new(__FILE__).dirname.parent.cleanpath
HOMEBREW_CELLAR=HOMEBREW_PREFIX+'Cellar'
HOMEBREW_VERSION='0.4'
HOMEBREW_WWW='http://bit.ly/Homebrew'
HOMEBREW_USER_AGENT="Homebrew #{HOMEBREW_VERSION} (Ruby #{VERSION}; Mac OS X 10.5 Leopard)"

if %w[/ /usr].include? HOMEBREW_PREFIX.to_s then abort <<-troba
You have placed Homebrew at the prefix: #{HOMEBREW_PREFIX}
This is not currently supported. Voice your support for this feature at:
#{HOMEBREW_WWW}
troba
end

if `sw_vers -productVersion` =~ /10\.(\d)\.(\d+)/ and $1.to_i < 5
  onoe "Homebrew requires Leopard or higher"
  abort "But thanks for your interest anyway!"
end

# Pathname often throws if CWD doesn't exist
Dir.chdir '/' unless File.directory? ENV['PWD']


begin
  case ARGV.shift
    when '--prefix' then puts HOMEBREW_PREFIX
    when '--cache' then puts HOMEBREW_CACHE
    when '-h', '--help', '--usage', '-?' then puts ARGV.usage
    when '-v', '--version' then puts HOMEBREW_VERSION

    when 'home', 'homepage'
      if ARGV.named_empty?
        exec "open", HOMEBREW_WWW
      else
        exec "open", *ARGV.formulae.collect {|f| f.homepage}
      end

    when 'ls', 'list'
      exec "find", *ARGV.kegs+%w[-not -type d -print]

    when 'edit'
      if ARGV.named_empty?
        exec "mate", *Dir["#{HOMEBREW_PREFIX}/Library/*"]<<
                          "#{HOMEBREW_PREFIX}/bin/brew"<<
                          "#{HOMEBREW_PREFIX}/README"
      else
        exec "mate", *ARGV.formulae.collect {|f| f.path}
      end

    when 'install'
      # we need to ensure a pristine ENV for each process or the formula
      # will start with the ENV from the previous build
      ARGV.formulae.each do |f|
        pid=fork
        if pid.nil?
          exec "brew", "install-just-one", f.name, *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

    # this is an internal option, don't expose it to the user
    when 'install-just-one'
      require 'keg'
      f=ARGV.formulae.shift
      raise "#{f.name} is already installed" if f.installed? unless ARGV.force?
      BEGINNING=Time.now
      begin
        install f
        ohai "Caveats", f.caveats, ''
        ohai 'Finishing up'
        clean f
        raise "Nothing was installed to #{f.prefix}" unless f.installed?
        Keg.new(f.prefix).link
      rescue Exception
        f.prefix.rmtree if f.prefix.directory?
        raise
      end
      puts "#{f.prefix}: #{f.prefix.abv}, built in #{pretty_duration Time.now-BEGINNING}"

    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 'up', 'update'
      puts "Reserved command"

    when 'prune'
      prune

    when 'mk', 'make'
      if ARGV.include? '--macports'
        exec "open", "http://www.macports.org/ports.php?by=name&substr=#{ARGV.next}"
      else
        exec "mate", *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

    else
      puts ARGV.usage
  end

rescue UsageError
  onoe "Invalid usage"
  puts ARGV.usage
rescue SystemExit
  ohai "Kernel.exit" if ARGV.verbose?
rescue Interrupt => e
  puts # seemingly a newline is typical
  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 this backtrace:"
  ohai e.inspect
  puts e.backtrace
end