Merge pull request #10113 from samford/xorg-use-pagematch
Xorg: Use PageMatch#find_versions
This commit is contained in:
		
						commit
						8ac02c9b09
					
				@ -524,7 +524,7 @@ module Homebrew
 | 
			
		||||
 | 
			
		||||
        if debug
 | 
			
		||||
          puts "URL (strategy):   #{strategy_data[:url]}" if strategy_data[:url] != url
 | 
			
		||||
          puts "URL (final):   #{strategy_data[:final_url]}" if strategy_data[:final_url]
 | 
			
		||||
          puts "URL (final):      #{strategy_data[:final_url]}" if strategy_data[:final_url]
 | 
			
		||||
          puts "Regex (strategy): #{strategy_data[:regex].inspect}" if strategy_data[:regex] != livecheck_regex
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -59,7 +59,7 @@ module Homebrew
 | 
			
		||||
          # * `/href=["']?example-v?(\d+(?:\.\d+)+)-bin\.zip/i`
 | 
			
		||||
          regex ||= /href=["']?#{Regexp.escape(prefix)}v?(\d+(?:\.\d+)+)#{Regexp.escape(suffix)}/i
 | 
			
		||||
 | 
			
		||||
          Homebrew::Livecheck::Strategy::PageMatch.find_versions(page_url, regex)
 | 
			
		||||
          PageMatch.find_versions(page_url, regex)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@ -71,7 +71,7 @@ module Homebrew
 | 
			
		||||
          # * `/href=.*?example-v?(\d+(?:\.\d+)+)\.t/i`
 | 
			
		||||
          regex ||= /href=.*?#{Regexp.escape(match[:prefix])}v?(\d+(?:\.\d+)+)#{Regexp.escape(suffix)}/i
 | 
			
		||||
 | 
			
		||||
          Homebrew::Livecheck::Strategy::PageMatch.find_versions(page_url, regex)
 | 
			
		||||
          PageMatch.find_versions(page_url, regex)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@ -54,7 +54,7 @@ module Homebrew
 | 
			
		||||
          # Example regex: `/href=.*?Brew[._-]v?(\d+(?:\.\d+)*)\.t/i`
 | 
			
		||||
          regex ||= /href=.*?#{prefix}[._-]v?(\d+(?:\.\d+)*)#{Regexp.escape(suffix)}/i
 | 
			
		||||
 | 
			
		||||
          Homebrew::Livecheck::Strategy::PageMatch.find_versions(page_url, regex)
 | 
			
		||||
          PageMatch.find_versions(page_url, regex)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@ -65,7 +65,7 @@ module Homebrew
 | 
			
		||||
          # The default regex is the same for all URLs using this strategy
 | 
			
		||||
          regex ||= %r{href=.*?/tag/v?(\d+(?:\.\d+)+)["' >]}i
 | 
			
		||||
 | 
			
		||||
          Homebrew::Livecheck::Strategy::PageMatch.find_versions(page_url, regex)
 | 
			
		||||
          PageMatch.find_versions(page_url, regex)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@ -53,7 +53,7 @@ module Homebrew
 | 
			
		||||
          # Example regex: `/example-(\d+\.([0-8]\d*?)?[02468](?:\.\d+)*?)\.t/i`
 | 
			
		||||
          regex ||= /#{Regexp.escape(package_name)}-(\d+\.([0-8]\d*?)?[02468](?:\.\d+)*?)\.t/i
 | 
			
		||||
 | 
			
		||||
          Homebrew::Livecheck::Strategy::PageMatch.find_versions(page_url, regex)
 | 
			
		||||
          PageMatch.find_versions(page_url, regex)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@ -89,7 +89,7 @@ module Homebrew
 | 
			
		||||
          # Example regex: `%r{href=.*?example[._-]v?(\d+(?:\.\d+)*)(?:\.[a-z]+|/)}i`
 | 
			
		||||
          regex ||= %r{href=.*?#{project_name}[._-]v?(\d+(?:\.\d+)*)(?:\.[a-z]+|/)}i
 | 
			
		||||
 | 
			
		||||
          Homebrew::Livecheck::Strategy::PageMatch.find_versions(page_url, regex)
 | 
			
		||||
          PageMatch.find_versions(page_url, regex)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@ -43,7 +43,7 @@ module Homebrew
 | 
			
		||||
          # Example regex: `%r{<h3>example-(.*?)/?</h3>}i`
 | 
			
		||||
          regex ||= %r{<h3>#{Regexp.escape(package_name)}-(.*?)/?</h3>}i
 | 
			
		||||
 | 
			
		||||
          Homebrew::Livecheck::Strategy::PageMatch.find_versions(page_url, regex)
 | 
			
		||||
          PageMatch.find_versions(page_url, regex)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@ -49,7 +49,7 @@ module Homebrew
 | 
			
		||||
          # The default regex is the same for all URLs using this strategy
 | 
			
		||||
          regex ||= %r{class="[^"]*version[^"]*"[^>]*>\s*Latest version is (.+)\s*</}
 | 
			
		||||
 | 
			
		||||
          Homebrew::Livecheck::Strategy::PageMatch.find_versions(page_url, regex)
 | 
			
		||||
          PageMatch.find_versions(page_url, regex)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@ -46,7 +46,7 @@ module Homebrew
 | 
			
		||||
          # * `%r{href=.*?/package/@example/example/v/(\d+(?:\.\d+)+)"}i`
 | 
			
		||||
          regex ||= %r{href=.*?/package/#{Regexp.escape(package_name)}/v/(\d+(?:\.\d+)+)"}i
 | 
			
		||||
 | 
			
		||||
          Homebrew::Livecheck::Strategy::PageMatch.find_versions(page_url, regex)
 | 
			
		||||
          PageMatch.find_versions(page_url, regex)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@ -71,12 +71,29 @@ module Homebrew
 | 
			
		||||
 | 
			
		||||
        # Checks the content at the URL for new versions, using the provided
 | 
			
		||||
        # 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.merge!(Strategy.page_content(url))
 | 
			
		||||
          content = match_data.delete(:content)
 | 
			
		||||
          content = if provided_content.present?
 | 
			
		||||
            provided_content
 | 
			
		||||
          else
 | 
			
		||||
            match_data.merge!(Strategy.page_content(url))
 | 
			
		||||
            match_data.delete(:content)
 | 
			
		||||
          end
 | 
			
		||||
          return match_data if content.blank?
 | 
			
		||||
 | 
			
		||||
          page_matches(content, regex, &block).each do |match_text|
 | 
			
		||||
 | 
			
		||||
@ -55,7 +55,7 @@ module Homebrew
 | 
			
		||||
            %r{href=.*?/packages.*?/#{Regexp.escape(package_name)}[._-]
 | 
			
		||||
               v?(\d+(?:\.\d+)*(.post\d+)?)#{Regexp.escape(suffix)}}ix
 | 
			
		||||
 | 
			
		||||
          Homebrew::Livecheck::Strategy::PageMatch.find_versions(page_url, regex)
 | 
			
		||||
          PageMatch.find_versions(page_url, regex)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@ -66,7 +66,7 @@ module Homebrew
 | 
			
		||||
          # create something that works for most URLs.
 | 
			
		||||
          regex ||= %r{url=.*?/#{Regexp.escape(project_name)}/files/.*?[-_/](\d+(?:[-.]\d+)+)[-_/%.]}i
 | 
			
		||||
 | 
			
		||||
          Homebrew::Livecheck::Strategy::PageMatch.find_versions(page_url, regex)
 | 
			
		||||
          PageMatch.find_versions(page_url, regex)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@ -1,8 +1,6 @@
 | 
			
		||||
# typed: false
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
require "open-uri"
 | 
			
		||||
 | 
			
		||||
module Homebrew
 | 
			
		||||
  module Livecheck
 | 
			
		||||
    module Strategy
 | 
			
		||||
@ -74,7 +72,6 @@ module Homebrew
 | 
			
		||||
        # @return [Hash]
 | 
			
		||||
        def self.find_versions(url, regex)
 | 
			
		||||
          file_name = File.basename(url)
 | 
			
		||||
 | 
			
		||||
          /^(?<module_name>.+)-\d+/i =~ file_name
 | 
			
		||||
 | 
			
		||||
          # /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.
 | 
			
		||||
          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
 | 
			
		||||
 | 
			
		||||
          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 = PageMatch.find_versions(page_url, regex, cached_content)
 | 
			
		||||
 | 
			
		||||
          # Cache responses to avoid unnecessary duplicate fetches
 | 
			
		||||
          @page_data[page_url] = URI.parse(page_url).open.read unless @page_data.key?(page_url)
 | 
			
		||||
 | 
			
		||||
          matches = @page_data[page_url].scan(regex)
 | 
			
		||||
          matches.map(&:first).uniq.each do |match|
 | 
			
		||||
            match_data[:matches][match] = Version.new(match)
 | 
			
		||||
          end
 | 
			
		||||
          # Cache any new page content
 | 
			
		||||
          @page_data[page_url] = match_data[:content] if match_data[:content].present?
 | 
			
		||||
 | 
			
		||||
          match_data
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
@ -34,7 +34,24 @@ describe Homebrew::Livecheck::Strategy::PageMatch do
 | 
			
		||||
      </html>
 | 
			
		||||
    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(: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
 | 
			
		||||
    it "returns true for any URL" do
 | 
			
		||||
@ -52,4 +69,10 @@ describe Homebrew::Livecheck::Strategy::PageMatch do
 | 
			
		||||
        .to eq(page_content_matches)
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user