Refactored code for resources in livecheck command

This commit is contained in:
Mohammad Zain Abbas 2022-08-07 18:14:24 +02:00
parent 656b18b13a
commit 6055f78218

View File

@ -286,9 +286,9 @@ module Homebrew
# Check current and latest resources (if "--resources" flag is given) # Check current and latest resources (if "--resources" flag is given)
# Only check current and latest versions if we have resources to check against # Only check current and latest versions if we have resources to check against
if check_resources && formula_or_cask.is_a?(Formula) && formula_or_cask.resources.present? if check_resources && formula_or_cask.is_a?(Formula) && formula_or_cask.resources.present?
current_resources = formula_or_cask.resources.map do |resource| # current_resources = formula_or_cask.resources.map do |resource|
{ name: resource.name, version: resource.version, livecheckable: resource.livecheckable? } # { name: resource.name, version: resource.version, livecheckable: resource.livecheckable? }
end # end
resource_version_info = formula_or_cask.resources.map do |resource| resource_version_info = formula_or_cask.resources.map do |resource|
resource_info = resource_version( resource_info = resource_version(
@ -301,9 +301,11 @@ module Homebrew
resource_info resource_info
end end
latest_resources = resource_version_info.map do |resource| # p "resource_version_info: #{resource_version_info}"
{ name: resource[:resource], version: resource[:version][:latest] }
end # latest_resources = resource_version_info.map do |resource|
# { name: resource[:resource], version: resource[:version][:latest] }
# end
end end
if latest.blank? if latest.blank?
@ -348,34 +350,36 @@ 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)
if check_resources && formula_or_cask.is_a?(Formula) && formula_or_cask.resources.present? if check_resources && formula_or_cask.is_a?(Formula) && formula_or_cask.resources.present?
resources_info = [] info[:resources] = resource_version_info
latest_resources_names = latest_resources.map { |r| r[:name] } # resources_info = []
current_resources.each do |resource| # latest_resources_names = latest_resources.map { |r| r[:name] }
current = resource[:version] # current_resources.each do |resource|
current_str = current.to_s # current = resource[:version]
latest = if latest_resources_names.include?(resource[:name].to_s) # current_str = current.to_s
res = latest_resources.find { |r| r[:name].to_s == resource[:name].to_s } # latest = if latest_resources_names.include?(resource[:name].to_s)
res[:version] # res = latest_resources.find { |r| r[:name].to_s == resource[:name].to_s }
else # res[:version]
current # else
end # current
latest_str = latest.to_s # end
# latest_str = latest.to_s
is_newer_than_upstream = current > latest # is_newer_than_upstream = current > latest
is_outdated = (current != latest) && !is_newer_than_upstream # is_outdated = (current != latest) && !is_newer_than_upstream
res_info = {} # res_info = {}
res_info[:resource] = resource[:name] # res_info[:resource] = resource[:name]
res_info[:version] = { # res_info[:version] = {
current: current_str, # current: current_str,
latest: latest_str, # latest: latest_str,
newer_than_upstream: is_newer_than_upstream, # newer_than_upstream: is_newer_than_upstream,
outdated: is_outdated, # outdated: is_outdated,
} # }
resources_info << res_info # resources_info << res_info
end # end
info[:resources] = resources_info # info[:resources] = resources_info
end end
next if newer_only && !info[:version][:outdated] next if newer_only && !info[:version][:outdated]
@ -391,7 +395,7 @@ module Homebrew
print_latest_version(info, verbose: verbose, ambiguous_cask: ambiguous_casks.include?(formula_or_cask), resource: false) print_latest_version(info, verbose: verbose, ambiguous_cask: ambiguous_casks.include?(formula_or_cask), resource: false)
if check_resources && formula_or_cask.resources.present? if check_resources && formula_or_cask.resources.present?
resources_info.each do |r_info| resource_version_info.each do |r_info|
print_latest_version( print_latest_version(
r_info, r_info,
verbose: verbose, verbose: verbose,
@ -672,174 +676,172 @@ module Homebrew
puts "Livecheckable?: #{has_livecheckable ? "Yes" : "No"}" puts "Livecheckable?: #{has_livecheckable ? "Yes" : "No"}"
end end
resource_version_info = { 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) livecheck = resource.livecheck
if has_livecheckable livecheck_url = has_livecheckable ? livecheck.url : resource.url
livecheck = resource.livecheck livecheck_regex = livecheck&.regex
livecheck_url = livecheck.url livecheck_strategy = livecheck&.strategy
livecheck_regex = livecheck.regex livecheck_strategy_block = livecheck&.strategy_block
livecheck_strategy = livecheck.strategy
livecheck_strategy_block = livecheck.strategy_block
livecheck_url_string = livecheck_url_to_string( livecheck_url_string = livecheck_url_to_string(
livecheck_url, livecheck_url,
resource, resource,
)
urls = [livecheck_url_string] if livecheck_url_string
urls ||= checkable_urls(resource)
checked_urls = []
urls.each_with_index do |original_url, i|
# Only preprocess the URL when it's appropriate
url = if STRATEGY_SYMBOLS_TO_SKIP_PREPROCESS_URL.include?(livecheck_strategy)
original_url
else
preprocess_url(original_url)
end
next if checked_urls.include?(url)
strategies = Strategy.from_url(
url,
livecheck_strategy: livecheck_strategy,
url_provided: livecheck_url.present?,
regex_provided: livecheck_regex.present?,
block_provided: livecheck_strategy_block.present?,
) )
urls = [livecheck_url_string] if livecheck_url_string strategy = Strategy.from_symbol(livecheck_strategy) || strategies.first
urls ||= checkable_urls(resource) strategy_name = livecheck_strategy_names[strategy]
checked_urls = [] if debug
puts
urls.each_with_index do |original_url, i| if livecheck_url.is_a?(Symbol)
# Only preprocess the URL when it's appropriate # This assumes the URL symbol will fit within the available space
url = if STRATEGY_SYMBOLS_TO_SKIP_PREPROCESS_URL.include?(livecheck_strategy) puts "URL (#{livecheck_url}):".ljust(18, " ") + original_url
original_url
else else
preprocess_url(original_url) puts "URL: #{original_url}"
end end
next if checked_urls.include?(url) puts "URL (processed): #{url}" if url != original_url
if strategies.present? && verbose
puts "Strategies: #{strategies.map { |s| livecheck_strategy_names[s] }.join(", ")}"
end
puts "Strategy: #{strategy.blank? ? "None" : strategy_name}"
puts "Regex: #{livecheck_regex.inspect}" if livecheck_regex.present?
end
strategies = Strategy.from_url( if livecheck_strategy.present?
url, if livecheck_url.blank? && strategy.method(:find_versions).parameters.include?([:keyreq, :url])
livecheck_strategy: livecheck_strategy, odebug "#{strategy_name} strategy requires a URL"
url_provided: livecheck_url.present?, next
regex_provided: livecheck_regex.present?, elsif livecheck_strategy != :page_match && strategies.exclude?(strategy)
block_provided: livecheck_strategy_block.present?, odebug "#{strategy_name} strategy does not apply to this URL"
) next
end
end
next if strategy.blank?
strategy = Strategy.from_symbol(livecheck_strategy) || strategies.first homebrew_curl = case strategy_name
strategy_name = livecheck_strategy_names[strategy] when "PageMatch", "HeaderMatch"
use_homebrew_curl?(resource, url)
end
puts "Homebrew curl?: Yes" if debug && homebrew_curl.present?
strategy_data = strategy.find_versions(
url: url, regex: livecheck_regex,
homebrew_curl: homebrew_curl, &livecheck_strategy_block
)
match_version_map = strategy_data[:matches]
regex = strategy_data[:regex]
messages = strategy_data[:messages]
checked_urls << url
if debug if messages.is_a?(Array) && match_version_map.blank?
puts puts messages unless json
if livecheck_url.is_a?(Symbol) next if i + 1 < urls.length
# This assumes the URL symbol will fit within the available space
puts "URL (#{livecheck_url}):".ljust(18, " ") + original_url return status_hash(resource, "error", messages, full_name: full_name, verbose: verbose)
else end
puts "URL: #{original_url}"
if debug
if strategy_data[:url].present? && strategy_data[:url] != url
puts "URL (strategy): #{strategy_data[:url]}"
end
puts "URL (final): #{strategy_data[:final_url]}" if strategy_data[:final_url].present?
if strategy_data[:regex].present? && strategy_data[:regex] != livecheck_regex
puts "Regex (strategy): #{strategy_data[:regex].inspect}"
end
puts "Cached?: Yes" if strategy_data[:cached] == true
end
match_version_map.delete_if do |_match, version|
next true if version.blank?
next false if has_livecheckable
UNSTABLE_VERSION_KEYWORDS.any? do |rejection|
version.to_s.include?(rejection)
end
end
next if match_version_map.blank?
if debug
puts
puts "Matched Versions:"
if verbose
match_version_map.each do |match, version|
puts "#{match} => #{version.inspect}"
end end
puts "URL (processed): #{url}" if url != original_url else
if strategies.present? && verbose puts match_version_map.values.join(", ")
puts "Strategies: #{strategies.map { |s| livecheck_strategy_names[s] }.join(", ")}"
end
puts "Strategy: #{strategy.blank? ? "None" : strategy_name}"
puts "Regex: #{livecheck_regex.inspect}" if livecheck_regex.present?
end end
end
if livecheck_strategy.present? res_current = resource.version
if livecheck_url.blank? && strategy.method(:find_versions).parameters.include?([:keyreq, :url]) res_latest = Version.new(match_version_map.values.max_by do |v| LivecheckVersion.create(resource, v) end)
odebug "#{strategy_name} strategy requires a URL"
next
elsif livecheck_strategy != :page_match && strategies.exclude?(strategy)
odebug "#{strategy_name} strategy does not apply to this URL"
next
end
end
next if strategy.blank?
homebrew_curl = case strategy_name is_newer_than_upstream = res_current > res_latest
when "PageMatch", "HeaderMatch" is_outdated = (res_current != res_latest) && !is_newer_than_upstream
use_homebrew_curl?(resource, url)
end
puts "Homebrew curl?: Yes" if debug && homebrew_curl.present?
strategy_data = strategy.find_versions(
url: url, regex: livecheck_regex,
homebrew_curl: homebrew_curl, &livecheck_strategy_block
)
match_version_map = strategy_data[:matches]
regex = strategy_data[:regex]
messages = strategy_data[:messages]
checked_urls << url
if messages.is_a?(Array) && match_version_map.blank? resource_version_info = {
puts messages unless json resource: resource_name(resource, full_name: full_name),
next if i + 1 < urls.length version: {
current: res_current.to_s,
return status_hash(resource, "error", messages, full_name: full_name, verbose: verbose) latest: res_latest.to_s,
end newer_than_upstream: is_newer_than_upstream,
outdated: is_outdated,
if debug },
if strategy_data[:url].present? && strategy_data[:url] != url }
puts "URL (strategy): #{strategy_data[:url]}"
end
puts "URL (final): #{strategy_data[:final_url]}" if strategy_data[:final_url].present?
if strategy_data[:regex].present? && strategy_data[:regex] != livecheck_regex
puts "Regex (strategy): #{strategy_data[:regex].inspect}"
end
puts "Cached?: Yes" if strategy_data[:cached] == true
end
match_version_map.delete_if do |_match, version|
next true if version.blank?
next false if has_livecheckable
UNSTABLE_VERSION_KEYWORDS.any? do |rejection|
version.to_s.include?(rejection)
end
end
next if match_version_map.blank?
if debug
puts
puts "Matched Versions:"
if verbose
match_version_map.each do |match, version|
puts "#{match} => #{version.inspect}"
end
else
puts match_version_map.values.join(", ")
end
end
resource_version_info[:version][:latest] = Version.new(match_version_map.values.max_by do |v|
LivecheckVersion.create(resource, v)
end)
next unless json
if json && verbose
resource_version_info[:meta] = { livecheckable: has_livecheckable ? "Yes" : "No" } resource_version_info[:meta] = { livecheckable: has_livecheckable ? "Yes" : "No" }
if has_livecheckable if has_livecheckable
resource_version_info[:meta][:livecheck] = { url: {} } res_livecheck = { url: {} }
if livecheck_url.is_a?(Symbol) && livecheck_url_string if livecheck_url.is_a?(Symbol) && livecheck_url_string
resource_version_info[:meta][:livecheck][:url][:symbol] = res_livecheck[:url][:symbol] =
livecheck_url livecheck_url
end end
if strategy_data[:url].present? && strategy_data[:url] != url if strategy_data[:url].present? && strategy_data[:url] != url
resource_version_info[:meta][:livecheck][:url][:strategy] = strategy_data[:url] res_livecheck[:url][:strategy] = strategy_data[:url]
end end
if strategy_data[:final_url] if strategy_data[:final_url]
resource_version_info[:meta][:livecheck][:url][:final] = res_livecheck[:url][:final] =
strategy_data[:final_url] strategy_data[:final_url]
end end
resource_version_info[:meta][:livecheck][:url][:homebrew_curl] = homebrew_curl if homebrew_curl.present? res_livecheck[:url][:homebrew_curl] = homebrew_curl if homebrew_curl.present?
resource_version_info[:meta][:livecheck][:strategy] = strategy.present? ? strategy_name : nil res_livecheck[:strategy] = strategy.present? ? strategy_name : nil
if strategies.present? if strategies.present?
resource_version_info[:meta][:livecheck][:strategies] = strategies.map do |s| res_livecheck[:strategies] = strategies.map do |s|
livecheck_strategy_names[s] livecheck_strategy_names[s]
end end
end end
resource_version_info[:meta][:livecheck][:regex] = regex.inspect if regex.present? res_livecheck[:regex] = regex.inspect if regex.present?
resource_version_info[:meta][:livecheck][:cached] = true if strategy_data[:cached] == true res_livecheck[:cached] = true if strategy_data[:cached] == true
resource_version_info[:meta][:livecheck] = res_livecheck
end end
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
end end
else
# If there's no livecheck block in resource
resource_version_info[:version][:latest] = resource.version
resource_version_info[:meta] = { url: resource.url.to_s } if json && verbose
end end
resource_version_info[:version][:newer_than_upstream] =
resource_version_info[:version][:current] > resource_version_info[:version][:latest]
resource_version_info[:version][:outdated] =
resource_version_info[:version][:current] < resource_version_info[:version][:latest]
resource_version_info resource_version_info
end end