Support block in PageMatch strategy.

This commit is contained in:
Markus Reiter 2020-12-14 02:07:07 +01:00 committed by Sam Ford
parent 1857e0ebfa
commit 7555556be8
No known key found for this signature in database
GPG Key ID: 95209E46C7FFDEFE

View File

@ -46,21 +46,22 @@ module Homebrew
# @param regex [Regexp] a regex used for matching versions in the # @param regex [Regexp] a regex used for matching versions in the
# content # content
# @return [Array] # @return [Array]
def self.page_matches(url, regex) def self.page_matches(url, regex, &block)
page = Strategy.page_contents(url) page = Strategy.page_contents(url)
matches = page.scan(regex) if block
data = { page: page }
regex_names = regex.names.map(&:to_sym) case (value = block.call(data))
when String
if regex_names.count > 1 return [value]
matches.map do |match| when Array
match_data = regex_names.zip(match).to_h return value
regex_names.sort.map { |name| match_data[name] }.join(",") else
end.uniq raise TypeError, "Return value of `strategy :page_match` block must be a string or array."
else end
matches.map(&:first).uniq
end end
page.scan(regex).map(&:first).uniq
end end
# Checks the content at the URL for new versions, using the provided # Checks the content at the URL for new versions, using the provided
@ -69,10 +70,10 @@ module Homebrew
# @param url [String] the URL of the content to check # @param url [String] the URL of the content to check
# @param regex [Regexp] a regex used for matching versions in content # @param regex [Regexp] a regex used for matching versions in content
# @return [Hash] # @return [Hash]
def self.find_versions(url, regex) def self.find_versions(url, regex, &block)
match_data = { matches: {}, regex: regex, url: url } match_data = { matches: {}, regex: regex, url: url }
page_matches(url, regex).each do |match| page_matches(url, regex, &block).each do |match|
match_data[:matches][match] = Version.new(match) match_data[:matches][match] = Version.new(match)
end end