brew/bin/brew

304 lines
7.4 KiB
Plaintext
Raw Normal View History

2009-05-21 00:04:43 +01:00
#!/usr/bin/ruby
$:.unshift __FILE__+'/../../Library/Homebrew'
require 'env'
require 'find'
2009-05-21 00:04:43 +01:00
PRISTINE_ARGV=ARGV.dup
2009-06-26 13:01:23 +01:00
# often causes Ruby to throw exception ffs
Dir.chdir '/' unless File.directory? ENV['PWD']
2009-06-26 13:01:23 +01:00
######################################################################## funcs
# remove symlinks that no longer point to files
2009-05-22 00:09:08 +01:00
def prune
n=0
dirs=Array.new
HOMEBREW_PREFIX.find do |path|
2009-05-22 00:09:08 +01:00
if path.directory?
name=path.relative_path_from(HOMEBREW_PREFIX).to_s
2009-07-28 00:33:08 +01:00
if name == '.git' or name == 'Cellar' or name == 'Library'
Find.prune
else
dirs<<path
end
2009-05-22 00:09:08 +01:00
elsif path.symlink?
resolved_path=path.dirname+path.readlink
unless resolved_path.exist?
path.unlink
n+=1
end
2009-05-22 00:09:08 +01:00
end
end
dirs.sort.reverse_each do |d|
if d.children.length == 0
d.rmdir
n+=1
end
end
return n
2009-05-22 00:09:08 +01:00
end
# we actually remove formulae from ARGV so that any other analysis of ARGV
# only includes relevent arguments
# TODO require will throw if no formula, so we should catch no?
def extract_named_args
args=Array.new
ARGV.delete_if do |arg|
if arg[0,1] == '-'
false
2009-06-05 12:57:00 +01:00
else
args<<arg
true
2009-06-05 12:57:00 +01:00
end
end
raise "Expecting the name of a keg or formula, eg:\n==> brew #{PRISTINE_ARGV.join ' '} wget" if args.empty?
return args
end
def extract_kegs
require 'keg'
kegs=extract_named_args.collect {|name| Keg.new name}
return kegs
end
def abv keg=nil
path=keg ? keg.path : HOMEBREW_CELLAR
if path.directory?
`find #{path} -type f | wc -l`.strip+' files, '+`du -hd0 #{path} | cut -d"\t" -f1`.strip
else
2009-07-31 01:15:58 +01:00
''
end
end
def install formula
require 'keg'
beginning = Time.now
formula.brew do
if ARGV.include? '--interactive'
ohai "Entering interactive mode"
puts "Type `exit' to return and finalize the installation"
puts "Install to this prefix: #{formula.prefix}"
pid=fork
if pid.nil?
exec 'bash'
else
Process.wait pid
end
elsif ARGV.include? '--help'
ohai './configure --help'
puts `./configure --help`
exit
else
formula.prefix.mkpath
formula.install
2009-07-31 00:14:08 +01:00
%w[README ChangeLog COPYING LICENSE COPYRIGHT AUTHORS].each do |file|
formula.prefix.install file if File.file? file
end
end
end
2009-08-01 17:43:54 +01:00
raise "Nothing was installed to #{formula.prefix}" unless formula.installed?
ohai 'Finishing up'
keg=Keg.new formula
keg.clean
keg.ln
if formula.caveats
ohai "Caveats"
puts formula.caveats
ohai "Summary"
end
puts "#{keg.path}: "+abv(keg)+", built in #{pretty_duration Time.now-beginning}"
rescue Exception
formula.prefix.rmtree if formula.prefix.directory?
raise
end
def mk url, mode='make'
require 'formula'
path=Pathname.new(url)
/(.*?)[-_.]?#{path.version}/.match path.basename
raise "Couldn't parse name from #{url}" if $1.nil? or $1.empty?
path=Formula.path $1
raise "#{path} already exists!" if File.exist? path
f=File.new path, 'w'
f.puts "require 'brewkit'"
f.puts
f.puts "class #{Formula.class $1} <Formula"
f.puts " @url='#{url}'"
f.puts " @homepage=''" # second because you fill in these two first
f.puts " @md5=''"
f.puts
if mode == "cmake"
f.puts " def deps"
f.puts " BinaryDep.new 'cmake'"
f.puts " end"
f.puts
end
f.puts " def install"
if mode == "make"
f.puts " system \"./configure --disable-debug --prefix='\#{prefix}'\""
f.puts " system \"make install\""
elsif mode == "cmake"
f.puts " system \"cmake -G 'Unix Makefiles' -DCMAKE_INSTALL_PREFIX=\#{prefix}\""
f.puts " system \"make\""
f.puts " system \"make install\""
end
f.puts " end"
f.print "end"
f.close
return path
end
def prefix
Pathname.new(__FILE__).dirname.parent.expand_path
end
def usage
name=File.basename $0
<<-EOS
2009-06-05 07:51:23 +01:00
Usage: #{name} command [formula] ...
Usage: #{name} [--prefix] [--cache] [--version]
2009-06-18 10:32:53 +01:00
Usage: #{name} [--verbose]
2009-06-05 07:51:23 +01:00
Commands:
2009-06-18 10:32:53 +01:00
install formula ... [--debug] [--interactive]
2009-06-05 08:09:34 +01:00
rm formula ...
list formula ...
ln formula ...
2009-06-13 12:59:18 +01:00
info [formula]
2009-06-05 23:38:58 +01:00
mk url
2009-06-05 07:51:23 +01:00
prune
EOS
end
2009-05-23 16:37:24 +01:00
2009-06-05 08:09:34 +01:00
2009-06-15 01:41:53 +01:00
######################################################################## utils
def pretty_duration s
return "#{(s*1000).to_i} milliseconds" if s < 3
return "#{s.to_i} seconds" if s < 10*60
return "#{(s/60).to_i} minutes"
end
######################################################################### impl
begin
case ARGV.shift
when '--prefix' then puts prefix
when '--cache' then puts Homebrew::cache
when '-h', '--help', '--usage', '-?' then puts usage
when '-v', '--version' then puts HOMEBREW_VERSION
when 'macports' then exec "open 'http://www.macports.org/ports.php?by=name&substr=#{ARGV.shift}'"
when 'home', 'homepage'
homepages=extract_names.collect {|keg| Formula.create(name).homepage}
exec "open #{homepages.join' '}"
when 'ls', 'list'
dirs=extract_kegs.collect {|keg| keg.path}
exec "find #{dirs.join' '} -not -type d -print"
2009-06-26 13:03:49 +01:00
when 'edit'
if ARGV.empty?
r=HOMEBREW_PREFIX
exec "mate #{r}/Library/Formula #{r}/Library/Homebrew #{r}/bin/brew #{r}/README"
2009-06-26 13:03:49 +01:00
else
2009-07-31 01:15:58 +01:00
require 'formula'
paths=extract_named_args.collect {|name| Formula.path(name).to_s.gsub ' ', '\\ '}
exec "mate #{paths.join ' '}"
2009-06-26 13:03:49 +01:00
end
when 'install'
require 'formula'
extract_named_args.each do |name|
f=Formula.create(name)
2009-08-01 17:43:54 +01:00
raise "#{f.name} already installed!\n==> #{f.prefix}" if f.installed? unless ARGV.include? '--force'
install f
2009-06-05 08:09:34 +01:00
end
2009-07-22 20:27:58 +01:00
when 'ln', 'link'
n=0
(kegs=extract_kegs).each do |keg|
n+=nn=keg.ln
puts "Created #{nn} links for #{keg.name}" if kegs.length > 1
end
puts "Created #{n} links"
2009-07-22 20:27:58 +01:00
2009-06-18 09:50:01 +01:00
when 'rm', 'uninstall'
extract_kegs.each do |keg|
puts "Removing #{keg.name}..."
keg.rm
end
print "Pruning #{prefix}/..."
puts " #{prune} symbolic links pruned"
when 'prune'
puts "Pruned #{prune} symbolic links"
when 'mk', 'make'
mode = "make"
if ARGV.length > 0
if ARGV[0] == '--cmake'
ARGV.shift
mode = "cmake"
end
end
paths=ARGV.collect {|arg| mk arg, mode}
if paths.empty?
raise "Invalid URL"
elsif Kernel.system "which mate > /dev/null" and $? == 0
paths=paths.collect {|path| path.to_s.gsub " ", "\\ "}
exec "mate #{paths.join ' '}"
else
puts paths.join("\n")
end
when 'info', 'abv'
2009-06-13 12:59:18 +01:00
if ARGV.empty?
puts `ls #{HOMEBREW_CELLAR} | wc -l`.strip+" kegs, "+abv
elsif ARGV[0][0..6] == 'http://'
puts Pathname.new(ARGV.shift).version
2009-06-13 12:59:18 +01:00
else
2009-08-01 17:43:54 +01:00
require 'formula'
#TODO show outdated status and that
2009-08-01 17:43:54 +01:00
frm=Formula.create(extract_named_args[0])
puts "#{frm.name} #{frm.version}"
puts frm.homepage
2009-07-29 00:55:36 +01:00
if frm.installed?
2009-08-01 17:43:54 +01:00
keg=Keg.new frm
puts "#{abv keg} (installed to #{keg.path})"
end
if frm.caveats
ohai 'Caveats'
puts frm.caveats
2009-06-13 12:59:18 +01:00
end
2009-06-05 12:57:00 +01:00
end
else
puts usage
end
2009-05-23 16:37:24 +01:00
rescue StandardError, Interrupt => e
if ARGV.include? '--verbose' or ENV['HOMEBREW_DEBUG']
raise
elsif e.kind_of? Interrupt
puts # seeimgly a newline is typical
exit 130
elsif e.kind_of? StandardError and not e.kind_of? NameError
puts "\033[1;31mError\033[0;0m: #{e}"
exit 1
2009-05-21 00:04:43 +01:00
else
raise
end
end