Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.39 KB | None | 0 0
  1. #!/usr/bin/ruby
  2. # This script installs to /usr/local only. To install elsewhere (which is
  3. # unsupported) you can untar https://github.com/Homebrew/brew/tarball/master
  4. # anywhere you like.
  5. HOMEBREW_PREFIX = "/usr/local".freeze
  6. HOMEBREW_REPOSITORY = "/usr/local/Homebrew".freeze
  7. HOMEBREW_CACHE = "#{ENV["HOME"]}/Library/Caches/Homebrew".freeze
  8. BREW_REPO = "https://github.com/Homebrew/brew".freeze
  9.  
  10. # TODO: bump version when new macOS is released
  11. MACOS_LATEST_SUPPORTED = "10.15".freeze
  12. # TODO: bump version when new macOS is released
  13. MACOS_OLDEST_SUPPORTED = "10.13".freeze
  14.  
  15. # no analytics during installation
  16. ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"] = "1"
  17. ENV["HOMEBREW_NO_ANALYTICS_MESSAGE_OUTPUT"] = "1"
  18.  
  19. # get nicer global variables
  20. require "English"
  21.  
  22. module Tty
  23. module_function
  24.  
  25. def blue
  26. bold 34
  27. end
  28.  
  29. def red
  30. bold 31
  31. end
  32.  
  33. def reset
  34. escape 0
  35. end
  36.  
  37. def bold(code = 39)
  38. escape "1;#{code}"
  39. end
  40.  
  41. def underline
  42. escape "4;39"
  43. end
  44.  
  45. def escape(code)
  46. "\033[#{code}m" if STDOUT.tty?
  47. end
  48. end
  49.  
  50. class Array
  51. def shell_s
  52. cp = dup
  53. first = cp.shift
  54. cp.map { |arg| arg.gsub " ", "\\ " }.unshift(first).join(" ")
  55. end
  56. end
  57.  
  58. def ohai(*args)
  59. puts "#{Tty.blue}==>#{Tty.bold} #{args.shell_s}#{Tty.reset}"
  60. end
  61.  
  62. def warn(warning)
  63. puts "#{Tty.red}Warning#{Tty.reset}: #{warning.chomp}"
  64. end
  65.  
  66. def system(*args)
  67. abort "Failed during: #{args.shell_s}" unless Kernel.system(*args)
  68. end
  69.  
  70. def sudo(*args)
  71. args.unshift("-A") unless ENV["SUDO_ASKPASS"].nil?
  72. ohai "/usr/bin/sudo", *args
  73. system "/usr/bin/sudo", *args
  74. end
  75.  
  76. def getc
  77. system "/bin/stty raw -echo"
  78. if STDIN.respond_to?(:getbyte)
  79. STDIN.getbyte
  80. else
  81. STDIN.getc
  82. end
  83. ensure
  84. system "/bin/stty -raw echo"
  85. end
  86.  
  87. def wait_for_user
  88. puts
  89. puts "Press RETURN to continue or any other key to abort"
  90. c = getc
  91. # we test for \r and \n because some stuff does \r instead
  92. abort unless (c == 13) || (c == 10)
  93. end
  94.  
  95. class Version
  96. include Comparable
  97. attr_reader :parts
  98.  
  99. def initialize(str)
  100. @parts = str.split(".").map(&:to_i)
  101. end
  102.  
  103. def <=>(other)
  104. parts <=> self.class.new(other).parts
  105. end
  106.  
  107. def to_s
  108. parts.join(".")
  109. end
  110. end
  111.  
  112. def macos_version
  113. @macos_version ||= Version.new(`/usr/bin/sw_vers -productVersion`.chomp[/10\.\d+/])
  114. end
  115.  
  116. def should_install_command_line_tools?
  117. if macos_version > "10.13"
  118. !File.exist?("/Library/Developer/CommandLineTools/usr/bin/git")
  119. else
  120. !File.exist?("/Library/Developer/CommandLineTools/usr/bin/git") ||
  121. !File.exist?("/usr/include/iconv.h")
  122. end
  123. end
  124.  
  125. def user_only_chmod?(path)
  126. return false unless File.directory?(path)
  127.  
  128. mode = File.stat(path).mode & 0777
  129. # u = (mode >> 6) & 07
  130. # g = (mode >> 3) & 07
  131. # o = (mode >> 0) & 07
  132. mode != 0755
  133. end
  134.  
  135. def chmod?(path)
  136. File.exist?(path) && !(File.readable?(path) && File.writable?(path) && File.executable?(path))
  137. end
  138.  
  139. def chown?(path)
  140. !File.owned?(path)
  141. end
  142.  
  143. def chgrp?(path)
  144. !File.grpowned?(path)
  145. end
  146.  
  147. # USER isn't always set so provide a fall back for the installer and subprocesses.
  148. ENV["USER"] ||= `id -un`.chomp
  149.  
  150. # Invalidate sudo timestamp before exiting (if it wasn't active before).
  151. Kernel.system "/usr/bin/sudo -n -v 2>/dev/null"
  152. at_exit { Kernel.system "/usr/bin/sudo", "-k" } unless $CHILD_STATUS.success?
  153.  
  154. # The block form of Dir.chdir fails later if Dir.CWD doesn't exist which I
  155. # guess is fair enough. Also sudo prints a warning message for no good reason
  156. Dir.chdir "/usr"
  157.  
  158. ####################################################################### script
  159. if RUBY_PLATFORM.to_s.downcase.include?("linux")
  160. abort <<-EOABORT
  161. To install Linuxbrew, paste at a terminal prompt:
  162. sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"
  163. EOABORT
  164. elsif macos_version < "10.7"
  165. abort <<-EOABORT
  166. Your Mac OS X version is too old. See:
  167. #{Tty.underline}https://github.com/mistydemeo/tigerbrew#{Tty.reset}"
  168. EOABORT
  169. elsif macos_version < "10.9"
  170. abort "Your OS X version is too old"
  171. elsif Process.uid.zero?
  172. abort "Don't run this as root!"
  173. elsif !`dsmemberutil checkmembership -U "#{ENV["USER"]}" -G admin`.include?("user is a member")
  174. abort "This script requires the user #{ENV["USER"]} to be an Administrator."
  175. elsif File.directory?(HOMEBREW_PREFIX) && (!File.executable? HOMEBREW_PREFIX)
  176. abort <<-EOABORT
  177. The Homebrew prefix, #{HOMEBREW_PREFIX}, exists but is not searchable. If this is
  178. not intentional, please restore the default permissions and try running the
  179. installer again:
  180. sudo chmod 775 #{HOMEBREW_PREFIX}
  181. EOABORT
  182. # TODO: bump version when new macOS is released
  183. elsif macos_version > MACOS_LATEST_SUPPORTED || macos_version < MACOS_OLDEST_SUPPORTED
  184. who = "We"
  185. if macos_version > MACOS_LATEST_SUPPORTED
  186. what = "pre-release version"
  187. elsif macos_version < MACOS_OLDEST_SUPPORTED
  188. who << " (and Apple)"
  189. what = "old version"
  190. else
  191. return
  192. end
  193. ohai "You are using macOS #{macos_version.parts.join(".")}."
  194. ohai "#{who} do not provide support for this #{what}."
  195.  
  196. puts <<-EOS
  197. This installation may not succeed.
  198. After installation, you will encounter build failures with some formulae.
  199. Please create pull requests instead of asking for help on Homebrew's GitHub,
  200. Discourse, Twitter or IRC. You are responsible for resolving any issues you
  201. experience while you are running this #{what}.
  202.  
  203. EOS
  204. end
  205.  
  206. ohai "This script will install:"
  207. puts "#{HOMEBREW_PREFIX}/bin/brew"
  208. puts "#{HOMEBREW_PREFIX}/share/doc/homebrew"
  209. puts "#{HOMEBREW_PREFIX}/share/man/man1/brew.1"
  210. puts "#{HOMEBREW_PREFIX}/share/zsh/site-functions/_brew"
  211. puts "#{HOMEBREW_PREFIX}/etc/bash_completion.d/brew"
  212. puts HOMEBREW_REPOSITORY.to_s
  213.  
  214. # Keep relatively in sync with
  215. # https://github.com/Homebrew/brew/blob/master/Library/Homebrew/keg.rb
  216. group_chmods = %w[bin etc include lib sbin share opt var
  217. Frameworks
  218. etc/bash_completion.d lib/pkgconfig
  219. share/aclocal share/doc share/info share/locale share/man
  220. share/man/man1 share/man/man2 share/man/man3 share/man/man4
  221. share/man/man5 share/man/man6 share/man/man7 share/man/man8
  222. var/log var/homebrew var/homebrew/linked
  223. bin/brew]
  224. .map { |d| File.join(HOMEBREW_PREFIX, d) }
  225. .select { |d| chmod?(d) }
  226. # zsh refuses to read from these directories if group writable
  227. zsh_dirs = %w[share/zsh share/zsh/site-functions]
  228. .map { |d| File.join(HOMEBREW_PREFIX, d) }
  229. mkdirs = %w[bin etc include lib sbin share var opt
  230. share/zsh share/zsh/site-functions
  231. var/homebrew var/homebrew/linked
  232. Cellar Caskroom Homebrew Frameworks]
  233. .map { |d| File.join(HOMEBREW_PREFIX, d) }
  234. .reject { |d| File.directory?(d) }
  235.  
  236. user_chmods = zsh_dirs.select { |d| user_only_chmod?(d) }
  237. chmods = group_chmods + user_chmods
  238. chowns = chmods.select { |d| chown?(d) }
  239. chgrps = chmods.select { |d| chgrp?(d) }
  240.  
  241. unless group_chmods.empty?
  242. ohai "The following existing directories will be made group writable:"
  243. puts(*group_chmods)
  244. end
  245. unless user_chmods.empty?
  246. ohai "The following existing directories will be made writable by user only:"
  247. puts(*user_chmods)
  248. end
  249. unless chowns.empty?
  250. ohai "The following existing directories will have their owner set to #{Tty.underline}#{ENV["USER"]}#{Tty.reset}:"
  251. puts(*chowns)
  252. end
  253. unless chgrps.empty?
  254. ohai "The following existing directories will have their group set to #{Tty.underline}admin#{Tty.reset}:"
  255. puts(*chgrps)
  256. end
  257. unless mkdirs.empty?
  258. ohai "The following new directories will be created:"
  259. puts(*mkdirs)
  260. end
  261. if should_install_command_line_tools?
  262. ohai "The Xcode Command Line Tools will be installed."
  263. end
  264.  
  265. wait_for_user if STDIN.tty? && !ENV["CI"]
  266.  
  267. if File.directory? HOMEBREW_PREFIX
  268. sudo "/bin/chmod", "u+rwx", *chmods unless chmods.empty?
  269. sudo "/bin/chmod", "g+rwx", *group_chmods unless group_chmods.empty?
  270. sudo "/bin/chmod", "755", *user_chmods unless user_chmods.empty?
  271. sudo "/usr/sbin/chown", ENV["USER"], *chowns unless chowns.empty?
  272. sudo "/usr/bin/chgrp", "admin", *chgrps unless chgrps.empty?
  273. else
  274. sudo "/bin/mkdir", "-p", HOMEBREW_PREFIX
  275. sudo "/usr/sbin/chown", "root:wheel", HOMEBREW_PREFIX
  276. end
  277.  
  278. unless mkdirs.empty?
  279. sudo "/bin/mkdir", "-p", *mkdirs
  280. sudo "/bin/chmod", "g+rwx", *mkdirs
  281. sudo "/bin/chmod", "755", *zsh_dirs
  282. sudo "/usr/sbin/chown", ENV["USER"], *mkdirs
  283. sudo "/usr/bin/chgrp", "admin", *mkdirs
  284. end
  285.  
  286. sudo "/bin/mkdir", "-p", HOMEBREW_CACHE unless File.directory? HOMEBREW_CACHE
  287. sudo "/bin/chmod", "g+rwx", HOMEBREW_CACHE if chmod? HOMEBREW_CACHE
  288. sudo "/usr/sbin/chown", ENV["USER"], HOMEBREW_CACHE if chown? HOMEBREW_CACHE
  289. sudo "/usr/bin/chgrp", "admin", HOMEBREW_CACHE if chgrp? HOMEBREW_CACHE
  290. system "/usr/bin/touch", "#{HOMEBREW_CACHE}/.cleaned" if File.directory? HOMEBREW_CACHE
  291.  
  292. if should_install_command_line_tools? && macos_version >= "10.13"
  293. ohai "Searching online for the Command Line Tools"
  294. # This temporary file prompts the 'softwareupdate' utility to list the Command Line Tools
  295. clt_placeholder = "/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress"
  296. sudo "/usr/bin/touch", clt_placeholder
  297.  
  298. clt_label_command = "/usr/sbin/softwareupdate -l | " \
  299. "grep -B 1 -E 'Command Line Tools' | " \
  300. "awk -F'*' '/^ *\\*/ {print $2}' | " \
  301. "sed -e 's/^ *Label: //' -e 's/^ *//' | " \
  302. "sort -V | " \
  303. "tail -n1"
  304. clt_label = `#{clt_label_command}`.chomp
  305.  
  306. unless clt_label.empty?
  307. ohai "Installing #{clt_label}"
  308. sudo "/usr/sbin/softwareupdate", "-i", clt_label
  309. sudo "/bin/rm", "-f", clt_placeholder
  310. sudo "/usr/bin/xcode-select", "--switch", "/Library/Developer/CommandLineTools"
  311. end
  312. end
  313.  
  314. # Headless install may have failed, so fallback to original 'xcode-select' method
  315. if should_install_command_line_tools? && STDIN.tty?
  316. ohai "Installing the Command Line Tools (expect a GUI popup):"
  317. sudo "/usr/bin/xcode-select", "--install"
  318. puts "Press any key when the installation has completed."
  319. getc
  320. sudo "/usr/bin/xcode-select", "--switch", "/Library/Developer/CommandLineTools"
  321. end
  322.  
  323. abort <<-EOABORT if `/usr/bin/xcrun clang 2>&1` =~ /license/ && !$CHILD_STATUS.success?
  324. You have not agreed to the Xcode license.
  325. Before running the installer again please agree to the license by opening
  326. Xcode.app or running:
  327. sudo xcodebuild -license
  328. EOABORT
  329.  
  330. ohai "Downloading and installing Homebrew..."
  331. Dir.chdir HOMEBREW_REPOSITORY do
  332. # we do it in four steps to avoid merge errors when reinstalling
  333. system "git", "init", "-q"
  334.  
  335. # "git remote add" will fail if the remote is defined in the global config
  336. system "git", "config", "remote.origin.url", BREW_REPO
  337. system "git", "config", "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*"
  338.  
  339. # ensure we don't munge line endings on checkout
  340. system "git", "config", "core.autocrlf", "false"
  341.  
  342. system "git", "fetch", "origin", "master:refs/remotes/origin/master",
  343. "--tags", "--force"
  344.  
  345. system "git", "reset", "--hard", "origin/master"
  346.  
  347. system "ln", "-sf", "#{HOMEBREW_REPOSITORY}/bin/brew", "#{HOMEBREW_PREFIX}/bin/brew"
  348.  
  349. system "#{HOMEBREW_PREFIX}/bin/brew", "update", "--force"
  350. end
  351.  
  352. warn "#{HOMEBREW_PREFIX}/bin is not in your PATH." unless ENV["PATH"].split(":").include? "#{HOMEBREW_PREFIX}/bin"
  353.  
  354. ohai "Installation successful!"
  355. puts
  356.  
  357. # Use the shell's audible bell.
  358. print "\a"
  359.  
  360. # Use an extra newline and bold to avoid this being missed.
  361. ohai "Homebrew has enabled anonymous aggregate formulae and cask analytics."
  362. puts <<-EOS
  363. #{Tty.bold}Read the analytics documentation (and how to opt-out) here:
  364. #{Tty.underline}https://docs.brew.sh/Analytics#{Tty.reset}
  365. No analytics data has been sent yet (or will be during this `install` run).
  366.  
  367. EOS
  368.  
  369. ohai "Homebrew is run entirely by unpaid volunteers. Please consider donating:"
  370. puts <<-EOS
  371. #{Tty.underline}https://github.com/Homebrew/brew#donations#{Tty.reset}
  372. EOS
  373.  
  374. Dir.chdir HOMEBREW_REPOSITORY do
  375. system "git", "config", "--replace-all", "homebrew.analyticsmessage", "true"
  376. system "git", "config", "--replace-all", "homebrew.caskanalyticsmessage", "true"
  377. end
  378.  
  379. ohai "Next steps:"
  380. puts "- Run `brew help` to get started"
  381. puts "- Further documentation: "
  382. puts " #{Tty.underline}https://docs.brew.sh#{Tty.reset}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement