2009-08-31 16:01:36 +01:00
|
|
|
# Copyright 2009 Max Howell and other contributors.
|
2009-07-31 02:51:17 +01:00
|
|
|
#
|
2009-08-31 16:01:36 +01:00
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions
|
|
|
|
# are met:
|
2009-07-31 02:51:17 +01:00
|
|
|
#
|
2009-08-31 16:01:36 +01:00
|
|
|
# 1. Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
2009-07-31 02:51:17 +01:00
|
|
|
#
|
2009-08-31 16:01:36 +01:00
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2009-07-31 02:51:17 +01:00
|
|
|
|
2009-10-15 14:42:19 +01:00
|
|
|
class Tty
|
|
|
|
class <<self
|
|
|
|
def blue; bold 34; end
|
|
|
|
def white; bold 39; end
|
|
|
|
def red; underline 31; end
|
|
|
|
def yellow; underline 33 ; end
|
|
|
|
def reset; escape 0; end
|
2009-11-11 18:36:50 +00:00
|
|
|
def em; underline 39; end
|
2009-10-15 14:42:19 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
def color n
|
|
|
|
escape "0;#{n}"
|
|
|
|
end
|
|
|
|
def bold n
|
|
|
|
escape "1;#{n}"
|
|
|
|
end
|
|
|
|
def underline n
|
|
|
|
escape "4;#{n}"
|
|
|
|
end
|
|
|
|
def escape n
|
|
|
|
"\033[#{n}m" if $stdout.tty?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-08 14:05:09 +01:00
|
|
|
# args are additional inputs to puts until a nil arg is encountered
|
2009-09-16 19:29:43 +01:00
|
|
|
def ohai title, *sput
|
2009-10-23 16:05:32 +01:00
|
|
|
title = title.to_s[0, `/usr/bin/tput cols`.strip.to_i-4] unless ARGV.verbose?
|
2009-10-15 14:42:19 +01:00
|
|
|
puts "#{Tty.blue}==>#{Tty.white} #{title}#{Tty.reset}"
|
2009-09-16 19:29:43 +01:00
|
|
|
puts *sput unless sput.empty?
|
2009-07-31 02:51:17 +01:00
|
|
|
end
|
2009-07-31 03:56:46 +01:00
|
|
|
|
|
|
|
def opoo warning
|
2009-10-15 14:42:19 +01:00
|
|
|
puts "#{Tty.red}Warning#{Tty.reset}: #{warning}"
|
2009-08-10 16:48:30 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def onoe error
|
2009-10-01 15:41:15 +01:00
|
|
|
lines = error.to_s.split'\n'
|
2009-10-15 14:42:19 +01:00
|
|
|
puts "#{Tty.red}Error#{Tty.reset}: #{lines.shift}"
|
2009-10-01 15:41:15 +01:00
|
|
|
puts *lines unless lines.empty?
|
2009-08-10 16:48:30 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def pretty_duration s
|
2009-09-03 20:58:33 +01:00
|
|
|
return "2 seconds" if s < 3 # avoids the plural problem ;)
|
|
|
|
return "#{s.to_i} seconds" if s < 120
|
|
|
|
return "%.1f minutes" % (s/60)
|
2009-08-10 16:48:30 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def interactive_shell
|
2009-11-09 17:44:29 +00:00
|
|
|
fork do
|
2009-08-31 18:23:41 +01:00
|
|
|
# TODO make the PS1 var change pls
|
|
|
|
#brown="\[\033[0;33m\]"
|
|
|
|
#reset="\[\033[0m\]"
|
|
|
|
#ENV['PS1']="Homebrew-#{HOMEBREW_VERSION} #{brown}\W#{reset}\$ "
|
|
|
|
exec ENV['SHELL']
|
|
|
|
end
|
2009-11-09 17:44:29 +00:00
|
|
|
Process.wait
|
|
|
|
unless $?.success?
|
|
|
|
puts "Aborting due to non-zero exit status"
|
|
|
|
exit $?
|
|
|
|
end
|
2009-07-31 03:56:46 +01:00
|
|
|
end
|
2009-08-11 12:20:55 -07:00
|
|
|
|
|
|
|
# Kernel.system but with exceptions
|
|
|
|
def safe_system cmd, *args
|
|
|
|
puts "#{cmd} #{args*' '}" if ARGV.verbose?
|
2009-11-09 17:44:29 +00:00
|
|
|
fork do
|
2009-12-04 17:48:07 +00:00
|
|
|
args.collect!{|arg| arg.to_s}
|
|
|
|
exec(cmd, *args) rescue nil
|
2009-11-11 19:42:35 +00:00
|
|
|
exit! 1 # never gets here unless exec failed
|
2009-11-09 17:44:29 +00:00
|
|
|
end
|
|
|
|
Process.wait
|
|
|
|
raise ExecutionError.new(cmd, args, $?) unless $?.success?
|
2009-08-11 12:20:55 -07:00
|
|
|
end
|
|
|
|
|
2009-09-04 15:28:18 +01:00
|
|
|
def curl *args
|
|
|
|
safe_system 'curl', '-f#LA', HOMEBREW_USER_AGENT, *args unless args.empty?
|
2009-08-11 12:20:55 -07:00
|
|
|
end
|
2009-09-04 15:22:25 -07:00
|
|
|
|
|
|
|
def puts_columns items, cols = 4
|
2009-12-18 16:28:30 -08:00
|
|
|
return if items.empty?
|
|
|
|
|
2009-09-22 20:43:06 +02:00
|
|
|
if $stdout.tty?
|
2009-09-23 16:44:10 +01:00
|
|
|
items = items.join("\n") if items.is_a?(Array)
|
|
|
|
items.concat("\n") unless items.empty?
|
2009-11-14 13:43:07 +13:00
|
|
|
|
|
|
|
# determine the best width to display for different console sizes
|
|
|
|
console_width = `/bin/stty size`.chomp.split(" ").last
|
|
|
|
longest = items.sort_by { |item| item.length }.last
|
|
|
|
optimal_col_width = (console_width.to_f / (longest.length + 2).to_f).floor
|
|
|
|
cols = optimal_col_width > 1 ? optimal_col_width : 1
|
|
|
|
|
|
|
|
IO.popen("/usr/bin/pr -#{cols} -t -w#{console_width}", "w"){|io| io.write(items) }
|
2009-09-22 20:43:06 +02:00
|
|
|
else
|
2009-09-23 16:44:10 +01:00
|
|
|
puts *items
|
2009-09-22 20:43:06 +02:00
|
|
|
end
|
2009-09-04 15:22:25 -07:00
|
|
|
end
|
2009-09-05 14:03:41 -04:00
|
|
|
|
|
|
|
def exec_editor *args
|
|
|
|
editor=ENV['EDITOR']
|
|
|
|
if editor.nil?
|
2009-09-23 16:44:10 +01:00
|
|
|
if system "/usr/bin/which -s mate"
|
2009-09-05 14:03:41 -04:00
|
|
|
editor='mate'
|
|
|
|
else
|
2009-09-23 16:44:10 +01:00
|
|
|
editor='/usr/bin/vim'
|
2009-09-05 14:03:41 -04:00
|
|
|
end
|
|
|
|
end
|
2009-09-08 22:10:14 +01:00
|
|
|
# we split the editor because especially on mac "mate -w" is common
|
|
|
|
# but we still want to use the comma-delimited version of exec because then
|
|
|
|
# we don't have to escape args, and escaping 100% is tricky
|
|
|
|
exec *(editor.split+args)
|
2009-09-05 14:03:41 -04:00
|
|
|
end
|
2009-09-08 15:31:28 -07:00
|
|
|
|
2010-01-13 11:14:58 +00:00
|
|
|
# returns array of architectures suitable for -arch gcc flag
|
|
|
|
def archs_for_command cmd
|
|
|
|
cmd = `/usr/bin/which #{cmd}` unless Pathname.new(cmd).absolute?
|
|
|
|
cmd.gsub! ' ', '\\ '
|
2009-09-08 15:31:28 -07:00
|
|
|
|
2010-01-13 11:14:58 +00:00
|
|
|
IO.popen("/usr/bin/file #{cmd}").readlines.inject(%w[]) do |archs, line|
|
2009-09-08 15:31:28 -07:00
|
|
|
case line
|
|
|
|
when /Mach-O executable ppc/
|
|
|
|
archs << :ppc7400
|
|
|
|
when /Mach-O 64-bit executable ppc64/
|
|
|
|
archs << :ppc64
|
|
|
|
when /Mach-O executable i386/
|
|
|
|
archs << :i386
|
|
|
|
when /Mach-O 64-bit executable x86_64/
|
|
|
|
archs << :x86_64
|
2010-01-13 11:14:58 +00:00
|
|
|
else
|
|
|
|
archs
|
2009-09-08 15:31:28 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-10-15 12:36:09 +01:00
|
|
|
|
|
|
|
# replaces before with after for the file path
|
|
|
|
def inreplace path, before, after
|
2009-10-23 19:01:05 +01:00
|
|
|
f = File.open(path, 'r')
|
|
|
|
o = f.read.gsub(before, after)
|
|
|
|
f.reopen(path, 'w').write(o)
|
|
|
|
f.close
|
2009-10-15 12:36:09 +01:00
|
|
|
end
|
2009-11-06 17:09:14 +00:00
|
|
|
|
|
|
|
def ignore_interrupts
|
|
|
|
std_trap = trap("INT") {}
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
trap("INT", std_trap)
|
|
|
|
end
|
2009-11-09 18:24:36 +00:00
|
|
|
|
|
|
|
def nostdout
|
|
|
|
if ARGV.verbose?
|
|
|
|
yield
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
require 'stringio'
|
|
|
|
real_stdout = $stdout
|
|
|
|
$stdout = StringIO.new
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
$stdout = real_stdout
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|