diff --git a/Library/Homebrew/livecheck/strategy.rb b/Library/Homebrew/livecheck/strategy.rb index c2c6f5ca40..4115432951 100644 --- a/Library/Homebrew/livecheck/strategy.rb +++ b/Library/Homebrew/livecheck/strategy.rb @@ -86,6 +86,7 @@ end require_relative "strategy/apache" require_relative "strategy/bitbucket" +require_relative "strategy/cpan" require_relative "strategy/git" require_relative "strategy/github_latest" require_relative "strategy/gnome" diff --git a/Library/Homebrew/livecheck/strategy/cpan.rb b/Library/Homebrew/livecheck/strategy/cpan.rb new file mode 100644 index 0000000000..9a988335c2 --- /dev/null +++ b/Library/Homebrew/livecheck/strategy/cpan.rb @@ -0,0 +1,67 @@ +# typed: false +# frozen_string_literal: true + +require "uri" + +module Homebrew + module Livecheck + module Strategy + # The {Cpan} strategy identifies versions of software at + # cpan.metacpan.org by checking directory listing pages. + # + # CPAN URLs take the following format: + # + # * `https://cpan.metacpan.org/authors/id/M/MI/MIYAGAWA/Carton-v1.0.34.tar.gz` + # + # @api public + class Cpan + NICE_NAME = "CPAN" + + # The allowlist used to determine if the strategy applies to the URL. + HOST_ALLOWLIST = ["cpan.metacpan.org"].freeze + + # Whether the strategy can be applied to the provided URL. + # + # @param url [String] the URL to match against + # @return [Boolean] + def self.match?(url) + uri = URI.parse url + HOST_ALLOWLIST.include? uri.host + rescue URI::InvalidURIError + false + end + + # Generates a URL and regex (if one isn't provided) and passes them + # to {PageMatch.find_versions} to identify versions in the content. + # + # @param url [String] the URL of the content to check + # @param regex [Regexp] a regex used for matching versions in content + # @return [Hash] + def self.find_versions(url, regex = nil) + %r{ + /authors/id/(?(?:[^/](?:/[^/]+){2})) # The author path (e.g. M/MI/MIYAGAWA) + /(?.+) # The package path (e.g. Carton-v1.0.34.tar.gz) + }ix =~ url + + # We need a Pathname because we've monkeypatched extname to support + # double extensions (e.g. tar.gz). + pathname = Pathname.new(package_path) + + # Exteract package name + /^(?.+)-v?\d+/ =~ pathname.basename(pathname.extname) + # Use `\.t` instead of specific tarball extensions (e.g. .tar.gz) + suffix = pathname.extname.sub!(/\.t(?:ar\..+|[a-z0-9]+)$/i, "\.t") + + # A page containing a directory listing of the latest source tarball + package_dir = Pathname.new(author_path).join(pathname.dirname) + page_url = "https://cpan.metacpan.org/authors/id/#{package_dir}/" + + # Example regex: `%r{href=.*?Carton-v?(\d+(?:\.\d+)*).t}i` + regex ||= /href=.*?#{package_name}-v?(\d+(?:\.\d+)*)#{Regexp.escape(suffix)}/i + + Homebrew::Livecheck::Strategy::PageMatch.find_versions(page_url, regex) + end + end + end + end +end diff --git a/Library/Homebrew/test/livecheck/strategy/cpan_spec.rb b/Library/Homebrew/test/livecheck/strategy/cpan_spec.rb new file mode 100644 index 0000000000..9538d0d3d6 --- /dev/null +++ b/Library/Homebrew/test/livecheck/strategy/cpan_spec.rb @@ -0,0 +1,21 @@ +# typed: false +# frozen_string_literal: true + +require "livecheck/strategy/cpan" + +describe Homebrew::Livecheck::Strategy::Cpan do + subject(:cpan) { described_class } + + let(:cpan_url) { "https://cpan.metacpan.org/authors/id/M/MI/MIYAGAWA/Carton-v1.0.34.tar.gz" } + let(:non_cpan_url) { "https://brew.sh/test" } + + describe "::match?" do + it "returns true if the argument provided is a CPAN URL" do + expect(cpan.match?(cpan_url)).to be true + end + + it "returns false if the argument provided is not a CPAN URL" do + expect(cpan.match?(non_cpan_url)).to be false + end + end +end