Merge pull request #13346 from bevanjkay/extract-plist

livecheck: allow custom url in extract_plist strategy
This commit is contained in:
Sam Ford 2022-06-24 08:44:14 -04:00 committed by GitHub
commit 770af2d4bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 9 deletions

View File

@ -20,7 +20,7 @@ module Cask
attr_reader :token, :sourcefile_path, :source, :config, :default_config
attr_accessor :download
attr_accessor :download, :allow_reassignment
def self.all
Tap.flat_map(&:cask_files).map do |f|
@ -38,11 +38,12 @@ module Cask
@tap
end
def initialize(token, sourcefile_path: nil, source: nil, tap: nil, config: nil, &block)
def initialize(token, sourcefile_path: nil, source: nil, tap: nil, config: nil, allow_reassignment: false, &block)
@token = token
@sourcefile_path = sourcefile_path
@source = source
@tap = tap
@allow_reassignment = allow_reassignment
@block = block
@default_config = config || Config.new

View File

@ -112,7 +112,7 @@ module Cask
def set_unique_stanza(stanza, should_return)
return instance_variable_get("@#{stanza}") if should_return
if instance_variable_defined?("@#{stanza}")
if !@cask.allow_reassignment && instance_variable_defined?("@#{stanza}")
raise CaskInvalidError.new(cask, "'#{stanza}' stanza may only appear once.")
end
@ -137,7 +137,7 @@ module Cask
return unless default
unless @language_blocks.default.nil?
if !@cask.allow_reassignment && @language_blocks.default.present?
raise CaskInvalidError.new(cask, "Only one default language may be defined.")
end
@ -294,7 +294,9 @@ module Cask
@livecheck ||= Livecheck.new(self)
return @livecheck unless block
raise CaskInvalidError.new(cask, "'livecheck' stanza may only appear once.") if @livecheckable
if !@cask.allow_reassignment && @livecheckable
raise CaskInvalidError.new(cask, "'livecheck' stanza may only appear once.")
end
@livecheckable = true
@livecheck.instance_eval(&block)

View File

@ -563,6 +563,7 @@ module Homebrew
# Identifies the latest version of the formula and returns a Hash containing
# the version information. Returns nil if a latest version couldn't be found.
# rubocop:disable Metrics/CyclomaticComplexity
sig {
params(
formula_or_cask: T.any(Formula, Cask::Cask),
@ -657,7 +658,7 @@ module Homebrew
end
if livecheck_strategy.present?
if livecheck_url.blank?
if livecheck_url.blank? && strategy.method(:find_versions).parameters.include?([:keyreq, :url])
odebug "#{strategy_name} strategy requires a URL"
next
elsif livecheck_strategy != :page_match && strategies.exclude?(strategy)
@ -768,6 +769,7 @@ module Homebrew
nil
end
# rubocop:enable Metrics/CyclomaticComplexity
end
# rubocop:enable Metrics/ModuleLength
end

View File

@ -82,25 +82,37 @@ module Homebrew
# versions from `plist` files.
#
# @param cask [Cask::Cask] the cask to check for version information
# @param url [String, nil] an alternative URL to check for version
# information
# @param regex [Regexp, nil] a regex for use in a strategy block
# @return [Hash]
sig {
params(
cask: Cask::Cask,
url: T.nilable(String),
regex: T.nilable(Regexp),
_unused: T.nilable(T::Hash[Symbol, T.untyped]),
block: T.untyped,
).returns(T::Hash[Symbol, T.untyped])
}
def self.find_versions(cask:, regex: nil, **_unused, &block)
def self.find_versions(cask:, url: nil, regex: nil, **_unused, &block)
if regex.present? && block.blank?
raise ArgumentError, "#{T.must(name).demodulize} only supports a regex when using a `strategy` block"
end
raise ArgumentError, "The #{T.must(name).demodulize} strategy only supports casks." unless T.unsafe(cask)
match_data = { matches: {}, regex: regex }
match_data = { matches: {}, regex: regex, url: url }
unversioned_cask_checker = if url.present? && url != cask.url.to_s
# Create a copy of the `cask` that uses the `livecheck` block URL
cask_copy = Cask::CaskLoader.load(cask.full_name)
cask_copy.allow_reassignment = true
cask_copy.url { url }
UnversionedCaskChecker.new(cask_copy)
else
UnversionedCaskChecker.new(cask)
end
unversioned_cask_checker = UnversionedCaskChecker.new(cask)
items = unversioned_cask_checker.all_versions.transform_values { |v| Item.new(bundle_version: v) }
versions_from_items(items, regex, &block).each do |version_text|