Skip to content

Commit

Permalink
livecheck: move url/regex generation into methods
Browse files Browse the repository at this point in the history
  • Loading branch information
samford committed Aug 17, 2021
1 parent f026dd2 commit 2682130
Show file tree
Hide file tree
Showing 27 changed files with 795 additions and 198 deletions.
3 changes: 3 additions & 0 deletions Library/Homebrew/livecheck/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ module Strategy
# In rare cases, this can also be a double newline (`\n\n`).
HTTP_HEAD_BODY_SEPARATOR = "\r\n\r\n"

# A regex used to identify a tarball extension at the end of a string.
TARBALL_EXTENSION_REGEX = /\.t(?:ar\..+|[a-z0-9]+)$/i.freeze

# An error message to use when a `strategy` block returns a value of
# an inappropriate type.
INVALID_BLOCK_RETURN_VALUE_MSG = "Return value of a strategy block must be a string or array of strings."
Expand Down
50 changes: 35 additions & 15 deletions Library/Homebrew/livecheck/strategy/apache.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: false
# typed: true
# frozen_string_literal: true

module Homebrew
Expand Down Expand Up @@ -42,6 +42,38 @@ def self.match?(url)
URL_MATCH_REGEX.match?(url)
end

# Extracts information from a provided URL and uses it to generate
# various input values used by the strategy to check for new versions.
# Some of these values act as defaults and can be overridden in a
# `livecheck` block.
#
# @param url [String] the URL used to generate values
# @return [Hash]
sig { params(url: String).returns(T::Hash[Symbol, T.untyped]) }
def self.generate_input_values(url)
values = {}

match = url.match(URL_MATCH_REGEX)
return values if match.blank?

# Example URL: `https://archive.apache.org/dist/example/`
values[:url] = "https://archive.apache.org/dist/#{match[:path]}/"

regex_prefix = Regexp.escape(match[:prefix] || "").gsub("\\-", "-")

# Use `\.t` instead of specific tarball extensions (e.g. .tar.gz)
suffix = match[:suffix]&.sub(Strategy::TARBALL_EXTENSION_REGEX, "\.t")
regex_suffix = Regexp.escape(suffix || "").gsub("\\-", "-")

# Example directory regex: `%r{href=["']?v?(\d+(?:\.\d+)+)/}i`
# Example file regexes:
# * `/href=["']?example-v?(\d+(?:\.\d+)+)\.t/i`
# * `/href=["']?example-v?(\d+(?:\.\d+)+)-bin\.zip/i`
values[:regex] = /href=["']?#{regex_prefix}v?(\d+(?:\.\d+)+)#{regex_suffix}/i

values
end

# Generates a URL and regex (if one isn't provided) and passes them
# to {PageMatch.find_versions} to identify versions in the content.
#
Expand All @@ -59,21 +91,9 @@ def self.match?(url)
).returns(T::Hash[Symbol, T.untyped])
}
def self.find_versions(url:, regex: nil, **unused, &block)
match = url.match(URL_MATCH_REGEX)

# Use `\.t` instead of specific tarball extensions (e.g. .tar.gz)
suffix = match[:suffix].sub(/\.t(?:ar\..+|[a-z0-9]+)$/i, "\.t")

# Example URL: `https://archive.apache.org/dist/example/`
page_url = "https://archive.apache.org/dist/#{match[:path]}/"

# Example directory regex: `%r{href=["']?v?(\d+(?:\.\d+)+)/}i`
# Example file regexes:
# * `/href=["']?example-v?(\d+(?:\.\d+)+)\.t/i`
# * `/href=["']?example-v?(\d+(?:\.\d+)+)-bin\.zip/i`
regex ||= /href=["']?#{Regexp.escape(match[:prefix])}v?(\d+(?:\.\d+)+)#{Regexp.escape(suffix)}/i
generated = generate_input_values(url)

PageMatch.find_versions(url: page_url, regex: regex, **unused, &block)
T.unsafe(PageMatch).find_versions(url: generated[:url], regex: regex || generated[:regex], **unused, &block)
end
end
end
Expand Down
58 changes: 39 additions & 19 deletions Library/Homebrew/livecheck/strategy/bitbucket.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: false
# typed: true
# frozen_string_literal: true

module Homebrew
Expand Down Expand Up @@ -49,6 +49,42 @@ def self.match?(url)
URL_MATCH_REGEX.match?(url)
end

# Extracts information from a provided URL and uses it to generate
# various input values used by the strategy to check for new versions.
# Some of these values act as defaults and can be overridden in a
# `livecheck` block.
#
# @param url [String] the URL used to generate values
# @return [Hash]
sig { params(url: String).returns(T::Hash[Symbol, T.untyped]) }
def self.generate_input_values(url)
values = {}

match = url.match(URL_MATCH_REGEX)
return values if match.blank?

# `/get/` archives are Git tag snapshots, so we need to check that tab
# instead of the main `/downloads/` page
values[:url] = if match[:dl_type] == "get"
"https://bitbucket.org/#{match[:path]}/downloads/?tab=tags"
else
"https://bitbucket.org/#{match[:path]}/downloads/"
end

regex_prefix = Regexp.escape(T.must(match[:prefix])).gsub("\\-", "-")

# Use `\.t` instead of specific tarball extensions (e.g. .tar.gz)
suffix = T.must(match[:suffix]).sub(Strategy::TARBALL_EXTENSION_REGEX, "\.t")
regex_suffix = Regexp.escape(suffix).gsub("\\-", "-")

# Example regexes:
# * `/href=.*?v?(\d+(?:\.\d+)+)\.t/i`
# * `/href=.*?example-v?(\d+(?:\.\d+)+)\.t/i`
values[:regex] = /href=.*?#{regex_prefix}v?(\d+(?:\.\d+)+)#{regex_suffix}/i

values
end

# Generates a URL and regex (if one isn't provided) and passes them
# to {PageMatch.find_versions} to identify versions in the content.
#
Expand All @@ -66,25 +102,9 @@ def self.match?(url)
).returns(T::Hash[Symbol, T.untyped])
}
def self.find_versions(url:, regex: nil, **unused, &block)
match = url.match(URL_MATCH_REGEX)

# Use `\.t` instead of specific tarball extensions (e.g. .tar.gz)
suffix = match[:suffix].sub(/\.t(?:ar\..+|[a-z0-9]+)$/i, "\.t")

# `/get/` archives are Git tag snapshots, so we need to check that tab
# instead of the main `/downloads/` page
page_url = if match[:dl_type] == "get"
"https://bitbucket.org/#{match[:path]}/downloads/?tab=tags"
else
"https://bitbucket.org/#{match[:path]}/downloads/"
end

# Example regexes:
# * `/href=.*?v?(\d+(?:\.\d+)+)\.t/i`
# * `/href=.*?example-v?(\d+(?:\.\d+)+)\.t/i`
regex ||= /href=.*?#{Regexp.escape(match[:prefix])}v?(\d+(?:\.\d+)+)#{Regexp.escape(suffix)}/i
generated = generate_input_values(url)

PageMatch.find_versions(url: page_url, regex: regex, **unused, &block)
T.unsafe(PageMatch).find_versions(url: generated[:url], regex: regex || generated[:regex], **unused, &block)
end
end
end
Expand Down
44 changes: 32 additions & 12 deletions Library/Homebrew/livecheck/strategy/cpan.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: false
# typed: true
# frozen_string_literal: true

module Homebrew
Expand Down Expand Up @@ -40,6 +40,35 @@ def self.match?(url)
URL_MATCH_REGEX.match?(url)
end

# Extracts information from a provided URL and uses it to generate
# various input values used by the strategy to check for new versions.
# Some of these values act as defaults and can be overridden in a
# `livecheck` block.
#
# @param url [String] the URL used to generate values
# @return [Hash]
sig { params(url: String).returns(T::Hash[Symbol, T.untyped]) }
def self.generate_input_values(url)
values = {}

match = url.match(URL_MATCH_REGEX)
return values if match.blank?

# The directory listing page where the archive files are found
values[:url] = "https://cpan.metacpan.org#{match[:path]}"

regex_prefix = Regexp.escape(T.must(match[:prefix])).gsub("\\-", "-")

# Use `\.t` instead of specific tarball extensions (e.g. .tar.gz)
suffix = T.must(match[:suffix]).sub(Strategy::TARBALL_EXTENSION_REGEX, "\.t")
regex_suffix = Regexp.escape(suffix).gsub("\\-", "-")

# Example regex: `/href=.*?Brew[._-]v?(\d+(?:\.\d+)*)\.t/i`
values[:regex] = /href=.*?#{regex_prefix}[._-]v?(\d+(?:\.\d+)*)#{regex_suffix}/i

values
end

# Generates a URL and regex (if one isn't provided) and passes them
# to {PageMatch.find_versions} to identify versions in the content.
#
Expand All @@ -57,18 +86,9 @@ def self.match?(url)
).returns(T::Hash[Symbol, T.untyped])
}
def self.find_versions(url:, regex: nil, **unused, &block)
match = url.match(URL_MATCH_REGEX)

# Use `\.t` instead of specific tarball extensions (e.g. .tar.gz)
suffix = match[:suffix].sub(/\.t(?:ar\..+|[a-z0-9]+)$/i, "\.t")

# The directory listing page where the archive files are found
page_url = "https://cpan.metacpan.org#{match[:path]}"

# Example regex: `/href=.*?Brew[._-]v?(\d+(?:\.\d+)*)\.t/i`
regex ||= /href=.*?#{match[:prefix]}[._-]v?(\d+(?:\.\d+)*)#{Regexp.escape(suffix)}/i
generated = generate_input_values(url)

PageMatch.find_versions(url: page_url, regex: regex, **unused, &block)
T.unsafe(PageMatch).find_versions(url: generated[:url], regex: regex || generated[:regex], **unused, &block)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/livecheck/strategy/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class Git
# lowest to highest).
PRIORITY = 8

# The default regex used to naively identify numeric versions from tags
# when a regex isn't provided.
# The default regex used to naively identify versions from tags when a
# regex isn't provided.
DEFAULT_REGEX = /\D*(.+)/.freeze

# Whether the strategy can be applied to the provided URL.
Expand Down
36 changes: 27 additions & 9 deletions Library/Homebrew/livecheck/strategy/github_latest.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: false
# typed: true
# frozen_string_literal: true

module Homebrew
Expand Down Expand Up @@ -48,6 +48,10 @@ class GithubLatest
/(?<repository>[^/]+) # The GitHub repository name
}ix.freeze

# The default regex used to identify a version from a tag when a regex
# isn't provided.
DEFAULT_REGEX = %r{href=.*?/tag/v?(\d+(?:\.\d+)+)["' >]}i.freeze

# Whether the strategy can be applied to the provided URL.
#
# @param url [String] the URL to match against
Expand All @@ -57,6 +61,26 @@ def self.match?(url)
URL_MATCH_REGEX.match?(url)
end

# Extracts information from a provided URL and uses it to generate
# various input values used by the strategy to check for new versions.
# Some of these values act as defaults and can be overridden in a
# `livecheck` block.
#
# @param url [String] the URL used to generate values
# @return [Hash]
sig { params(url: String).returns(T::Hash[Symbol, T.untyped]) }
def self.generate_input_values(url)
values = {}

match = url.sub(/\.git$/i, "").match(URL_MATCH_REGEX)
return values if match.blank?

# Example URL: `https://github.com/example/example/releases/latest`
values[:url] = "https://github.com/#{match[:username]}/#{match[:repository]}/releases/latest"

values
end

# Generates a URL and regex (if one isn't provided) and passes them
# to {PageMatch.find_versions} to identify versions in the content.
#
Expand All @@ -74,15 +98,9 @@ def self.match?(url)
).returns(T::Hash[Symbol, T.untyped])
}
def self.find_versions(url:, regex: nil, **unused, &block)
match = url.sub(/\.git$/i, "").match(URL_MATCH_REGEX)

# Example URL: `https://github.com/example/example/releases/latest`
page_url = "https://github.com/#{match[:username]}/#{match[:repository]}/releases/latest"

# The default regex is the same for all URLs using this strategy
regex ||= %r{href=.*?/tag/v?(\d+(?:\.\d+)+)["' >]}i
generated = generate_input_values(url)

PageMatch.find_versions(url: page_url, regex: regex, **unused, &block)
T.unsafe(PageMatch).find_versions(url: generated[:url], regex: regex || DEFAULT_REGEX, **unused, &block)
end
end
end
Expand Down
49 changes: 36 additions & 13 deletions Library/Homebrew/livecheck/strategy/gnome.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: false
# typed: true
# frozen_string_literal: true

module Homebrew
Expand Down Expand Up @@ -45,6 +45,32 @@ def self.match?(url)
URL_MATCH_REGEX.match?(url)
end

# Extracts information from a provided URL and uses it to generate
# various input values used by the strategy to check for new versions.
# Some of these values act as defaults and can be overridden in a
# `livecheck` block.
#
# @param url [String] the URL used to generate values
# @return [Hash]
sig { params(url: String).returns(T::Hash[Symbol, T.untyped]) }
def self.generate_input_values(url)
values = {}

match = url.match(URL_MATCH_REGEX)
return values if match.blank?

values[:url] = "https://download.gnome.org/sources/#{match[:package_name]}/cache.json"

regex_name = Regexp.escape(T.must(match[:package_name])).gsub("\\-", "-")

# GNOME archive files seem to use a standard filename format, so we
# count on the delimiter between the package name and numeric
# version being a hyphen and the file being a tarball.
values[:regex] = /#{regex_name}-(\d+(?:\.\d+)+)\.t/i

values
end

# Generates a URL and regex (if one isn't provided) and passes them
# to {PageMatch.find_versions} to identify versions in the content.
#
Expand All @@ -62,27 +88,24 @@ def self.match?(url)
).returns(T::Hash[Symbol, T.untyped])
}
def self.find_versions(url:, regex: nil, **unused, &block)
match = url.match(URL_MATCH_REGEX)
generated = generate_input_values(url)

page_url = "https://download.gnome.org/sources/#{match[:package_name]}/cache.json"
version_data = T.unsafe(PageMatch).find_versions(
url: generated[:url],
regex: regex || generated[:regex],
**unused,
&block
)

if regex.blank?
# GNOME archive files seem to use a standard filename format, so we
# count on the delimiter between the package name and numeric
# version being a hyphen and the file being a tarball.
regex = /#{Regexp.escape(match[:package_name])}-(\d+(?:\.\d+)+)\.t/i
version_data = PageMatch.find_versions(url: page_url, regex: regex, **unused, &block)

# Filter out unstable versions using the old version scheme where
# the major version is below 40.
version_data[:matches].reject! do |_, version|
version.major < 40 && (version.minor >= 90 || version.minor.to_i.odd?)
end

version_data
else
PageMatch.find_versions(url: page_url, regex: regex, **unused, &block)
end

version_data
end
end
end
Expand Down

0 comments on commit 2682130

Please sign in to comment.