Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
issyl0 committed Jul 6, 2024
1 parent cd18694 commit 0a18f77
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
16 changes: 9 additions & 7 deletions Library/Homebrew/development_tools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def locate(tool)
# Don't call tools (cc, make, strip, etc.) directly!
# Give the name of the binary you look for as a string to this method
# in order to get the full path back as a Pathname.
(@locate ||= T.let({}, T.nilable(T::Hash[T.untyped, T.untyped]))).fetch(tool) do |key|
(@locate ||= T.let({}, T.nilable(T::Hash[T.any(String, Symbol), T.untyped]))).fetch(tool) do |key|
@locate[key] = if File.executable?((path = "/usr/bin/#{tool}"))
Pathname.new path
# Homebrew GCCs most frequently; much faster to check this before xcrun
Expand Down Expand Up @@ -63,8 +63,9 @@ def default_compiler
sig { returns(Version) }
def clang_version
@clang_version ||= T.let(
if (path = locate("clang")) && (bv = `#{path} --version`[/(?:clang|LLVM) version (\d+\.\d(?:\.\d)?)/, 1])
Version.new(bv)
if (path = locate("clang")) &&
(build_version = `#{path} --version`[/(?:clang|LLVM) version (\d+\.\d(?:\.\d)?)/, 1])
Version.new(build_version)
else
Version::NULL
end, T.nilable(Version)
Expand Down Expand Up @@ -93,8 +94,8 @@ def clang_build_version
def llvm_clang_build_version
@llvm_clang_build_version ||= T.let(begin
path = Formulary.factory("llvm").opt_prefix/"bin/clang"
if path.executable? && (bv = `#{path} --version`[/clang version (\d+\.\d\.\d)/, 1])
Version.new(bv)
if path.executable? && (build_version = `#{path} --version`[/clang version (\d+\.\d\.\d)/, 1])
Version.new(build_version)
else
Version::NULL
end
Expand All @@ -109,8 +110,9 @@ def gcc_version(cc)
(@gcc_version ||= T.let({}, T.nilable(T::Hash[String, Version]))).fetch(cc) do
path = HOMEBREW_PREFIX/"opt/#{CompilerSelector.preferred_gcc}/bin"/cc
path = locate(cc) unless path.exist?
version = if path && (bv = `#{path} --version`[/gcc(?:(?:-\d+(?:\.\d)?)? \(.+\))? (\d+\.\d\.\d)/, 1])
Version.new(bv)
version = if path &&
(build_version = `#{path} --version`[/gcc(?:(?:-\d+(?:\.\d)?)? \(.+\))? (\d+\.\d\.\d)/, 1])
Version.new(build_version)
else
Version::NULL
end
Expand Down
17 changes: 10 additions & 7 deletions Library/Homebrew/unpack_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
module UnpackStrategy
extend T::Helpers

# FIXME: Enable cop again when https://github.com/sorbet/sorbet/issues/3532 is fixed.
AnyStrategy = T.type_alias do # rubocop:disable Style/MutableConstant
T.any(
T.class_of(UnpackStrategy::Tar),
Expand Down Expand Up @@ -101,6 +102,8 @@ def self.from_type(type)

sig { params(extension: String).returns(T.nilable(AnyStrategy)) }
def self.from_extension(extension)
return unless strategies

strategies&.sort_by { |s| s.extensions.map(&:length).max || 0 }
&.reverse
&.find { |s| s.extensions.any? { |ext| extension.end_with?(ext) } }
Expand All @@ -113,15 +116,15 @@ def self.from_magic(path)

sig {
params(path: Pathname, prioritize_extension: T::Boolean, type: T.nilable(Symbol), ref_type: T.nilable(String),
ref: T.nilable(String), merge_xattrs: T.nilable(T::Boolean)).returns(T.untyped)
ref: T.nilable(String), merge_xattrs: T::Boolean).returns(T.untyped)
}
def self.detect(path, prioritize_extension: false, type: nil, ref_type: nil, ref: nil, merge_xattrs: nil)
def self.detect(path, prioritize_extension: false, type: nil, ref_type: nil, ref: nil, merge_xattrs: false)
strategy = from_type(type) if type

if prioritize_extension && path.extname.present?
strategy ||= from_extension(path.extname)
strategy ||= strategies&.select { |s| s < Directory || s == Fossil }
&.find { |s| s.can_extract?(path) }

strategy ||= strategies&.find { |s| (s < Directory || s == Fossil) && s.can_extract?(path) }
else
strategy ||= from_magic(path)
strategy ||= from_extension(path.extname)
Expand All @@ -135,14 +138,14 @@ def self.detect(path, prioritize_extension: false, type: nil, ref_type: nil, ref
sig { returns(Pathname) }
attr_reader :path

sig { returns(T.nilable(T::Boolean)) }
sig { returns(T::Boolean) }
attr_reader :merge_xattrs

sig {
params(path: T.any(String, Pathname), ref_type: T.nilable(String), ref: T.nilable(String),
merge_xattrs: T.nilable(T::Boolean)).void
merge_xattrs: T::Boolean).void
}
def initialize(path, ref_type: nil, ref: nil, merge_xattrs: nil)
def initialize(path, ref_type: nil, ref: nil, merge_xattrs: false)
@path = T.let(Pathname(path).expand_path, Pathname)
@ref_type = ref_type
@ref = ref
Expand Down

0 comments on commit 0a18f77

Please sign in to comment.