Allow extracting URL in Sparkle strategy.

This commit is contained in:
Markus Reiter 2020-12-13 12:23:20 +01:00 committed by Sam Ford
parent 10b5548eac
commit b3c46ba2b9
No known key found for this signature in database
GPG Key ID: 95209E46C7FFDEFE
3 changed files with 22 additions and 10 deletions

View File

@ -67,7 +67,9 @@ class Livecheck
# #
# @param symbol [Symbol] symbol for the desired strategy # @param symbol [Symbol] symbol for the desired strategy
# @return [Symbol, nil] # @return [Symbol, nil]
def strategy(symbol = nil) def strategy(symbol = nil, &block)
@strategy_block = block if block
case symbol case symbol
when nil when nil
@strategy @strategy
@ -78,6 +80,8 @@ class Livecheck
end end
end end
attr_reader :strategy_block
# Sets the `@url` instance variable to the provided argument or returns the # Sets the `@url` instance variable to the provided argument or returns the
# `@url` instance variable when no argument is provided. The argument can be # `@url` instance variable when no argument is provided. The argument can be
# a `String` (a URL) or a supported `Symbol` corresponding to a URL in the # a `String` (a URL) or a supported `Symbol` corresponding to a URL in the

View File

@ -480,7 +480,7 @@ module Homebrew
next if strategy.blank? next if strategy.blank?
strategy_data = strategy.find_versions(url, livecheck_regex) strategy_data = strategy.find_versions(url, livecheck_regex, &livecheck.strategy_block)
match_version_map = strategy_data[:matches] match_version_map = strategy_data[:matches]
regex = strategy_data[:regex] regex = strategy_data[:regex]

View File

@ -1,6 +1,7 @@
# typed: false # typed: false
# frozen_string_literal: true # frozen_string_literal: true
require "bundle_version"
require_relative "page_match" require_relative "page_match"
module Homebrew module Homebrew
@ -27,7 +28,7 @@ module Homebrew
# Checks the content at the URL for new versions. # Checks the content at the URL for new versions.
sig { params(url: String, regex: T.nilable(Regexp)).returns(T::Hash[Symbol, T.untyped]) } sig { params(url: String, regex: T.nilable(Regexp)).returns(T::Hash[Symbol, T.untyped]) }
def self.find_versions(url, regex) def self.find_versions(url, regex, &block)
raise ArgumentError, "The #{NICE_NAME} strategy does not support regular expressions." if regex raise ArgumentError, "The #{NICE_NAME} strategy does not support regular expressions." if regex
require "nokogiri" require "nokogiri"
@ -39,14 +40,21 @@ module Homebrew
xml = Nokogiri.parse(contents) xml = Nokogiri.parse(contents)
xml.remove_namespaces! xml.remove_namespaces!
match = xml.xpath("//rss//channel//item//enclosure") enclosure =
.map { |enclosure| [*enclosure["shortVersionString"], *enclosure["version"]].uniq } xml.xpath("//rss//channel//item//enclosure")
.reject(&:empty?) .map { |e| { url: e["url"], version: BundleVersion.new(e["shortVersionString"], e["version"]) } }
.uniq .max_by { |e| e[:version] }
.max_by { |versions| versions.map { |v| Version.new(v) } }
&.join(",")
match_data[:matches][match] = Version.new(match) if match if enclosure
match = if block
enclosure[:version] = Cask::DSL::Version.new(enclosure[:version].nice_version)
block.call(enclosure).to_s
else
enclosure[:version].nice_version
end
match_data[:matches][match] = Version.new(match)
end
match_data match_data
end end