Refactored run_checks and resource_version methods for livecheck command

This commit is contained in:
Mohammad Zain Abbas 2022-07-27 18:25:12 +02:00
parent a8c1c0c841
commit 3dc1b9c231

View File

@ -254,9 +254,6 @@ module Homebrew
formula&.head&.downloader&.shutup! formula&.head&.downloader&.shutup!
# Check resources if "--resources" flag was given
# Use the `stable` version for comparison except for installed # Use the `stable` version for comparison except for installed
# head-only formulae. A formula with `stable` and `head` that's # head-only formulae. A formula with `stable` and `head` that's
# installed using `--head` will still use the `stable` version for # installed using `--head` will still use the `stable` version for
@ -291,7 +288,7 @@ module Homebrew
has_resources = formula_or_cask.resources.any? has_resources = formula_or_cask.resources.any?
# Only check current and latest versions of resources if we have resources to check against # Only check current and latest versions if we have resources to check against
if has_resources if has_resources
current_resources = formula_or_cask.resources.map { |resource| { name: resource.name, version: resource.version, livecheckable: resource.livecheckable? } } current_resources = formula_or_cask.resources.map { |resource| { name: resource.name, version: resource.version, livecheckable: resource.livecheckable? } }
@ -304,7 +301,7 @@ module Homebrew
debug: debug debug: debug
) )
latest_resources = resource_version_info.map { |resource| { name: resource[:name], version: resource[:latest] } } latest_resources = resource_version_info.map { |resource| { name: resource[:resource], version: resource[:version][:latest] } }
else else
# In case we don't have any resources for that Formula/Cask # In case we don't have any resources for that Formula/Cask
@ -356,6 +353,8 @@ module Homebrew
info[:meta][:head_only] = true if formula&.head_only? info[:meta][:head_only] = true if formula&.head_only?
info[:meta].merge!(version_info[:meta]) if version_info.present? && version_info.key?(:meta) info[:meta].merge!(version_info[:meta]) if version_info.present? && version_info.key?(:meta)
info[:resources] = resource_version_info if check_resources
next if newer_only && !info[:version][:outdated] next if newer_only && !info[:version][:outdated]
has_a_newer_upstream_version ||= true has_a_newer_upstream_version ||= true
@ -366,6 +365,13 @@ module Homebrew
next info next info
end end
if check_resources && has_resources && debug
puts <<~EOS
----------
EOS
end
print_latest_version(info, verbose: verbose, ambiguous_cask: ambiguous_casks.include?(formula_or_cask)) print_latest_version(info, verbose: verbose, ambiguous_cask: ambiguous_casks.include?(formula_or_cask))
if check_resources && has_resources if check_resources && has_resources
@ -396,11 +402,6 @@ module Homebrew
} }
resources_info << info resources_info << info
end end
puts <<~EOS
----------
EOS
print_latest_resource_version(resources_info, verbose: verbose, ambiguous_cask: ambiguous_casks.include?(formula_or_cask)) print_latest_resource_version(resources_info, verbose: verbose, ambiguous_cask: ambiguous_casks.include?(formula_or_cask))
end end
@ -474,28 +475,31 @@ module Homebrew
sig { sig {
params( params(
formula_or_cask: T.any(Formula, Cask::Cask), package_or_resource: T.any(Formula, Cask::Cask, Resource),
status_str: String, status_str: String,
messages: T.nilable(T::Array[String]), messages: T.nilable(T::Array[String]),
full_name: T::Boolean, full_name: T::Boolean,
verbose: T::Boolean, verbose: T::Boolean,
).returns(Hash) ).returns(Hash)
} }
def status_hash(formula_or_cask, status_str, messages = nil, full_name: false, verbose: false) def status_hash(package_or_resource, status_str, messages = nil, full_name: false, verbose: false)
formula = formula_or_cask if formula_or_cask.is_a?(Formula) formula = package_or_resource if package_or_resource.is_a?(Formula)
cask = formula_or_cask if formula_or_cask.is_a?(Cask::Cask) cask = package_or_resource if package_or_resource.is_a?(Cask::Cask)
resource = package_or_resource if package_or_resource.is_a?(Resource)
status_hash = {} status_hash = {}
if formula if formula
status_hash[:formula] = formula_name(formula, full_name: full_name) status_hash[:formula] = formula_name(formula, full_name: full_name)
elsif cask elsif cask
status_hash[:cask] = cask_name(formula_or_cask, full_name: full_name) status_hash[:cask] = cask_name(package_or_resource, full_name: full_name)
elsif resource
status_hash[:resource] = resource_name(package_or_resource, full_name: full_name)
end end
status_hash[:status] = status_str status_hash[:status] = status_str
status_hash[:messages] = messages if messages.is_a?(Array) status_hash[:messages] = messages if messages.is_a?(Array)
status_hash[:meta] = { status_hash[:meta] = {
livecheckable: formula_or_cask.livecheckable?, livecheckable: package_or_resource.livecheckable?,
} }
status_hash[:meta][:head_only] = true if formula&.head_only? status_hash[:meta][:head_only] = true if formula&.head_only?
@ -521,7 +525,7 @@ module Homebrew
info[:version][:latest] info[:version][:latest]
end end
puts "#{resource_s}: #{current_s} ==> #{latest_s}" puts "-- #{resource_s}: #{current_s} ==> #{latest_s}"
end end
end end
@ -668,10 +672,8 @@ module Homebrew
homebrew_curl_root_domains.include?(url_root_domain) homebrew_curl_root_domains.include?(url_root_domain)
end end
#================================================================================== # Identifies the latest version of the resources in a given Formulae/Casks and returns an Array of Hash containing
# the version information for all the resources. Returns an Array with nil value if a latest version couldn't be found for a given resource.
# Identifies the latest version of the resources and returns a Hash containing
# the version information. Returns nil if a latest version couldn't be found.
sig { sig {
params( params(
formula_or_cask: T.any(Formula, Cask::Cask), formula_or_cask: T.any(Formula, Cask::Cask),
@ -702,7 +704,14 @@ module Homebrew
odebug "Livecheckable?: #{has_livecheckable ? "Yes" : "No"}" odebug "Livecheckable?: #{has_livecheckable ? "Yes" : "No"}"
end end
# For now, only check resources with livecheck block resource_version_info = {
resource: resource_name(resource, full_name: full_name),
version: {
current: resource.version,
},
}
# Check resources with livecheck block (will be updated in the future)
if has_livecheckable if has_livecheckable
livecheck = resource.livecheck livecheck = resource.livecheck
livecheck_url = livecheck.url livecheck_url = livecheck.url
@ -781,9 +790,6 @@ module Homebrew
) )
match_version_map = strategy_data[:matches] match_version_map = strategy_data[:matches]
# if debug
# odebug "match_version_map: #{match_version_map}"
# end
regex = strategy_data[:regex] regex = strategy_data[:regex]
messages = strategy_data[:messages] messages = strategy_data[:messages]
checked_urls << url checked_urls << url
@ -792,7 +798,7 @@ module Homebrew
puts messages unless json puts messages unless json
next if i + 1 < urls.length next if i + 1 < urls.length
return status_hash(formula_or_cask, "error", messages, full_name: full_name, verbose: verbose) return status_hash(resource, "error", messages, full_name: full_name, verbose: verbose)
end end
if debug if debug
@ -830,38 +836,45 @@ module Homebrew
end end
end end
resource_version_info = { resource_version_info[:version][:latest] = Version.new(match_version_map.values.max_by { |v| LivecheckVersion.create(resource, v) })
name: resource_name(resource, full_name: full_name),
latest: Version.new(match_version_map.values.max_by { |v| LivecheckVersion.create(resource, v) }),
}
if json && verbose if json && verbose
resource_version_info[:meta] = {} resource_version_info[:meta] = {}
resource_version_info[:meta][:name] = resource_name(resource, full_name: full_name) if resource
resource_version_info[:meta][:livecheckable] = has_livecheckable ? "Yes" : "No" resource_version_info[:meta][:livecheckable] = has_livecheckable ? "Yes" : "No"
if has_livecheckable
resource_version_info[:meta][:livecheck] = {}
resource_version_info[:meta][:livecheck][:url] = {}
resource_version_info[:meta][:livecheck][:url][:symbol] = livecheck_url if livecheck_url.is_a?(Symbol) && livecheck_url_string
if strategy_data[:url].present? && strategy_data[:url] != url
resource_version_info[:meta][:livecheck][:url][:strategy] = strategy_data[:url]
end
resource_version_info[:meta][:livecheck][:url][:final] = strategy_data[:final_url] if strategy_data[:final_url]
resource_version_info[:meta][:livecheck][:url][:homebrew_curl] = homebrew_curl if homebrew_curl.present?
resource_version_info[:meta][:livecheck][:strategy] = strategy.present? ? strategy_name : nil
resource_version_info[:meta][:livecheck][:strategies] = strategies.map { |s| livecheck_strategy_names[s] } if strategies.present?
resource_version_info[:meta][:livecheck][:regex] = regex.inspect if regex.present?
resource_version_info[:meta][:livecheck][:cached] = true if strategy_data[:cached] == true
end
resource_version_info[:meta][:url] = {} resource_version_info[:meta][:url] = {}
resource_version_info[:meta][:url][:symbol] = livecheck_url if livecheck_url.is_a?(Symbol) && livecheck_url_string
resource_version_info[:meta][:url][:original] = original_url resource_version_info[:meta][:url][:original] = original_url
resource_version_info[:meta][:url][:processed] = url if url != original_url resource_version_info[:meta][:url][:processed] = url if url != original_url
if strategy_data[:url].present? && strategy_data[:url] != url
resource_version_info[:meta][:url][:strategy] = strategy_data[:url]
end end
resource_version_info[:meta][:url][:final] = strategy_data[:final_url] if strategy_data[:final_url] end
resource_version_info[:meta][:url][:homebrew_curl] = homebrew_curl if homebrew_curl.present? resources_version << resource_version_info
resource_version_info[:meta][:strategy] = strategy.present? ? strategy_name : nil else
resource_version_info[:meta][:strategies] = strategies.map { |s| livecheck_strategy_names[s] } if strategies.present? # If there's no livecheck block in resource
resource_version_info[:meta][:regex] = regex.inspect if regex.present? resource_version_info[:version][:latest] = resource.version
resource_version_info[:meta][:cached] = true if strategy_data[:cached] == true if json && verbose
resource_version_info[:meta] = {}
resource_version_info[:meta][:url] = resource.url.to_s
end end
resources_version << resource_version_info resources_version << resource_version_info
end end
end end
end
resources_version resources_version
end end
#==================================================================================
# Identifies the latest version of the formula and returns a Hash containing # Identifies the latest version of the formula and returns a Hash containing
# the version information. Returns nil if a latest version couldn't be found. # the version information. Returns nil if a latest version couldn't be found.
# rubocop:disable Metrics/CyclomaticComplexity # rubocop:disable Metrics/CyclomaticComplexity
@ -984,10 +997,7 @@ module Homebrew
&livecheck_strategy_block &livecheck_strategy_block
) )
# p strategy_data
match_version_map = strategy_data[:matches] match_version_map = strategy_data[:matches]
# p "match_version_map: #{match_version_map}"
regex = strategy_data[:regex] regex = strategy_data[:regex]
messages = strategy_data[:messages] messages = strategy_data[:messages]
checked_urls << url checked_urls << url