拷贝以下代码至文本,名字改为brew_install.rb. 然后在终端cd 到文件当前目录,然后 ruby brew_install.rb
#!/usr/bin/ruby
# This script installs to /usr/local only. To install elsewhere (which is
# unsupported) you can untar https://github.com/Homebrew/brew/tarball/master
# anywhere you like.
HOMEBREW_PREFIX ="/usr/local".freeze
HOMEBREW_REPOSITORY ="/usr/local/Homebrew".freeze
HOMEBREW_CORE_TAP = "/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core".freeze
HOMEBREW_CACHE = "#{ENV["HOME"]}/Library/Caches/Homebrew".freeze
BREW_REPO = "https://mirrors.ustc.edu.cn/brew.git".freeze
#TODO: bump version when new macOS is released
MACOS_LATEST_SUPPORTED ="10.14".freeze
#TODO: bump version when new macOS is released
MACOS_OLDEST_SUPPORTED ="10.12".freeze
# no analytics during installation
ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"] = "1"
ENV["HOMEBREW_NO_ANALYTICS_MESSAGE_OUTPUT"] = "1"
# get nicer global variables
require "English"
module Tty
module_function
defblue
bold34
end
defred
bold31
end
defreset
escape0
end
defbold(code =39)
escape"1;#{code}"
end
defunderline
escape"4;39"
end
defescape(code)
"\033[#{code}m"ifSTDOUT.tty?
end
end
classArray
defshell_s
cp = dup
first = cp.shift
cp.map { |arg| arg.gsub" ","\\ "}.unshift(first).join(" ")
end
end
defohai(*args)
puts"#{Tty.blue}==>#{Tty.bold} #{args.shell_s}#{Tty.reset}"
end
defwarn(warning)
puts"#{Tty.red}Warning#{Tty.reset}: #{warning.chomp}"
end
defsystem(*args)
abort"Failed during: #{args.shell_s}" unless Kernel.system(*args)
end
defsudo(*args)
args.unshift("-A")unlessENV["SUDO_ASKPASS"].nil?
ohai"/usr/bin/sudo", *args
system"/usr/bin/sudo", *args
end
defgetc
system"/bin/stty raw -echo"
ifSTDIN.respond_to?(:getbyte)
STDIN.getbyte
else
STDIN.getc
end
ensure
system"/bin/stty -raw echo"
end
defwait_for_user
puts
puts"Press RETURN to continue or any other key to abort"
c = getc
# we test for \r and \n because some stuff does \r instead
abortunless(c ==13) || (c ==10)
end
classVersion
includeComparable
attr_reader :parts
definitialize(str)
@parts = str.split(".").map(&:to_i)
end
def<=>(other)
parts <=>self.class.new(other).parts
end
defto_s
parts.join(".")
end
end
defmacos_version
@macos_version ||= Version.new(`/usr/bin/sw_vers -productVersion`.chomp[/10\.\d+/])
end
defshould_install_command_line_tools?
ifmacos_version >"10.13"
!File.exist?("/Library/Developer/CommandLineTools/usr/bin/git")
else
!File.exist?("/Library/Developer/CommandLineTools/usr/bin/git") ||
!File.exist?("/usr/include/iconv.h")
end
end
defuser_only_chmod?(path)
returnfalseunlessFile.directory?(path)
mode = File.stat(path).mode &0777
# u = (mode >> 6) & 07
# g = (mode >> 3) & 07
# o = (mode >> 0) & 07
mode !=0755
end
defchmod?(path)
File.exist?(path) && !(File.readable?(path) && File.writable?(path) && File.executable?(path))
end
defchown?(path)
!File.owned?(path)
end
defchgrp?(path)
!File.grpowned?(path)
end
# Invalidate sudo timestamp before exiting (if it wasn't active before).
Kernel.system "/usr/bin/sudo -n -v 2>/dev/null"
at_exit { Kernel.system"/usr/bin/sudo","-k"}unless$CHILD_STATUS.success?
# The block form of Dir.chdir fails later if Dir.CWD doesn't exist which I
# guess is fair enough. Also sudo prints a warning message for no good reason
Dir.chdir"/usr"
####################################################################### script
ifRUBY_PLATFORM.to_s.downcase.include?("linux")
abort <<-EOABORT
To install Linuxbrew, paste at a terminal prompt:
sh -c"$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"
EOABORT
elsifmacos_version <"10.7"
abort <<-EOABORT
Your Mac OS X version is too old. See:
#{Tty.underline}https://github.com/mistydemeo/tigerbrew#{Tty.reset}"
EOABORT
elsifmacos_version <"10.9"
abort"Your OS X version is too old"
elsifProcess.uid.zero?
abort"Don't run this as root!"
elsif !`dsmemberutil checkmembership -U "#{ENV["USER"]}" -G admin`.include?("user is a member")
abort"This script requires the user #{ENV["USER"]} to be an Administrator."
elsifFile.directory?(HOMEBREW_PREFIX) && (!File.executable? HOMEBREW_PREFIX)
abort <<-EOABORT
The Homebrew prefix, #{HOMEBREW_PREFIX}, exists but isnotsearchable. If this is
notintentional, please restore the default permissionsandtry running the
installer again:
sudo chmod775#{HOMEBREW_PREFIX}
EOABORT
#TODO: bump version when new macOS is released
elsifmacos_version > MACOS_LATEST_SUPPORTED || macos_version < MACOS_OLDEST_SUPPORTED
who ="We"
ifmacos_version > MACOS_LATEST_SUPPORTED
what ="pre-release version"
elsifmacos_version < MACOS_OLDEST_SUPPORTED
who <<" (and Apple)"
what ="old version"
else
return
end
ohai"You are using macOS #{macos_version.parts.join(".")}."
ohai"#{who} do not provide support for this #{what}."
puts <<-EOS
This installation maynotsucceed.
After installation, you will encounter build failuresandother breakages.
Please create pull requests instead of askingforhelp on Homebrew's
GitHub, Discourse, TwitterorIRC. As you are running this #{what},
you are responsibleforresolving any issues you experience.
EOS
end
ohai "This script will install:"
puts "#{HOMEBREW_PREFIX}/bin/brew"
puts "#{HOMEBREW_PREFIX}/share/doc/homebrew"
puts "#{HOMEBREW_PREFIX}/share/man/man1/brew.1"
puts "#{HOMEBREW_PREFIX}/share/zsh/site-functions/_brew"
puts "#{HOMEBREW_PREFIX}/etc/bash_completion.d/brew"
puts HOMEBREW_REPOSITORY.to_s
# Keep relatively in sync with
# https://github.com/Homebrew/brew/blob/master/Library/Homebrew/keg.rb
group_chmods = %w[bin etcincludelib sbin share opt var
Frameworks
etc/bash_completion.d lib/pkgconfig
share/aclocal share/doc share/info share/locale share/man
share/man/man1 share/man/man2 share/man/man3 share/man/man4
share/man/man5 share/man/man6 share/man/man7 share/man/man8
var/log var/homebrew var/homebrew/linked
bin/brew]
.map { |d| File.join(HOMEBREW_PREFIX, d) }
.select { |d| chmod?(d) }
# zsh refuses to read from these directories if group writable
zsh_dirs = %w[share/zsh share/zsh/site-functions]
.map { |d| File.join(HOMEBREW_PREFIX, d) }
mkdirs = %w[bin etcincludelib sbin share var opt
share/zsh share/zsh/site-functions
var/homebrew var/homebrew/linked
Cellar Caskroom Homebrew Frameworks]
.map { |d| File.join(HOMEBREW_PREFIX, d) }
.reject { |d| File.directory?(d) }
user_chmods = zsh_dirs.select { |d| user_only_chmod?(d) }
chmods = group_chmods + user_chmods
chowns = chmods.select { |d| chown?(d) }
chgrps = chmods.select { |d| chgrp?(d) }
unlessgroup_chmods.empty?
ohai"The following existing directories will be made group writable:"
puts(*group_chmods)
end
unlessuser_chmods.empty?
ohai"The following existing directories will be made writable by user only:"
puts(*user_chmods)
end
unlesschowns.empty?
ohai"The following existing directories will have their owner set to #{Tty.underline}#{ENV["USER"]}#{Tty.reset}:"
puts(*chowns)
end
unlesschgrps.empty?
ohai"The following existing directories will have their group set to #{Tty.underline}admin#{Tty.reset}:"
puts(*chgrps)
end
unlessmkdirs.empty?
ohai"The following new directories will be created:"
puts(*mkdirs)
end
ifshould_install_command_line_tools?
ohai"The Xcode Command Line Tools will be installed."
end
wait_for_userifSTDIN.tty? && !ENV["CI"]
ifFile.directory? HOMEBREW_PREFIX
sudo"/bin/chmod","u+rwx", *chmodsunlesschmods.empty?
sudo"/bin/chmod","g+rwx", *group_chmodsunlessgroup_chmods.empty?
sudo"/bin/chmod","755", *user_chmodsunlessuser_chmods.empty?
sudo"/usr/sbin/chown", ENV["USER"], *chownsunlesschowns.empty?
sudo"/usr/bin/chgrp","admin", *chgrpsunlesschgrps.empty?
else
sudo"/bin/mkdir","-p", HOMEBREW_PREFIX
sudo"/usr/sbin/chown", "root:wheel", HOMEBREW_PREFIX
end
unlessmkdirs.empty?
sudo"/bin/mkdir","-p", *mkdirs
sudo"/bin/chmod", "g+rwx", *mkdirs
sudo"/bin/chmod","755", *zsh_dirs
sudo"/usr/sbin/chown", ENV["USER"], *mkdirs
sudo"/usr/bin/chgrp", "admin", *mkdirs
end
sudo"/bin/mkdir","-p", HOMEBREW_CACHEunlessFile.directory? HOMEBREW_CACHE
sudo"/bin/chmod","g+rwx", HOMEBREW_CACHEifchmod? HOMEBREW_CACHE
sudo"/usr/sbin/chown", ENV["USER"], HOMEBREW_CACHEifchown? HOMEBREW_CACHE
sudo"/usr/bin/chgrp","admin", HOMEBREW_CACHEifchgrp? HOMEBREW_CACHE
ifshould_install_command_line_tools?
ohai"Searching online for the Command Line Tools"
# This temporary file prompts the 'softwareupdate' utility to list the Command Line Tools
clt_placeholder ="/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress"
sudo"/usr/bin/touch", clt_placeholder
clt_macos_version =ifmacos_version =="10.9"
"Mavericks"
else
macos_version
end
clt_sort =ifmacos_version >="10.13"
"sort -V"
else
"sort"
end
clt_label_command ="/usr/sbin/softwareupdate -l | " \
"grep -B 1 -E 'Command Line (Developer|Tools)' | " \
"awk -F'*' '/^ +\\*/ {print $2}' | " \
"grep '#{clt_macos_version}' | " \
"#{clt_sort} | "\
"sed 's/^ *//' | "\
"tail -n1"
clt_label =`#{clt_label_command}`.chomp
unlessclt_label.empty?
ohai"Installing #{clt_label}"
sudo"/usr/sbin/softwareupdate", "-i", clt_label
sudo"/bin/rm","-f", clt_placeholder
sudo"/usr/bin/xcode-select", "--switch", "/Library/Developer/CommandLineTools"
end
end
# Headless install may have failed, so fallback to original 'xcode-select' method
ifshould_install_command_line_tools? && STDIN.tty?
ohai"Installing the Command Line Tools (expect a GUI popup):"
sudo"/usr/bin/xcode-select", "--install"
puts"Press any key when the installation has completed."
getc
sudo"/usr/bin/xcode-select", "--switch", "/Library/Developer/CommandLineTools"
end
abort <<-EOABORTif`/usr/bin/xcrun clang 2>&1`=~/license/&& !$CHILD_STATUS.success?
You havenotagreed to the Xcode license.
Before running the installer again please agree to the license by opening
Xcode.apporrunning:
sudo xcodebuild -license
EOABORT
ohai "Downloading and installing Homebrew..."
Dir.chdir HOMEBREW_REPOSITORYdo
# we do it in four steps to avoid merge errors when reinstalling
system"git", "init", "-q"
# "git remote add" will fail if the remote is defined in the global config
system"git", "config", "remote.origin.url", BREW_REPO
system"git", "config", "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*"
# ensure we don't munge line endings on checkout
system"git", "config", "core.autocrlf", "false"
system"git", "fetch", "origin", "master:refs/remotes/origin/master",
"--tags", "--force"
system"git", "reset", "--hard", "origin/master"
system"ln", "-sf", "#{HOMEBREW_REPOSITORY}/bin/brew", "#{HOMEBREW_PREFIX}/bin/brew"
system"#{HOMEBREW_PREFIX}/bin/brew", "update", "--force"
end
warn "#{HOMEBREW_PREFIX}/bin is not in your PATH." unless ENV["PATH"].split(":").include? "#{HOMEBREW_PREFIX}/bin"
ohai "Installation successful!"
puts
# Use the shell's audible bell.
print"\a"
# Use an extra newline and bold to avoid this being missed.
ohai "Homebrew has enabled anonymous aggregate formulae and cask analytics."
puts <<-EOS
#{Tty.bold}Read the analytics documentation (andhow to opt-out) here:
#{Tty.underline}https://docs.brew.sh/Analytics#{Tty.reset}
EOS
ohai "Homebrew is run entirely by unpaid volunteers. Please consider donating:"
puts <<-EOS
#{Tty.underline}https://github.com/Homebrew/brew#donations#{Tty.reset}
EOS
Dir.chdir HOMEBREW_REPOSITORYdo
system"git", "config", "--local", "--replace-all", "homebrew.analyticsmessage", "true"
system"git", "config", "--local", "--replace-all", "homebrew.caskanalyticsmessage", "true"
end
ohai "Next steps:"
puts "- Run `brew help` to get started"
puts "- Further documentation: "
puts " #{Tty.underline}https://docs.brew.sh#{Tty.reset}"