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

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 4.13 KB  |  hits: 12  |  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 getc  # NOTE only tested on OS X
  36.   system "/bin/stty raw -echo"
  37.   if RUBY_VERSION >= '1.8.7'
  38.     STDIN.getbyte
  39.   else
  40.     STDIN.getc
  41.   end
  42. ensure
  43.   system "/bin/stty -raw echo"
  44. end
  45.  
  46. def badlibs
  47.   @badlibs ||= begin
  48.     Dir['/usr/local/lib/*.dylib'].select do |dylib|
  49.       ENV['dylib'] = dylib
  50.       File.file? dylib and not File.symlink? dylib and `/usr/bin/file "$dylib"` =~ /shared library/
  51.     end
  52.   end
  53. end
  54.  
  55. ####################################################################### script
  56. abort "/usr/local/.git already exists!" unless Dir["/usr/local/.git/*"].empty?
  57. abort "Don't run this as root!" if Process.uid == 0
  58. abort <<-EOABORT unless `groups`.split.include? "staff"
  59. This script requires the user #{ENV['USER']} to be in the staff group. If this
  60. sucks for you then you can install Homebrew in your home directory or however
  61. you please; please refer to the website. If you still want to use this script
  62. the following command should work:
  63.  
  64.     dscl /Local/Default -append /Groups/staff GroupMembership $USER
  65. EOABORT
  66.  
  67. ohai "This script will install:"
  68. puts "/usr/local/bin/brew"
  69. puts "/usr/local/Library/Formula/..."
  70. puts "/usr/local/Library/Homebrew/..."
  71.  
  72. chmods = %w( share/man lib/pkgconfig var/log share/locale
  73.              share/man/man1 share/man/man2 share/man/man3 share/man/man4
  74.              share/man/man5 share/man/man6 share/man/man7 share/man/man8
  75.              share/info share/doc share/aclocal ).map{ |d| "/usr/local/#{d}" }
  76. root_dirs = []
  77. %w(bin Cellar etc include lib Library sbin share var .git).each do |d|
  78.   d = "/usr/local/#{d}"
  79.   if File.directory? d then chmods else root_dirs end << d
  80. end
  81. chmods = chmods.select{ |d| File.directory? d and not File.writable? d }
  82. chgrps = chmods.reject{ |d| File.stat(d).grpowned? }
  83.  
  84. unless chmods.empty?
  85.   ohai "The following directories will be made group writable:"
  86.   puts *chmods
  87. end
  88. unless chgrps.empty?
  89.   ohai "The following directories will have their group set to #{Tty.underline 39}staff#{Tty.reset}:"
  90.   puts *chgrps
  91. end
  92.  
  93.  
  94. if STDIN.tty?
  95.   puts
  96.   puts "Press enter to continue"
  97.   abort unless getc == 13
  98. end
  99.  
  100. sudo "/bin/mkdir /usr/local" unless File.directory? "/usr/local"
  101. sudo "/bin/chmod o+w /usr/local"
  102. sudo "/bin/chmod", "g+w", *chmods unless chmods.empty?
  103. sudo "/usr/bin/chgrp", "staff", *chgrps unless chgrps.empty?
  104. system "/bin/mkdir", *root_dirs unless root_dirs.empty?
  105.  
  106.  
  107. Dir.chdir "/usr/local" do
  108.   ohai "Downloading and Installing Homebrew..."
  109.   # -m to stop tar erroring out if it can't modify the mtime for root owned directories
  110.   # pipefail to cause the exit status from curl to propogate if it fails
  111.   system "/bin/bash -o pipefail -c '/usr/bin/curl -sSfL https://github.com/mxcl/homebrew/tarball/master | /usr/bin/tar xz -m --strip 1'"
  112. end
  113.  
  114. # we reset the permissions of /usr/local because we want to minimise the
  115. # amount of fiddling we do to the system. Some tools require /usr/local to be
  116. # by non-writable for non-root users.
  117. sudo "/bin/chmod o-w /usr/local"
  118.  
  119. warn "/usr/local/bin is not in your PATH." unless ENV['PATH'].split(':').include? '/usr/local/bin'
  120. warn "Now install Xcode: http://developer.apple.com/technologies/xcode.html" unless Kernel.system "/usr/bin/which -s gcc"
  121.  
  122. unless badlibs.empty?
  123.   warn "The following *evil* dylibs exist in /usr/local/lib"
  124.   puts "They may break builds or worse. You should consider deleting them:"
  125.   puts *badlibs
  126. end
  127.  
  128. ohai "Installation successful!"
  129. puts "Now type: brew help"