2015-08-03 13:09:07 +01:00
|
|
|
require "pathname"
|
2016-07-09 13:51:43 +01:00
|
|
|
require "emoji"
|
2015-08-03 13:09:07 +01:00
|
|
|
require "exceptions"
|
2016-10-02 04:11:43 +02:00
|
|
|
require "utils/analytics"
|
|
|
|
require "utils/curl"
|
|
|
|
require "utils/fork"
|
2016-08-30 21:38:13 +02:00
|
|
|
require "utils/formatter"
|
2016-10-02 04:11:43 +02:00
|
|
|
require "utils/git"
|
|
|
|
require "utils/github"
|
2016-05-28 20:09:49 +01:00
|
|
|
require "utils/hash"
|
2015-08-03 13:09:07 +01:00
|
|
|
require "utils/inreplace"
|
2016-09-30 18:22:53 -05:00
|
|
|
require "utils/link"
|
2015-08-03 13:09:07 +01:00
|
|
|
require "utils/popen"
|
2016-12-10 14:20:47 +00:00
|
|
|
require "utils/svn"
|
2016-08-26 16:04:47 +02:00
|
|
|
require "utils/tty"
|
2016-12-16 19:39:12 +01:00
|
|
|
require "time"
|
2009-10-15 14:42:19 +01:00
|
|
|
|
2017-06-14 12:51:36 +02:00
|
|
|
def require?(path)
|
|
|
|
return false if path.nil?
|
|
|
|
require path
|
|
|
|
rescue LoadError => e
|
|
|
|
# we should raise on syntax errors but not if the file doesn't exist.
|
|
|
|
raise unless e.message.include?(path)
|
|
|
|
end
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
def ohai(title, *sput)
|
2013-03-31 15:18:38 -05:00
|
|
|
title = Tty.truncate(title) if $stdout.tty? && !ARGV.verbose?
|
2016-08-30 21:38:13 +02:00
|
|
|
puts Formatter.headline(title, color: :blue)
|
2014-07-19 23:59:43 -05:00
|
|
|
puts sput
|
2009-07-31 02:51:17 +01:00
|
|
|
end
|
2009-07-31 03:56:46 +01:00
|
|
|
|
2016-08-10 13:17:26 -04:00
|
|
|
def oh1(title, options = {})
|
|
|
|
if $stdout.tty? && !ARGV.verbose? && options.fetch(:truncate, :auto) == :auto
|
|
|
|
title = Tty.truncate(title)
|
|
|
|
end
|
2016-08-30 21:38:13 +02:00
|
|
|
puts Formatter.headline(title, color: :green)
|
2011-08-23 23:19:32 +01:00
|
|
|
end
|
|
|
|
|
2015-08-29 10:56:24 +01:00
|
|
|
# Print a warning (do this rarely)
|
2016-08-30 21:38:13 +02:00
|
|
|
def opoo(message)
|
|
|
|
$stderr.puts Formatter.warning(message, label: "Warning")
|
2009-08-10 16:48:30 +01:00
|
|
|
end
|
|
|
|
|
2016-08-30 21:38:13 +02:00
|
|
|
def onoe(message)
|
|
|
|
$stderr.puts Formatter.error(message, label: "Error")
|
2009-08-10 16:48:30 +01:00
|
|
|
end
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
def ofail(error)
|
2012-04-30 14:08:59 +10:00
|
|
|
onoe error
|
|
|
|
Homebrew.failed = true
|
|
|
|
end
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
def odie(error)
|
2012-04-14 17:17:16 +10:00
|
|
|
onoe error
|
|
|
|
exit 1
|
|
|
|
end
|
2010-11-09 13:00:33 +00:00
|
|
|
|
2016-12-16 19:39:12 +01:00
|
|
|
def odeprecated(method, replacement = nil, disable: false, disable_on: nil, caller: send(:caller))
|
2016-07-27 15:30:17 -06:00
|
|
|
replacement_message = if replacement
|
|
|
|
"Use #{replacement} instead."
|
|
|
|
else
|
|
|
|
"There is no replacement."
|
|
|
|
end
|
|
|
|
|
2016-12-16 19:39:12 +01:00
|
|
|
unless disable_on.nil?
|
|
|
|
if disable_on > Time.now
|
|
|
|
will_be_disabled_message = " and will be disabled on #{disable_on.strftime("%Y-%m-%d")}"
|
|
|
|
else
|
|
|
|
disable = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
verb = if disable
|
|
|
|
"disabled"
|
|
|
|
else
|
|
|
|
"deprecated#{will_be_disabled_message}"
|
|
|
|
end
|
|
|
|
|
2016-07-30 14:51:14 +02:00
|
|
|
# Try to show the most relevant location in message, i.e. (if applicable):
|
|
|
|
# - Location in a formula.
|
|
|
|
# - Location outside of 'compat/'.
|
|
|
|
# - Location of caller of deprecated method (if all else fails).
|
2016-12-16 19:39:12 +01:00
|
|
|
backtrace = caller
|
2016-07-31 19:12:04 +01:00
|
|
|
tap_message = nil
|
2016-07-30 14:51:14 +02:00
|
|
|
caller_message = backtrace.detect do |line|
|
2017-04-27 20:17:06 +02:00
|
|
|
next unless line =~ %r{^#{Regexp.escape(HOMEBREW_LIBRARY)}/Taps/([^/]+/[^/]+)/}
|
2017-06-10 20:23:20 +03:00
|
|
|
tap = Tap.fetch Regexp.last_match(1)
|
2016-09-11 17:47:04 +01:00
|
|
|
tap_message = "\nPlease report this to the #{tap} tap!"
|
|
|
|
true
|
2016-07-30 14:51:14 +02:00
|
|
|
end
|
|
|
|
caller_message ||= backtrace.detect do |line|
|
|
|
|
!line.start_with?("#{HOMEBREW_LIBRARY_PATH}/compat/")
|
|
|
|
end
|
|
|
|
caller_message ||= backtrace[1]
|
2016-07-27 15:30:17 -06:00
|
|
|
|
2017-10-15 02:28:32 +02:00
|
|
|
message = <<~EOS
|
2016-07-27 15:30:17 -06:00
|
|
|
Calling #{method} is #{verb}!
|
|
|
|
#{replacement_message}
|
2016-07-31 19:12:04 +01:00
|
|
|
#{caller_message}#{tap_message}
|
2016-07-27 15:30:17 -06:00
|
|
|
EOS
|
|
|
|
|
2016-12-16 19:39:12 +01:00
|
|
|
if ARGV.homebrew_developer? || disable ||
|
2016-08-12 21:06:41 +01:00
|
|
|
Homebrew.raise_deprecation_exceptions?
|
2016-10-08 12:04:38 +02:00
|
|
|
raise MethodDeprecatedError, message
|
2017-09-02 12:38:18 +05:30
|
|
|
elsif !Homebrew.auditing?
|
2016-07-27 15:30:17 -06:00
|
|
|
opoo "#{message}\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def odisabled(method, replacement = nil, options = {})
|
2016-12-16 19:39:12 +01:00
|
|
|
options = { disable: true, caller: caller }.merge(options)
|
2016-07-30 14:59:16 +02:00
|
|
|
odeprecated(method, replacement, options)
|
2016-07-27 15:30:17 -06:00
|
|
|
end
|
|
|
|
|
2015-12-07 13:11:34 +00:00
|
|
|
def pretty_installed(f)
|
|
|
|
if !$stdout.tty?
|
2016-09-11 17:47:04 +01:00
|
|
|
f.to_s
|
2016-07-09 13:51:43 +01:00
|
|
|
elsif Emoji.enabled?
|
2017-01-20 12:27:49 +00:00
|
|
|
"#{Tty.bold}#{f} #{Formatter.success("✔")}#{Tty.reset}"
|
2015-12-07 13:11:34 +00:00
|
|
|
else
|
2016-08-30 21:38:13 +02:00
|
|
|
Formatter.success("#{Tty.bold}#{f} (installed)#{Tty.reset}")
|
2015-12-07 13:11:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def pretty_uninstalled(f)
|
|
|
|
if !$stdout.tty?
|
2016-09-11 17:47:04 +01:00
|
|
|
f.to_s
|
2016-07-09 13:51:43 +01:00
|
|
|
elsif Emoji.enabled?
|
2017-01-20 12:27:49 +00:00
|
|
|
"#{Tty.bold}#{f} #{Formatter.error("✘")}#{Tty.reset}"
|
2015-12-07 13:11:34 +00:00
|
|
|
else
|
2016-08-30 21:38:13 +02:00
|
|
|
Formatter.error("#{Tty.bold}#{f} (uninstalled)#{Tty.reset}")
|
2015-12-07 13:11:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
def pretty_duration(s)
|
2015-10-07 22:48:42 +02:00
|
|
|
s = s.to_i
|
|
|
|
res = ""
|
|
|
|
|
|
|
|
if s > 59
|
|
|
|
m = s / 60
|
|
|
|
s %= 60
|
2017-03-11 11:33:12 +01:00
|
|
|
res = Formatter.pluralize(m, "minute")
|
2016-09-11 17:47:04 +01:00
|
|
|
return res if s.zero?
|
2015-10-07 22:48:42 +02:00
|
|
|
res << " "
|
|
|
|
end
|
|
|
|
|
2017-03-11 11:33:12 +01:00
|
|
|
res << Formatter.pluralize(s, "second")
|
2009-08-10 16:48:30 +01:00
|
|
|
end
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
def interactive_shell(f = nil)
|
2010-08-20 10:01:49 -07:00
|
|
|
unless f.nil?
|
2015-08-03 13:09:07 +01:00
|
|
|
ENV["HOMEBREW_DEBUG_PREFIX"] = f.prefix
|
|
|
|
ENV["HOMEBREW_DEBUG_INSTALL"] = f.full_name
|
2010-08-20 10:01:49 -07:00
|
|
|
end
|
|
|
|
|
2015-07-28 14:50:58 +08:00
|
|
|
if ENV["SHELL"].include?("zsh") && ENV["HOME"].start_with?(HOMEBREW_TEMP.resolved_path.to_s)
|
|
|
|
FileUtils.touch "#{ENV["HOME"]}/.zshrc"
|
|
|
|
end
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
Process.wait fork { exec ENV["SHELL"] }
|
2014-03-29 17:47:42 -05:00
|
|
|
|
2017-06-10 20:12:55 +03:00
|
|
|
return if $CHILD_STATUS.success?
|
|
|
|
raise "Aborted due to non-zero exit status (#{$CHILD_STATUS.exitstatus})" if $CHILD_STATUS.exited?
|
|
|
|
raise $CHILD_STATUS.inspect
|
2009-07-31 03:56:46 +01:00
|
|
|
end
|
2009-08-11 12:20:55 -07:00
|
|
|
|
2010-01-13 09:00:51 +00:00
|
|
|
module Homebrew
|
2016-09-26 01:44:51 +02:00
|
|
|
module_function
|
|
|
|
|
|
|
|
def _system(cmd, *args)
|
2014-03-29 02:24:01 -05:00
|
|
|
pid = fork do
|
2010-01-13 09:00:51 +00:00
|
|
|
yield if block_given?
|
2015-08-03 13:09:07 +01:00
|
|
|
args.collect!(&:to_s)
|
2016-09-11 17:47:04 +01:00
|
|
|
begin
|
|
|
|
exec(cmd, *args)
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
2010-01-13 09:00:51 +00:00
|
|
|
exit! 1 # never gets here unless exec failed
|
|
|
|
end
|
2014-03-29 02:24:01 -05:00
|
|
|
Process.wait(pid)
|
2017-06-10 20:12:55 +03:00
|
|
|
$CHILD_STATUS.success?
|
2010-01-13 09:00:51 +00:00
|
|
|
end
|
2014-06-30 19:15:03 -05:00
|
|
|
|
2016-09-26 01:44:51 +02:00
|
|
|
def system(cmd, *args)
|
2017-06-01 16:06:51 +02:00
|
|
|
puts "#{cmd} #{args * " "}" if ARGV.verbose?
|
2015-09-08 16:06:39 +08:00
|
|
|
_system(cmd, *args)
|
|
|
|
end
|
|
|
|
|
2017-05-07 17:28:37 +01:00
|
|
|
def install_gem_setup_path!(name, version = nil, executable = name)
|
|
|
|
# Respect user's preferences for where gems should be installed.
|
2017-11-04 16:31:18 +00:00
|
|
|
ENV["GEM_HOME"] = if ENV["HOMEBREW_GEM_HOME"].to_s.empty?
|
|
|
|
Gem.user_dir
|
|
|
|
else
|
|
|
|
ENV["HOMEBREW_GEM_HOME"]
|
|
|
|
end
|
2017-10-29 14:44:43 +00:00
|
|
|
unless ENV["HOMEBREW_GEM_PATH"].to_s.empty?
|
|
|
|
ENV["GEM_PATH"] = ENV["HOMEBREW_GEM_PATH"]
|
|
|
|
end
|
2017-05-07 17:28:37 +01:00
|
|
|
|
|
|
|
# Make rubygems notice env changes.
|
|
|
|
Gem.clear_paths
|
|
|
|
Gem::Specification.reset
|
|
|
|
|
|
|
|
# Add Gem binary directory and (if missing) Ruby binary directory to PATH.
|
|
|
|
path = PATH.new(ENV["PATH"])
|
|
|
|
path.prepend(RUBY_BIN) if which("ruby") != RUBY_PATH
|
|
|
|
path.prepend(Gem.bindir)
|
|
|
|
ENV["PATH"] = path
|
|
|
|
|
|
|
|
if Gem::Specification.find_all_by_name(name, version).empty?
|
|
|
|
ohai "Installing or updating '#{name}' gem"
|
|
|
|
install_args = %W[--no-ri --no-rdoc #{name}]
|
|
|
|
install_args << "--version" << version if version
|
|
|
|
|
|
|
|
# Do `gem install [...]` without having to spawn a separate process or
|
|
|
|
# having to find the right `gem` binary for the running Ruby interpreter.
|
|
|
|
require "rubygems/commands/install_command"
|
|
|
|
install_cmd = Gem::Commands::InstallCommand.new
|
|
|
|
install_cmd.handle_options(install_args)
|
|
|
|
exit_code = 1 # Should not matter as `install_cmd.execute` always throws.
|
|
|
|
begin
|
|
|
|
install_cmd.execute
|
|
|
|
rescue Gem::SystemExitException => e
|
|
|
|
exit_code = e.exit_code
|
|
|
|
end
|
|
|
|
odie "Failed to install/update the '#{name}' gem." if exit_code.nonzero?
|
|
|
|
end
|
|
|
|
|
|
|
|
return if which(executable)
|
2017-10-15 02:28:32 +02:00
|
|
|
odie <<~EOS
|
2017-05-07 17:28:37 +01:00
|
|
|
The '#{name}' gem is installed but couldn't find '#{executable}' in the PATH:
|
|
|
|
#{ENV["PATH"]}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2017-10-07 00:31:28 +02:00
|
|
|
# rubocop:disable Style/GlobalVars
|
2016-04-18 17:39:21 -04:00
|
|
|
def inject_dump_stats!(the_module, pattern)
|
2017-10-07 00:31:28 +02:00
|
|
|
@injected_dump_stat_modules ||= {}
|
2016-09-26 01:44:51 +02:00
|
|
|
@injected_dump_stat_modules[the_module] ||= []
|
|
|
|
injected_methods = @injected_dump_stat_modules[the_module]
|
2016-04-18 17:39:21 -04:00
|
|
|
the_module.module_eval do
|
|
|
|
instance_methods.grep(pattern).each do |name|
|
|
|
|
next if injected_methods.include? name
|
|
|
|
method = instance_method(name)
|
|
|
|
define_method(name) do |*args, &block|
|
|
|
|
begin
|
|
|
|
time = Time.now
|
|
|
|
method.bind(self).call(*args, &block)
|
|
|
|
ensure
|
|
|
|
$times[name] ||= 0
|
|
|
|
$times[name] += Time.now - time
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-23 22:02:23 +02:00
|
|
|
return unless $times.nil?
|
|
|
|
$times = {}
|
|
|
|
at_exit do
|
|
|
|
col_width = [$times.keys.map(&:size).max + 2, 15].max
|
|
|
|
$times.sort_by { |_k, v| v }.each do |method, time|
|
|
|
|
puts format("%-*s %0.4f sec", col_width, "#{method}:", time)
|
2016-04-18 17:39:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-10-07 00:31:28 +02:00
|
|
|
# rubocop:enable Style/GlobalVars
|
2010-01-13 09:00:51 +00:00
|
|
|
end
|
|
|
|
|
2017-06-01 17:45:07 -07:00
|
|
|
def with_homebrew_path
|
2017-10-29 14:44:43 +00:00
|
|
|
with_env(PATH: PATH.new(ENV["HOMEBREW_PATH"])) do
|
|
|
|
yield
|
|
|
|
end
|
2017-06-01 17:45:07 -07:00
|
|
|
end
|
|
|
|
|
2016-08-01 04:07:59 +01:00
|
|
|
def with_custom_locale(locale)
|
2017-10-29 14:44:43 +00:00
|
|
|
with_env(LC_ALL: locale) do
|
|
|
|
yield
|
|
|
|
end
|
2016-08-01 04:07:59 +01:00
|
|
|
end
|
|
|
|
|
2017-10-29 14:44:43 +00:00
|
|
|
def run_as_not_developer
|
|
|
|
with_env(HOMEBREW_DEVELOPER: nil) do
|
2017-07-13 17:26:13 -07:00
|
|
|
yield
|
|
|
|
end
|
2015-05-18 18:44:27 +08:00
|
|
|
end
|
|
|
|
|
2009-08-11 12:20:55 -07:00
|
|
|
# Kernel.system but with exceptions
|
2015-08-03 13:09:07 +01:00
|
|
|
def safe_system(cmd, *args)
|
|
|
|
Homebrew.system(cmd, *args) || raise(ErrorDuringExecution.new(cmd, args))
|
2010-01-13 09:00:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# prints no output
|
2015-08-03 13:09:07 +01:00
|
|
|
def quiet_system(cmd, *args)
|
2015-09-08 16:06:39 +08:00
|
|
|
Homebrew._system(cmd, *args) do
|
2012-07-06 12:56:18 -08:00
|
|
|
# Redirect output streams to `/dev/null` instead of closing as some programs
|
|
|
|
# will fail to execute if they can't write to an open stream.
|
2015-08-03 13:09:07 +01:00
|
|
|
$stdout.reopen("/dev/null")
|
|
|
|
$stderr.reopen("/dev/null")
|
2009-11-09 17:44:29 +00:00
|
|
|
end
|
2009-08-11 12:20:55 -07:00
|
|
|
end
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
def which(cmd, path = ENV["PATH"])
|
2017-04-28 12:39:00 +02:00
|
|
|
PATH.new(path).each do |p|
|
2015-08-14 22:57:49 +02:00
|
|
|
begin
|
|
|
|
pcmd = File.expand_path(cmd, p)
|
|
|
|
rescue ArgumentError
|
|
|
|
# File.expand_path will raise an ArgumentError if the path is malformed.
|
2016-05-01 14:55:45 +02:00
|
|
|
# See https://github.com/Homebrew/legacy-homebrew/issues/32789
|
2015-08-14 22:57:49 +02:00
|
|
|
next
|
|
|
|
end
|
2014-07-06 13:35:44 -05:00
|
|
|
return Pathname.new(pcmd) if File.file?(pcmd) && File.executable?(pcmd)
|
2014-03-16 11:52:11 -07:00
|
|
|
end
|
2015-08-03 13:09:07 +01:00
|
|
|
nil
|
2012-03-03 15:50:24 -08:00
|
|
|
end
|
|
|
|
|
2016-01-02 23:08:51 +08:00
|
|
|
def which_all(cmd, path = ENV["PATH"])
|
2017-04-28 12:39:00 +02:00
|
|
|
PATH.new(path).map do |p|
|
2016-01-02 23:08:51 +08:00
|
|
|
begin
|
|
|
|
pcmd = File.expand_path(cmd, p)
|
|
|
|
rescue ArgumentError
|
|
|
|
# File.expand_path will raise an ArgumentError if the path is malformed.
|
2016-05-01 14:55:45 +02:00
|
|
|
# See https://github.com/Homebrew/legacy-homebrew/issues/32789
|
2016-01-02 23:08:51 +08:00
|
|
|
next
|
|
|
|
end
|
|
|
|
Pathname.new(pcmd) if File.file?(pcmd) && File.executable?(pcmd)
|
|
|
|
end.compact.uniq
|
|
|
|
end
|
|
|
|
|
2011-12-16 14:27:58 -08:00
|
|
|
def which_editor
|
2017-06-07 16:07:40 +01:00
|
|
|
editor = ENV.values_at("HOMEBREW_EDITOR", "HOMEBREW_VISUAL")
|
|
|
|
.compact
|
|
|
|
.reject(&:empty?)
|
|
|
|
.first
|
|
|
|
return editor if editor
|
|
|
|
|
|
|
|
# Find Atom, Sublime Text, Textmate, BBEdit / TextWrangler, or vim
|
|
|
|
editor = %w[atom subl mate edit vim].find do |candidate|
|
|
|
|
candidate if which(candidate, ENV["HOMEBREW_PATH"])
|
2017-04-24 08:49:11 +01:00
|
|
|
end
|
2017-12-11 15:32:57 -08:00
|
|
|
editor ||= "vim"
|
2015-01-05 15:23:35 -05:00
|
|
|
|
2017-10-15 02:28:32 +02:00
|
|
|
opoo <<~EOS
|
2015-01-05 15:23:35 -05:00
|
|
|
Using #{editor} because no editor was set in the environment.
|
2017-04-19 10:49:20 +01:00
|
|
|
This may change in the future, so we recommend setting EDITOR,
|
2015-01-05 15:23:35 -05:00
|
|
|
or HOMEBREW_EDITOR to your preferred text editor.
|
|
|
|
EOS
|
|
|
|
|
2017-06-01 17:45:07 -07:00
|
|
|
editor
|
2011-12-16 14:27:58 -08:00
|
|
|
end
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
def exec_editor(*args)
|
2016-07-04 16:09:24 +01:00
|
|
|
puts "Editing #{args.join "\n"}"
|
2017-06-01 17:45:07 -07:00
|
|
|
with_homebrew_path { safe_exec(which_editor, *args) }
|
2012-12-27 23:34:29 -06:00
|
|
|
end
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
def exec_browser(*args)
|
2017-05-13 11:42:01 +01:00
|
|
|
browser = ENV["HOMEBREW_BROWSER"]
|
2016-04-25 17:53:43 +01:00
|
|
|
browser ||= OS::PATH_OPEN if defined?(OS::PATH_OPEN)
|
|
|
|
return unless browser
|
2012-12-27 23:34:29 -06:00
|
|
|
safe_exec(browser, *args)
|
|
|
|
end
|
2010-11-08 13:06:45 +00:00
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
def safe_exec(cmd, *args)
|
2012-12-27 23:34:29 -06:00
|
|
|
# This buys us proper argument quoting and evaluation
|
|
|
|
# of environment variables in the cmd parameter.
|
2014-09-20 11:42:19 -05:00
|
|
|
exec "/bin/sh", "-c", "#{cmd} \"$@\"", "--", *args
|
2009-09-05 14:03:41 -04:00
|
|
|
end
|
2009-09-08 15:31:28 -07:00
|
|
|
|
2010-11-09 13:00:33 +00:00
|
|
|
# GZips the given paths, and returns the gzipped paths
|
2015-08-03 13:09:07 +01:00
|
|
|
def gzip(*paths)
|
2010-09-12 22:55:52 +02:00
|
|
|
paths.collect do |path|
|
2017-12-12 09:27:06 -08:00
|
|
|
safe_system "gzip", path
|
2010-11-09 13:00:33 +00:00
|
|
|
Pathname.new("#{path}.gz")
|
2010-09-12 22:55:52 +02:00
|
|
|
end
|
2010-01-29 10:15:33 -08:00
|
|
|
end
|
|
|
|
|
2010-05-10 10:14:20 -07:00
|
|
|
# Returns array of architectures that the given command or library is built for.
|
2015-08-03 13:09:07 +01:00
|
|
|
def archs_for_command(cmd)
|
2012-05-28 20:39:05 -05:00
|
|
|
cmd = which(cmd) unless Pathname.new(cmd).absolute?
|
|
|
|
Pathname.new(cmd).archs
|
2009-09-08 15:31:28 -07:00
|
|
|
end
|
2009-10-15 12:36:09 +01:00
|
|
|
|
2012-09-11 20:59:59 -04:00
|
|
|
def ignore_interrupts(opt = nil)
|
|
|
|
std_trap = trap("INT") do
|
2012-09-27 16:21:39 -05:00
|
|
|
puts "One sec, just cleaning up" unless opt == :quietly
|
2012-09-11 20:59:59 -04:00
|
|
|
end
|
2009-11-06 17:09:14 +00:00
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
trap("INT", std_trap)
|
|
|
|
end
|
2009-11-09 18:24:36 +00:00
|
|
|
|
2016-11-11 20:08:26 +00:00
|
|
|
def capture_stderr
|
2016-11-14 12:28:45 +00:00
|
|
|
old = $stderr
|
|
|
|
$stderr = StringIO.new
|
2016-11-11 20:08:26 +00:00
|
|
|
yield
|
|
|
|
$stderr.string
|
|
|
|
ensure
|
|
|
|
$stderr = old
|
|
|
|
end
|
|
|
|
|
2009-11-09 18:24:36 +00:00
|
|
|
def nostdout
|
|
|
|
if ARGV.verbose?
|
|
|
|
yield
|
|
|
|
else
|
|
|
|
begin
|
2014-07-06 15:03:25 -05:00
|
|
|
out = $stdout.dup
|
|
|
|
$stdout.reopen("/dev/null")
|
2009-11-09 18:24:36 +00:00
|
|
|
yield
|
|
|
|
ensure
|
2014-07-06 15:03:25 -05:00
|
|
|
$stdout.reopen(out)
|
|
|
|
out.close
|
2009-11-09 18:24:36 +00:00
|
|
|
end
|
|
|
|
end
|
2010-01-13 09:00:51 +00:00
|
|
|
end
|
2010-04-07 21:01:12 -07:00
|
|
|
|
2017-11-17 17:41:58 +00:00
|
|
|
def paths
|
|
|
|
@paths ||= PATH.new(ENV["HOMEBREW_PATH"]).collect do |p|
|
2013-09-15 20:11:17 -07:00
|
|
|
begin
|
2015-08-03 13:09:07 +01:00
|
|
|
File.expand_path(p).chomp("/")
|
2013-09-15 20:11:17 -07:00
|
|
|
rescue ArgumentError
|
|
|
|
onoe "The following PATH component is invalid: #{p}"
|
|
|
|
end
|
|
|
|
end.uniq.compact
|
|
|
|
end
|
|
|
|
|
2015-11-17 16:12:31 +05:30
|
|
|
def disk_usage_readable(size_in_bytes)
|
2015-12-30 16:56:05 +08:00
|
|
|
if size_in_bytes >= 1_073_741_824
|
|
|
|
size = size_in_bytes.to_f / 1_073_741_824
|
2017-02-25 11:09:57 +00:00
|
|
|
unit = "GB"
|
2015-12-30 16:56:05 +08:00
|
|
|
elsif size_in_bytes >= 1_048_576
|
|
|
|
size = size_in_bytes.to_f / 1_048_576
|
2017-02-25 11:09:57 +00:00
|
|
|
unit = "MB"
|
2015-12-30 16:56:05 +08:00
|
|
|
elsif size_in_bytes >= 1_024
|
|
|
|
size = size_in_bytes.to_f / 1_024
|
2017-02-25 11:09:57 +00:00
|
|
|
unit = "KB"
|
2015-11-17 16:12:31 +05:30
|
|
|
else
|
2015-12-30 16:56:05 +08:00
|
|
|
size = size_in_bytes
|
|
|
|
unit = "B"
|
2015-11-17 16:12:31 +05:30
|
|
|
end
|
|
|
|
|
2015-12-30 16:56:05 +08:00
|
|
|
# avoid trailing zero after decimal point
|
2016-09-11 17:47:04 +01:00
|
|
|
if ((size * 10).to_i % 10).zero?
|
2015-12-30 16:56:05 +08:00
|
|
|
"#{size.to_i}#{unit}"
|
2015-11-17 16:12:31 +05:30
|
|
|
else
|
2016-09-11 17:47:04 +01:00
|
|
|
"#{format("%.1f", size)}#{unit}"
|
2015-11-17 16:12:31 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def number_readable(number)
|
|
|
|
numstr = number.to_i.to_s
|
|
|
|
(numstr.size - 3).step(1, -3) { |i| numstr.insert(i, ",") }
|
|
|
|
numstr
|
|
|
|
end
|
2016-07-05 08:45:17 -04:00
|
|
|
|
|
|
|
# Truncates a text string to fit within a byte size constraint,
|
|
|
|
# preserving character encoding validity. The returned string will
|
|
|
|
# be not much longer than the specified max_bytes, though the exact
|
|
|
|
# shortfall or overrun may vary.
|
|
|
|
def truncate_text_to_approximate_size(s, max_bytes, options = {})
|
|
|
|
front_weight = options.fetch(:front_weight, 0.5)
|
|
|
|
if front_weight < 0.0 || front_weight > 1.0
|
|
|
|
raise "opts[:front_weight] must be between 0.0 and 1.0"
|
|
|
|
end
|
|
|
|
return s if s.bytesize <= max_bytes
|
|
|
|
|
|
|
|
glue = "\n[...snip...]\n"
|
|
|
|
max_bytes_in = [max_bytes - glue.bytesize, 1].max
|
2016-08-18 14:35:39 +08:00
|
|
|
bytes = s.dup.force_encoding("BINARY")
|
|
|
|
glue_bytes = glue.encode("BINARY")
|
2016-07-05 08:45:17 -04:00
|
|
|
n_front_bytes = (max_bytes_in * front_weight).floor
|
|
|
|
n_back_bytes = max_bytes_in - n_front_bytes
|
2016-09-11 17:47:04 +01:00
|
|
|
if n_front_bytes.zero?
|
2016-07-05 08:45:17 -04:00
|
|
|
front = bytes[1..0]
|
|
|
|
back = bytes[-max_bytes_in..-1]
|
2016-09-11 17:47:04 +01:00
|
|
|
elsif n_back_bytes.zero?
|
2016-07-05 08:45:17 -04:00
|
|
|
front = bytes[0..(max_bytes_in - 1)]
|
|
|
|
back = bytes[1..0]
|
|
|
|
else
|
|
|
|
front = bytes[0..(n_front_bytes - 1)]
|
|
|
|
back = bytes[-n_back_bytes..-1]
|
|
|
|
end
|
|
|
|
out = front + glue_bytes + back
|
2016-08-18 14:35:39 +08:00
|
|
|
out.force_encoding("UTF-8")
|
2016-09-17 15:32:44 +01:00
|
|
|
out.encode!("UTF-16", invalid: :replace)
|
2016-08-18 14:35:39 +08:00
|
|
|
out.encode!("UTF-8")
|
2016-07-05 08:45:17 -04:00
|
|
|
out
|
|
|
|
end
|
2016-08-24 11:06:20 +01:00
|
|
|
|
2016-09-17 15:00:46 +01:00
|
|
|
def migrate_legacy_keg_symlinks_if_necessary
|
|
|
|
legacy_linked_kegs = HOMEBREW_LIBRARY/"LinkedKegs"
|
|
|
|
return unless legacy_linked_kegs.directory?
|
|
|
|
|
2016-09-17 19:23:42 +01:00
|
|
|
HOMEBREW_LINKED_KEGS.mkpath unless legacy_linked_kegs.children.empty?
|
2016-09-17 18:50:25 +01:00
|
|
|
legacy_linked_kegs.children.each do |link|
|
2016-09-17 23:55:28 -04:00
|
|
|
name = link.basename.to_s
|
2016-09-17 18:50:25 +01:00
|
|
|
src = begin
|
|
|
|
link.realpath
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
begin
|
|
|
|
(HOMEBREW_PREFIX/"opt/#{name}").realpath
|
|
|
|
rescue Errno::ENOENT
|
2016-09-19 10:40:16 +01:00
|
|
|
begin
|
|
|
|
Formulary.factory(name).installed_prefix
|
|
|
|
rescue
|
|
|
|
next
|
|
|
|
end
|
2016-09-17 18:50:25 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
dst = HOMEBREW_LINKED_KEGS/name
|
2016-09-17 19:02:36 +01:00
|
|
|
dst.unlink if dst.exist?
|
2016-09-17 18:50:25 +01:00
|
|
|
FileUtils.ln_sf(src.relative_path_from(dst.parent), dst)
|
2016-09-17 15:00:46 +01:00
|
|
|
end
|
|
|
|
FileUtils.rm_rf legacy_linked_kegs
|
|
|
|
|
|
|
|
legacy_pinned_kegs = HOMEBREW_LIBRARY/"PinnedKegs"
|
|
|
|
return unless legacy_pinned_kegs.directory?
|
|
|
|
|
2016-09-17 19:23:42 +01:00
|
|
|
HOMEBREW_PINNED_KEGS.mkpath unless legacy_pinned_kegs.children.empty?
|
2016-09-17 18:50:25 +01:00
|
|
|
legacy_pinned_kegs.children.each do |link|
|
2016-09-17 23:55:28 -04:00
|
|
|
name = link.basename.to_s
|
2016-09-17 18:50:25 +01:00
|
|
|
src = link.realpath
|
|
|
|
dst = HOMEBREW_PINNED_KEGS/name
|
|
|
|
FileUtils.ln_sf(src.relative_path_from(dst.parent), dst)
|
2016-09-17 15:00:46 +01:00
|
|
|
end
|
|
|
|
FileUtils.rm_rf legacy_pinned_kegs
|
|
|
|
end
|
2017-07-13 17:14:21 -07:00
|
|
|
|
|
|
|
# Calls the given block with the passed environment variables
|
|
|
|
# added to ENV, then restores ENV afterwards.
|
|
|
|
# Example:
|
2017-10-29 14:44:43 +00:00
|
|
|
# with_env(PATH: "/bin") do
|
2017-07-13 17:14:21 -07:00
|
|
|
# system "echo $PATH"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Note that this method is *not* thread-safe - other threads
|
|
|
|
# which happen to be scheduled during the block will also
|
|
|
|
# see these environment variables.
|
|
|
|
def with_env(hash)
|
|
|
|
old_values = {}
|
|
|
|
begin
|
|
|
|
hash.each do |key, value|
|
2017-10-29 14:44:43 +00:00
|
|
|
key = key.to_s
|
2017-07-13 17:14:21 -07:00
|
|
|
old_values[key] = ENV.delete(key)
|
|
|
|
ENV[key] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
yield if block_given?
|
|
|
|
ensure
|
|
|
|
ENV.update(old_values)
|
|
|
|
end
|
|
|
|
end
|
2017-07-14 17:03:33 +01:00
|
|
|
|
|
|
|
def shell_profile
|
|
|
|
Utils::Shell.profile
|
|
|
|
end
|
2017-07-08 19:33:44 -07:00
|
|
|
|
|
|
|
def tap_and_name_comparison
|
|
|
|
proc do |a, b|
|
|
|
|
if a.include?("/") && !b.include?("/")
|
|
|
|
1
|
|
|
|
elsif !a.include?("/") && b.include?("/")
|
|
|
|
-1
|
|
|
|
else
|
|
|
|
a <=> b
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|