2020-12-12 21:59:04 +01:00
|
|
|
# typed: false
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-12-13 12:23:20 +01:00
|
|
|
require "bundle_version"
|
2020-12-12 21:59:04 +01:00
|
|
|
require_relative "page_match"
|
|
|
|
|
|
|
|
module Homebrew
|
|
|
|
module Livecheck
|
|
|
|
module Strategy
|
|
|
|
# The {Sparkle} strategy fetches content at a URL and parses
|
|
|
|
# its contents as a Sparkle appcast in XML format.
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
class Sparkle
|
|
|
|
extend T::Sig
|
|
|
|
|
|
|
|
NICE_NAME = "Sparkle"
|
|
|
|
|
|
|
|
PRIORITY = 1
|
|
|
|
|
|
|
|
# Whether the strategy can be applied to the provided URL.
|
|
|
|
sig { params(url: String).returns(T::Boolean) }
|
|
|
|
def self.match?(url)
|
2020-12-14 02:35:26 +01:00
|
|
|
return false unless url.match?(%r{^https?://})
|
|
|
|
|
2020-12-14 04:35:26 +01:00
|
|
|
xml = url.end_with?(".xml")
|
2020-12-14 02:35:26 +01:00
|
|
|
xml ||= begin
|
|
|
|
headers = Strategy.page_headers(url)
|
2020-12-14 04:35:26 +01:00
|
|
|
content_type = headers["content-type"]&.split(";", 2)&.first
|
2020-12-14 02:35:26 +01:00
|
|
|
["application/xml", "text/xml"].include?(content_type)
|
|
|
|
end
|
|
|
|
return false unless xml
|
|
|
|
|
|
|
|
contents = Strategy.page_contents(url)
|
2020-12-14 04:36:05 +01:00
|
|
|
contents.match?(%r{https?://www.andymatuschak.org/xml-namespaces/sparkle})
|
2020-12-12 21:59:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Checks the content at the URL for new versions.
|
|
|
|
sig { params(url: String, regex: T.nilable(Regexp)).returns(T::Hash[Symbol, T.untyped]) }
|
2020-12-13 12:23:20 +01:00
|
|
|
def self.find_versions(url, regex, &block)
|
2020-12-12 21:59:04 +01:00
|
|
|
raise ArgumentError, "The #{NICE_NAME} strategy does not support regular expressions." if regex
|
|
|
|
|
|
|
|
require "nokogiri"
|
|
|
|
|
|
|
|
match_data = { matches: {}, regex: regex, url: url }
|
|
|
|
|
|
|
|
contents = Strategy.page_contents(url)
|
|
|
|
|
|
|
|
xml = Nokogiri.parse(contents)
|
|
|
|
xml.remove_namespaces!
|
|
|
|
|
2020-12-13 12:23:20 +01:00
|
|
|
enclosure =
|
|
|
|
xml.xpath("//rss//channel//item//enclosure")
|
|
|
|
.map { |e| { url: e["url"], version: BundleVersion.new(e["shortVersionString"], e["version"]) } }
|
|
|
|
.max_by { |e| e[:version] }
|
2020-12-12 21:59:04 +01:00
|
|
|
|
2020-12-13 12:23:20 +01:00
|
|
|
if enclosure
|
|
|
|
match = if block
|
2020-12-14 02:08:35 +01:00
|
|
|
enclosure[:version] = enclosure[:version].nice_version
|
2020-12-13 12:23:20 +01:00
|
|
|
block.call(enclosure).to_s
|
|
|
|
else
|
|
|
|
enclosure[:version].nice_version
|
|
|
|
end
|
|
|
|
|
|
|
|
match_data[:matches][match] = Version.new(match)
|
|
|
|
end
|
2020-12-12 21:59:04 +01:00
|
|
|
|
|
|
|
match_data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|