Xorg: Use PageMatch#find_versions
This commit is contained in:
parent
5818b28479
commit
5c9f5b9506
@ -71,12 +71,29 @@ module Homebrew
|
|||||||
|
|
||||||
# Checks the content at the URL for new versions, using the provided
|
# Checks the content at the URL for new versions, using the provided
|
||||||
# regex for matching.
|
# regex for matching.
|
||||||
sig { params(url: String, regex: T.nilable(Regexp)).returns(T::Hash[Symbol, T.untyped]) }
|
#
|
||||||
def self.find_versions(url, regex, &block)
|
# @param url [String] the URL of the content to check
|
||||||
|
# @param regex [Regexp] a regex used for matching versions in content
|
||||||
|
# @param provided_content [String] page content to use in place of
|
||||||
|
# fetching via Strategy#page_content
|
||||||
|
# @return [Hash]
|
||||||
|
sig do
|
||||||
|
params(
|
||||||
|
url: String,
|
||||||
|
regex: T.nilable(Regexp),
|
||||||
|
provided_content: T.nilable(String),
|
||||||
|
block: T.nilable(T.proc.params(arg0: String).returns(T.any(T::Array[String], String))),
|
||||||
|
).returns(T::Hash[Symbol, T.untyped])
|
||||||
|
end
|
||||||
|
def self.find_versions(url, regex, provided_content = nil, &block)
|
||||||
match_data = { matches: {}, regex: regex, url: url }
|
match_data = { matches: {}, regex: regex, url: url }
|
||||||
|
|
||||||
match_data.merge!(Strategy.page_content(url))
|
content = if provided_content.present?
|
||||||
content = match_data.delete(:content)
|
provided_content
|
||||||
|
else
|
||||||
|
match_data.merge!(Strategy.page_content(url))
|
||||||
|
match_data.delete(:content)
|
||||||
|
end
|
||||||
return match_data if content.blank?
|
return match_data if content.blank?
|
||||||
|
|
||||||
page_matches(content, regex, &block).each do |match_text|
|
page_matches(content, regex, &block).each do |match_text|
|
||||||
|
|||||||
@ -1,8 +1,6 @@
|
|||||||
# typed: false
|
# typed: false
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "open-uri"
|
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
module Livecheck
|
module Livecheck
|
||||||
module Strategy
|
module Strategy
|
||||||
@ -74,7 +72,6 @@ module Homebrew
|
|||||||
# @return [Hash]
|
# @return [Hash]
|
||||||
def self.find_versions(url, regex)
|
def self.find_versions(url, regex)
|
||||||
file_name = File.basename(url)
|
file_name = File.basename(url)
|
||||||
|
|
||||||
/^(?<module_name>.+)-\d+/i =~ file_name
|
/^(?<module_name>.+)-\d+/i =~ file_name
|
||||||
|
|
||||||
# /pub/ URLs redirect to the same URL with /archive/, so we replace
|
# /pub/ URLs redirect to the same URL with /archive/, so we replace
|
||||||
@ -82,17 +79,15 @@ module Homebrew
|
|||||||
# the URL gives us the relevant directory listing page.
|
# the URL gives us the relevant directory listing page.
|
||||||
page_url = url.sub("x.org/pub/", "x.org/archive/").delete_suffix(file_name)
|
page_url = url.sub("x.org/pub/", "x.org/archive/").delete_suffix(file_name)
|
||||||
|
|
||||||
|
# Example regex: /href=.*?example[._-]v?(\d+(?:\.\d+)+)\.t/i
|
||||||
regex ||= /href=.*?#{Regexp.escape(module_name)}[._-]v?(\d+(?:\.\d+)+)\.t/i
|
regex ||= /href=.*?#{Regexp.escape(module_name)}[._-]v?(\d+(?:\.\d+)+)\.t/i
|
||||||
|
|
||||||
match_data = { matches: {}, regex: regex, url: page_url }
|
# Use the cached page content to avoid duplicate fetches
|
||||||
|
cached_content = @page_data[page_url]
|
||||||
|
match_data = Homebrew::Livecheck::Strategy::PageMatch.find_versions(page_url, regex, cached_content)
|
||||||
|
|
||||||
# Cache responses to avoid unnecessary duplicate fetches
|
# Cache any new page content
|
||||||
@page_data[page_url] = URI.parse(page_url).open.read unless @page_data.key?(page_url)
|
@page_data[page_url] = match_data[:content] if match_data[:content].present?
|
||||||
|
|
||||||
matches = @page_data[page_url].scan(regex)
|
|
||||||
matches.map(&:first).uniq.each do |match|
|
|
||||||
match_data[:matches][match] = Version.new(match)
|
|
||||||
end
|
|
||||||
|
|
||||||
match_data
|
match_data
|
||||||
end
|
end
|
||||||
|
|||||||
@ -34,7 +34,24 @@ describe Homebrew::Livecheck::Strategy::PageMatch do
|
|||||||
</html>
|
</html>
|
||||||
EOS
|
EOS
|
||||||
}
|
}
|
||||||
|
|
||||||
let(:page_content_matches) { ["2.6.0", "2.5.0", "2.4.0", "2.3.0", "2.2.0", "2.1.0", "2.0.0", "1.9.0"] }
|
let(:page_content_matches) { ["2.6.0", "2.5.0", "2.4.0", "2.3.0", "2.2.0", "2.1.0", "2.0.0", "1.9.0"] }
|
||||||
|
let(:find_versions_return_hash) {
|
||||||
|
{
|
||||||
|
matches: {
|
||||||
|
"2.6.0" => Version.new("2.6.0"),
|
||||||
|
"2.5.0" => Version.new("2.5.0"),
|
||||||
|
"2.4.0" => Version.new("2.4.0"),
|
||||||
|
"2.3.0" => Version.new("2.3.0"),
|
||||||
|
"2.2.0" => Version.new("2.2.0"),
|
||||||
|
"2.1.0" => Version.new("2.1.0"),
|
||||||
|
"2.0.0" => Version.new("2.0.0"),
|
||||||
|
"1.9.0" => Version.new("1.9.0"),
|
||||||
|
},
|
||||||
|
regex: regex,
|
||||||
|
url: url,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
describe "::match?" do
|
describe "::match?" do
|
||||||
it "returns true for any URL" do
|
it "returns true for any URL" do
|
||||||
@ -52,4 +69,10 @@ describe Homebrew::Livecheck::Strategy::PageMatch do
|
|||||||
.to eq(page_content_matches)
|
.to eq(page_content_matches)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "::find_versions?" do
|
||||||
|
it "finds versions in provided_content" do
|
||||||
|
expect(page_match.find_versions(url, regex, page_content)).to eq(find_versions_return_hash)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user