Skip to content

Commit b9eff75

Browse files
authored
Merge pull request #19351 from Homebrew/livecheck/refactor-livecheck_strategy_names
livecheck: refactor livecheck_strategy_names
2 parents b464818 + efeff90 commit b9eff75

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

Library/Homebrew/livecheck/livecheck.rb

+14-19
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,10 @@ module Livecheck
2828
].freeze, T::Array[String])
2929
private_constant :UNSTABLE_VERSION_KEYWORDS
3030

31-
sig { returns(T::Hash[T::Class[T.anything], String]) }
32-
private_class_method def self.livecheck_strategy_names
33-
return T.must(@livecheck_strategy_names) if defined?(@livecheck_strategy_names)
34-
35-
# Cache demodulized strategy names, to avoid repeating this work
36-
@livecheck_strategy_names = T.let({}, T.nilable(T::Hash[T::Class[T.anything], String]))
37-
Strategy.constants.sort.each do |const_symbol|
38-
constant = Strategy.const_get(const_symbol)
39-
next unless constant.is_a?(Class)
40-
41-
T.must(@livecheck_strategy_names)[constant] = Utils.demodulize(T.must(constant.name))
42-
end
43-
T.must(@livecheck_strategy_names).freeze
31+
sig { params(strategy_class: T::Class[T.anything]).returns(String) }
32+
private_class_method def self.livecheck_strategy_names(strategy_class)
33+
@livecheck_strategy_names ||= T.let({}, T.nilable(T::Hash[T::Class[T.anything], String]))
34+
@livecheck_strategy_names[strategy_class] ||= Utils.demodulize(T.must(strategy_class.name))
4435
end
4536

4637
# Uses `formulae_and_casks_to_check` to identify taps in use other than
@@ -668,7 +659,9 @@ def self.latest_version(
668659
block_provided: livecheck_strategy_block.present?,
669660
)
670661
strategy = Strategy.from_symbol(livecheck_strategy) || strategies.first
671-
strategy_name = livecheck_strategy_names[strategy]
662+
next unless strategy
663+
664+
strategy_name = livecheck_strategy_names(strategy)
672665

673666
if strategy.respond_to?(:preprocess_url)
674667
url = strategy.preprocess_url(url)
@@ -686,7 +679,7 @@ def self.latest_version(
686679
puts "URL Options: #{livecheck_url_options}" if livecheck_url_options.present?
687680
puts "URL (processed): #{url}" if url != original_url
688681
if strategies.present? && verbose
689-
puts "Strategies: #{strategies.map { |s| livecheck_strategy_names[s] }.join(", ")}"
682+
puts "Strategies: #{strategies.map { |s| livecheck_strategy_names(s) }.join(", ")}"
690683
end
691684
puts "Strategy: #{strategy_name}" if strategy.present?
692685
puts "Regex: #{livecheck_regex.inspect}" if livecheck_regex.present?
@@ -823,7 +816,7 @@ def self.latest_version(
823816
version_info[:meta][:url][:homebrew_curl] = homebrew_curl if homebrew_curl.present?
824817
end
825818
version_info[:meta][:strategy] = strategy_name if strategy.present?
826-
version_info[:meta][:strategies] = strategies.map { |s| livecheck_strategy_names[s] } if strategies.present?
819+
version_info[:meta][:strategies] = strategies.map { |s| livecheck_strategy_names(s) } if strategies.present?
827820
version_info[:meta][:regex] = regex.inspect if regex.present?
828821
version_info[:meta][:cached] = true if strategy_data[:cached] == true
829822
version_info[:meta][:throttle] = livecheck_throttle if livecheck_throttle
@@ -892,7 +885,9 @@ def self.resource_version(
892885
block_provided: livecheck_strategy_block.present?,
893886
)
894887
strategy = Strategy.from_symbol(livecheck_strategy) || strategies.first
895-
strategy_name = livecheck_strategy_names[strategy]
888+
next unless strategy
889+
890+
strategy_name = livecheck_strategy_names(strategy)
896891

897892
if strategy.respond_to?(:preprocess_url)
898893
url = strategy.preprocess_url(url)
@@ -910,7 +905,7 @@ def self.resource_version(
910905
puts "URL Options: #{livecheck_url_options}" if livecheck_url_options.present?
911906
puts "URL (processed): #{url}" if url != original_url
912907
if strategies.present? && verbose
913-
puts "Strategies: #{strategies.map { |s| livecheck_strategy_names[s] }.join(", ")}"
908+
puts "Strategies: #{strategies.map { |s| livecheck_strategy_names(s) }.join(", ")}"
914909
end
915910
puts "Strategy: #{strategy_name}" if strategy.present?
916911
puts "Regex: #{livecheck_regex.inspect}" if livecheck_regex.present?
@@ -1032,7 +1027,7 @@ def self.resource_version(
10321027
end
10331028
resource_version_info[:meta][:strategy] = strategy_name if strategy.present?
10341029
if strategies.present?
1035-
resource_version_info[:meta][:strategies] = strategies.map { |s| livecheck_strategy_names[s] }
1030+
resource_version_info[:meta][:strategies] = strategies.map { |s| livecheck_strategy_names(s) }
10361031
end
10371032
resource_version_info[:meta][:regex] = regex.inspect if regex.present?
10381033
resource_version_info[:meta][:cached] = true if cached == true

Library/Homebrew/test/livecheck/livecheck_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@
7676
RUBY
7777
end
7878

79+
describe "::livecheck_strategy_names" do
80+
context "when provided with a strategy class" do
81+
it "returns demodulized class name" do
82+
# We run this twice with the same argument to exercise the caching logic
83+
expect(livecheck.send(:livecheck_strategy_names, Homebrew::Livecheck::Strategy::PageMatch)).to eq("PageMatch")
84+
expect(livecheck.send(:livecheck_strategy_names, Homebrew::Livecheck::Strategy::PageMatch)).to eq("PageMatch")
85+
end
86+
end
87+
end
88+
7989
describe "::resolve_livecheck_reference" do
8090
context "when a formula/cask has a `livecheck` block without formula/cask methods" do
8191
it "returns [nil, []]" do

0 commit comments

Comments
 (0)