Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 4.32 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/ruby
  2. # This script installs to /usr/local only. To install elsewhere you can just
  3. # untar https://github.com/mxcl/homebrew/tarball/master anywhere you like.
  4.  
  5. module Tty extend self
  6.   def blue; bold 34; end
  7.   def white; bold 39; end
  8.   def red; underline 31; end
  9.   def reset; escape 0; end
  10.   def bold n; escape "1;#{n}" end
  11.   def underline n; escape "4;#{n}" end
  12.   def escape n; "\033[#{n}m" if STDOUT.tty? end
  13. end
  14.  
  15. class Array
  16.   def shell_s
  17.     cp = dup
  18.     first = cp.shift
  19.     cp.map{ |arg| arg.gsub " ", "\\ " }.unshift(first) * " "
  20.   end
  21. end
  22.  
  23. def ohai *args
  24.   puts "#{Tty.blue}==>#{Tty.white} #{args.shell_s}#{Tty.reset}"
  25. end
  26.  
  27. def warn warning
  28.   puts "#{Tty.red}Warning#{Tty.reset}: #{warning.chomp}"
  29. end
  30.  
  31. def system *args
  32.   abort "Failed during: #{args.shell_s}" unless Kernel.system *args
  33. end
  34.  
  35. def sudo *args
  36.   args = if args.length > 1
  37.     args.unshift "/usr/bin/sudo"
  38.   else
  39.     "/usr/bin/sudo #{args.first}"
  40.   end
  41.   ohai *args
  42.   system *args
  43. end
  44.  
  45. def getc  # NOTE only tested on OS X
  46.   system "/bin/stty raw -echo"
  47.   if RUBY_VERSION >= '1.8.7'
  48.     STDIN.getbyte
  49.   else
  50.     STDIN.getc
  51.   end
  52. ensure
  53.   system "/bin/stty -raw echo"
  54. end
  55.  
  56. def badlibs
  57.   @badlibs ||= begin
  58.     Dir['/usr/local/lib/*.dylib'].select do |dylib|
  59.       ENV['dylib'] = dylib
  60.       File.file? dylib and not File.symlink? dylib and `/usr/bin/file "$dylib"` =~ /shared library/
  61.     end
  62.   end
  63. end
  64.  
  65. ####################################################################### script
  66. abort "/usr/local/.git already exists!" unless Dir["/usr/local/.git/*"].empty?
  67. abort "Don't run this as root!" if Process.uid == 0
  68. abort <<-EOABORT unless `groups`.split.include? "staff"
  69. This script requires the user #{ENV['USER']} to be in the staff group. If this
  70. sucks for you then you can install Homebrew in your home directory or however
  71. you please; please refer to the website. If you still want to use this script
  72. the following command should work:
  73.  
  74.     dscl /Local/Default -append /Groups/staff GroupMembership $USER
  75. EOABORT
  76.  
  77. ohai "This script will install:"
  78. puts "/usr/local/bin/brew"
  79. puts "/usr/local/Library/Formula/..."
  80. puts "/usr/local/Library/Homebrew/..."
  81.  
  82. chmods = %w( share/man lib/pkgconfig var/log share/locale
  83.              share/man/man1 share/man/man2 share/man/man3 share/man/man4
  84.              share/man/man5 share/man/man6 share/man/man7 share/man/man8
  85.              share/info share/doc share/aclocal ).map{ |d| "/usr/local/#{d}" }
  86. root_dirs = []
  87. %w(bin Cellar etc include lib Library sbin share var .git).each do |d|
  88.   d = "/usr/local/#{d}"
  89.   if File.directory? d then chmods else root_dirs end << d
  90. end
  91. chmods = chmods.select{ |d| File.directory? d and not File.writable? d }
  92. chgrps = chmods.reject{ |d| File.stat(d).grpowned? }
  93.  
  94. unless chmods.empty?
  95.   ohai "The following directories will be made group writable:"
  96.   puts *chmods
  97. end
  98. unless chgrps.empty?
  99.   ohai "The following directories will have their group set to #{Tty.underline 39}staff#{Tty.reset}:"
  100.   puts *chgrps
  101. end
  102.  
  103.  
  104. if STDIN.tty?
  105.   puts
  106.   puts "Press enter to continue"
  107.   abort unless getc == 13
  108. end
  109.  
  110. sudo "/bin/mkdir /usr/local" unless File.directory? "/usr/local"
  111. sudo "/bin/chmod o+w /usr/local"
  112. begin
  113.   sudo "/bin/chmod", "g+w", *chmods unless chmods.empty?
  114.   sudo "/usr/bin/chgrp", "staff", *chgrps unless chgrps.empty?
  115.   system "/bin/mkdir", *root_dirs unless root_dirs.empty?
  116.  
  117.   Dir.chdir "/usr/local" do
  118.     ohai "Downloading and Installing Homebrew..."
  119.     # -m to stop tar erroring out if it can't modify the mtime for root owned directories
  120.     # pipefail to cause the exit status from curl to propogate if it fails
  121.     system "/bin/bash -o pipefail -c '/usr/bin/curl -sSfL https://github.com/mxcl/homebrew/tarball/master | /usr/bin/tar xz -m --strip 1'"
  122.   end
  123. ensure
  124.   # we reset the permissions of /usr/local because we want to minimise the
  125.   # amount of fiddling we do to the system. Some tools require /usr/local to
  126.   # be by non-writable for non-root users.
  127.   sudo "/bin/chmod o-w /usr/local"
  128. end
  129.  
  130. warn "/usr/local/bin is not in your PATH." unless ENV['PATH'].split(':').include? '/usr/local/bin'
  131. warn "Now install Xcode: http://developer.apple.com/technologies/xcode.html" unless Kernel.system "/usr/bin/which -s gcc"
  132.  
  133. unless badlibs.empty?
  134.   warn "The following *evil* dylibs exist in /usr/local/lib"
  135.   puts "They may break builds or worse. You should consider deleting them:"
  136.   puts *badlibs
  137. end
  138.  
  139. ohai "Installation successful!"
  140. puts "Now type: brew help"