Skip to content

Instantly share code, notes, and snippets.

@trevmex
Forked from mxcl/install_homebrew.markdown
Created May 24, 2010 13:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save trevmex/411826 to your computer and use it in GitHub Desktop.
Install in any directory via command line arguments. The default behavior is still intact.
#!/usr/bin/ruby
#
# Download and execute this script in one-line with no temporary files:
#
# ruby -e "$(curl http://gist.github.com/raw/411826/install_homebrew.rb)"
#
#
# 24th May 2010 (trevmex):
# Adapted mxcl's fork: http://gist.github.com/323731
# Changed mxcl's code to include a prefix command line variable to allow
# installation to places other than /usr/local.
# Added a simple help file.
# Added an option to change the default group (our systems are bound by AD
# and the default group for users is not staff).
# Added an option to not use sudo.
#
# 30th March 2010:
# Added a check to make sure user is in the staff group. This was a problem
# for me, and I think it was due to me migrating my account over several
# versions of OS X. I cannot verify that for sure, and it was tested on
# 10.6.2 using the Directory Service command line utility and my laptop.
#
# My assumptions are:
# - you are running OS X 10.6.x
# - your machine is not managed as part of a group using networked
# Directory Services
# - you have not recently killed any baby seals or kittens
#
# 14th March 2010:
# Adapted CodeButler's fork: http://gist.github.com/331512
#
def help
puts <<-EOHELP
Usage: install_homebrew.rb [--prefix=...] [--proxy=...] [--help]
\t--prefix=[prefix]\tChanges the default prefix from /usr/local to the string
\t\t\t\tyou specify. Warning: if you do this, you will most likely have to
\t\t\t\tupdate your PATH environment variable to include the new path's bin
\t\t\t\tdirectory.
\t--no-sudo\tDo not require sudo to make changes.
\t--help\t\t\tDisplay this message.
EOHELP
Process.exit
end
# Get the prefix if it was set
install_root = "/usr/local" # the default
install_group = "staff" # the default
no_sudo = false # the default
ARGV.each do |arg|
if (/^--prefix=/).match(arg)
install_root = (/[^=]*$/).match(arg)[0]
elsif (/^--group=/).match(arg)
install_group = (/[^=]*$/).match(arg)[0]
elsif arg == "--no-sudo"
no_sudo = true
elsif arg == "--help"
help
end
end
module Tty extend self
def blue; bold 34; end
def white; bold 39; end
def red; underline 31; end
def reset; escape 0; 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
class Array
def shell_s
cp = dup
first = cp.shift
cp.map{ |arg| arg.gsub " ", "\\ " }.unshift(first) * " "
end
end
def ohai *args
puts "#{Tty.blue}==>#{Tty.white} #{args.shell_s}#{Tty.reset}"
end
def warn warning
puts "#{Tty.red}Warning#{Tty.reset}: #{warning.chomp}"
end
alias :system_orig :system
def system *args
abort "Failed during: #{args.shell_s}" unless system_orig *args
end
def sudo *args
args = if args.length > 1
args.unshift "sudo"
else
"sudo #{args}"
end
ohai *args
system *args
end
def getc # NOTE only tested on OS X
system "stty raw -echo"
STDIN.getc
ensure
system "stty -raw echo"
end
####################################################################### script
abort "#{install_root}/.git already exists!" if File.directory? "#{install_root}/.git"
abort "Don't run this as root!" if Process.uid == 0
# if the user set the install group, they should know what they are doing.
if install_group == 'staff'
abort <<-EOABORT unless `groups`.split.include? "#{install_group}"
This script requires the user #{ENV['USER']} to be in the #{install_group} group. If this
sucks for you then you can install Homebrew in your home directory or however
you please; please refer to the website. If you still want to use this script
the following command should work:
dscl /Local/Default -append /Groups/#{install_group} GroupMembership $USER
EOABORT
end
ohai "This script will install:"
puts "#{install_root}/bin/brew"
puts "#{install_root}/Library/Formula/..."
puts "#{install_root}/Library/Homebrew/..."
chmods = %w(bin etc include lib sbin share var . share/locale share/man share/info share/doc share/aclocal).
map{ |d| "#{install_root}/#{d}" }.
select{ |d| File.directory? d and not File.writable? d }
chgrps = chmods.reject{ |d| File.stat(d).grpowned? }
unless chmods.empty?
ohai "The following directories will be made group writable:"
puts *chmods
end
unless chgrps.empty?
ohai "The following directories will have their group set to #{Tty.underline 39}#{install_group}#{Tty.reset}:"
puts *chgrps
end
puts
puts "Press enter to continue"
abort unless getc == 13
if no_sudo
alias :run_command :system
else
alias :run_command :sudo
end
if File.directory? "#{install_root}"
run_command "/bin/chmod", "g+w", *chmods unless chmods.empty?
# all admin users are in staff, and if the user sets the install_group
# they should already know what they are doing.
run_command "/usr/bin/chgrp", "#{install_group}", *chgrps unless chgrps.empty?
else
run_command "/bin/mkdir #{install_root}"
run_command "/bin/chmod g+w #{install_root}"
# the group is set to wheel by default for some reason
run_command "/usr/bin/chgrp #{install_group} #{install_root}"
end
Dir.chdir "#{install_root}" do
ohai "Downloading and Installing Homebrew..."
# -m to stop tar erroring out if it can't modify the mtime for root owned directories
system "/usr/bin/curl -sfL http://github.com/mxcl/homebrew/tarball/master | /usr/bin/tar xz -m --strip 1"
end
ohai "Installation successful!"
if ENV['PATH'].split(':').include? '#{install_root}/bin'
puts "Yay! Now learn to brew:"
puts
puts " brew help"
puts
else
warn "#{install_root}/bin is not in your PATH"
end
@mxcl
Copy link

mxcl commented May 28, 2010

Line 178 should be "#{install_root}/bin".

Otherwise I'm not entirely keen to merge this as most of the existing logic is very specific to /usr/local. For any other directory you may as well just untar the tarball as it probably isn't a prefix that has already potentially got unix stuff in. Of course, it's still useful if you are installing to /usr or /opt/local, but then you're possibly crazy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment