brew/Library/Homebrew/utils.rb

137 lines
3.9 KiB
Ruby
Raw Normal View History

2023-02-27 11:43:16 -08:00
# typed: true
# frozen_string_literal: true
require "time"
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"
require "utils/gems"
2016-10-02 04:11:43 +02:00
require "utils/git"
2021-01-06 21:25:57 -08:00
require "utils/git_repository"
2016-10-02 04:11:43 +02:00
require "utils/github"
require "utils/gzip"
require "utils/inreplace"
require "utils/link"
require "utils/popen"
2020-07-06 03:32:18 +00:00
require "utils/repology"
require "utils/svn"
2016-08-26 16:04:47 +02:00
require "utils/tty"
2018-08-15 12:13:21 +02:00
require "tap_constants"
require "PATH"
2023-02-27 11:22:10 -08:00
require "extend/kernel"
module Homebrew
extend Context
2023-02-27 11:43:16 -08:00
extend T::Sig
2016-09-26 01:44:51 +02:00
module_function
2018-05-21 01:43:42 +02:00
def _system(cmd, *args, **options)
pid = fork do
yield if block_given?
args.map!(&:to_s)
2016-09-11 17:47:04 +01:00
begin
2018-05-21 01:43:42 +02:00
exec(cmd, *args, **options)
2016-09-11 17:47:04 +01:00
rescue
nil
end
exit! 1 # never gets here unless exec failed
end
2020-11-23 18:15:48 +01:00
Process.wait(T.must(pid))
$CHILD_STATUS.success?
end
2014-06-30 19:15:03 -05:00
2018-05-21 01:43:42 +02:00
def system(cmd, *args, **options)
if verbose?
puts "#{cmd} #{args * " "}".gsub(RUBY_PATH, "ruby")
.gsub($LOAD_PATH.join(File::PATH_SEPARATOR).to_s, "$LOAD_PATH")
end
2018-05-21 01:43:42 +02:00
_system(cmd, *args, **options)
end
# rubocop:disable Style/GlobalVars
2023-02-27 11:43:16 -08:00
sig { params(the_module: Module, pattern: Regexp).void }
def inject_dump_stats!(the_module, pattern)
@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]
the_module.module_eval do
instance_methods.grep(pattern).each do |name|
next if injected_methods.include? name
2018-09-17 02:45:00 +02:00
method = instance_method(name)
define_method(name) do |*args, &block|
time = Time.now
2020-11-23 18:15:48 +01:00
begin
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?
2018-09-17 02:45:00 +02:00
2016-09-23 22:02:23 +02:00
$times = {}
at_exit do
col_width = [$times.keys.map(&:size).max.to_i + 2, 15].max
2016-09-23 22:02:23 +02:00
$times.sort_by { |_k, v| v }.each do |method, time|
2019-10-03 08:50:45 +02:00
puts format("%<method>-#{col_width}s %<time>0.4f sec", method: "#{method}:", time: time)
end
end
end
# rubocop:enable Style/GlobalVars
end
2023-02-27 20:20:56 -08:00
module Utils
extend T::Sig
# Removes the rightmost segment from the constant expression in the string.
#
# deconstantize('Net::HTTP') # => "Net"
# deconstantize('::Net::HTTP') # => "::Net"
# deconstantize('String') # => ""
# deconstantize('::String') # => ""
# deconstantize('') # => ""
#
# See also #demodulize.
# @see https://github.com/rails/rails/blob/b0dd7c7/activesupport/lib/active_support/inflector/methods.rb#L247-L258
# `ActiveSupport::Inflector.deconstantize`
sig { params(path: String).returns(String) }
def self.deconstantize(path)
T.must(path[0, path.rindex("::") || 0]) # implementation based on the one in facets' Module#spacename
end
# Removes the module part from the expression in the string.
#
# demodulize('ActiveSupport::Inflector::Inflections') # => "Inflections"
# demodulize('Inflections') # => "Inflections"
# demodulize('::Inflections') # => "Inflections"
# demodulize('') # => ""
#
# See also #deconstantize.
# @see https://github.com/rails/rails/blob/b0dd7c7/activesupport/lib/active_support/inflector/methods.rb#L230-L245
# `ActiveSupport::Inflector.demodulize`
sig { params(path: String).returns(String) }
def self.demodulize(path)
if (i = path.rindex("::"))
T.must(path[(i + 2)..])
else
path
end
end
# A lightweight alternative to `ActiveSupport::Inflector.pluralize`:
# Combines `stem` with the `singular` or `plural` suffix based on `count`.
sig { params(stem: String, count: Integer, plural: String, singular: String).returns(String) }
def self.pluralize(stem, count, plural: "s", singular: "")
suffix = (count == 1) ? singular : plural
"#{stem}#{suffix}"
end
end