livecheck: refactor livecheck_strategy_names

This refactors the `livecheck_strategy_names` method to align with
Doug's `livecheck_find_versions_parameters` implementation.
This commit is contained in:
Sam Ford 2025-02-19 16:22:25 -05:00
parent ed13d33aef
commit efeff905eb
No known key found for this signature in database
GPG Key ID: 7AF5CBEE1DD6F76D
2 changed files with 24 additions and 19 deletions

View File

@ -28,19 +28,10 @@ module Homebrew
].freeze, T::Array[String])
private_constant :UNSTABLE_VERSION_KEYWORDS
sig { returns(T::Hash[T::Class[T.anything], String]) }
private_class_method def self.livecheck_strategy_names
return T.must(@livecheck_strategy_names) if defined?(@livecheck_strategy_names)
# Cache demodulized strategy names, to avoid repeating this work
@livecheck_strategy_names = T.let({}, T.nilable(T::Hash[T::Class[T.anything], String]))
Strategy.constants.sort.each do |const_symbol|
constant = Strategy.const_get(const_symbol)
next unless constant.is_a?(Class)
T.must(@livecheck_strategy_names)[constant] = Utils.demodulize(T.must(constant.name))
end
T.must(@livecheck_strategy_names).freeze
sig { params(strategy_class: T::Class[T.anything]).returns(String) }
private_class_method def self.livecheck_strategy_names(strategy_class)
@livecheck_strategy_names ||= T.let({}, T.nilable(T::Hash[T::Class[T.anything], String]))
@livecheck_strategy_names[strategy_class] ||= Utils.demodulize(T.must(strategy_class.name))
end
# Uses `formulae_and_casks_to_check` to identify taps in use other than
@ -668,7 +659,9 @@ module Homebrew
block_provided: livecheck_strategy_block.present?,
)
strategy = Strategy.from_symbol(livecheck_strategy) || strategies.first
strategy_name = livecheck_strategy_names[strategy]
next unless strategy
strategy_name = livecheck_strategy_names(strategy)
if strategy.respond_to?(:preprocess_url)
url = strategy.preprocess_url(url)
@ -686,7 +679,7 @@ module Homebrew
puts "URL Options: #{livecheck_url_options}" if livecheck_url_options.present?
puts "URL (processed): #{url}" if url != original_url
if strategies.present? && verbose
puts "Strategies: #{strategies.map { |s| livecheck_strategy_names[s] }.join(", ")}"
puts "Strategies: #{strategies.map { |s| livecheck_strategy_names(s) }.join(", ")}"
end
puts "Strategy: #{strategy_name}" if strategy.present?
puts "Regex: #{livecheck_regex.inspect}" if livecheck_regex.present?
@ -823,7 +816,7 @@ module Homebrew
version_info[:meta][:url][:homebrew_curl] = homebrew_curl if homebrew_curl.present?
end
version_info[:meta][:strategy] = strategy_name if strategy.present?
version_info[:meta][:strategies] = strategies.map { |s| livecheck_strategy_names[s] } if strategies.present?
version_info[:meta][:strategies] = strategies.map { |s| livecheck_strategy_names(s) } if strategies.present?
version_info[:meta][:regex] = regex.inspect if regex.present?
version_info[:meta][:cached] = true if strategy_data[:cached] == true
version_info[:meta][:throttle] = livecheck_throttle if livecheck_throttle
@ -892,7 +885,9 @@ module Homebrew
block_provided: livecheck_strategy_block.present?,
)
strategy = Strategy.from_symbol(livecheck_strategy) || strategies.first
strategy_name = livecheck_strategy_names[strategy]
next unless strategy
strategy_name = livecheck_strategy_names(strategy)
if strategy.respond_to?(:preprocess_url)
url = strategy.preprocess_url(url)
@ -910,7 +905,7 @@ module Homebrew
puts "URL Options: #{livecheck_url_options}" if livecheck_url_options.present?
puts "URL (processed): #{url}" if url != original_url
if strategies.present? && verbose
puts "Strategies: #{strategies.map { |s| livecheck_strategy_names[s] }.join(", ")}"
puts "Strategies: #{strategies.map { |s| livecheck_strategy_names(s) }.join(", ")}"
end
puts "Strategy: #{strategy_name}" if strategy.present?
puts "Regex: #{livecheck_regex.inspect}" if livecheck_regex.present?
@ -1032,7 +1027,7 @@ module Homebrew
end
resource_version_info[:meta][:strategy] = strategy_name if strategy.present?
if strategies.present?
resource_version_info[:meta][:strategies] = strategies.map { |s| livecheck_strategy_names[s] }
resource_version_info[:meta][:strategies] = strategies.map { |s| livecheck_strategy_names(s) }
end
resource_version_info[:meta][:regex] = regex.inspect if regex.present?
resource_version_info[:meta][:cached] = true if cached == true

View File

@ -76,6 +76,16 @@ RSpec.describe Homebrew::Livecheck do
RUBY
end
describe "::livecheck_strategy_names" do
context "when provided with a strategy class" do
it "returns demodulized class name" do
# We run this twice with the same argument to exercise the caching logic
expect(livecheck.send(:livecheck_strategy_names, Homebrew::Livecheck::Strategy::PageMatch)).to eq("PageMatch")
expect(livecheck.send(:livecheck_strategy_names, Homebrew::Livecheck::Strategy::PageMatch)).to eq("PageMatch")
end
end
end
describe "::resolve_livecheck_reference" do
context "when a formula/cask has a `livecheck` block without formula/cask methods" do
it "returns [nil, []]" do