diff --git a/Library/Homebrew/cask/audit.rb b/Library/Homebrew/cask/audit.rb index 74f255020d96a..e94d140d43bf2 100644 --- a/Library/Homebrew/cask/audit.rb +++ b/Library/Homebrew/cask/audit.rb @@ -574,7 +574,7 @@ def audit_min_os debug_messages << "Plist #{plist_min_os}" if plist_min_os debug_messages << "Sparkle #{sparkle_min_os}" if sparkle_min_os odebug "Minimum OS version: #{debug_messages.join(" | ")}" unless debug_messages.empty? - min_os = [sparkle_min_os, plist_min_os].compact.max + min_os = [plist_min_os, sparkle_min_os].compact.max return if min_os.nil? || min_os <= HOMEBREW_MACOS_OLDEST_ALLOWED @@ -597,31 +597,31 @@ def livecheck_min_os return unless cask.livecheckable? return if cask.livecheck.strategy != :sparkle - out, _, status = curl_output("--fail", "--silent", "--location", cask.livecheck.url) - return unless status.success? + # `Sparkle` strategy blocks that use the `items` argument (instead of + # `item`) contain arbitrary logic that ignores/overrides the strategy's + # sorting, so we can't identify which item would be first/newest here. + return if cask.livecheck.strategy_block.present? && + cask.livecheck.strategy_block.parameters[0] == [:opt, :items] - require "rexml/document" - - xml = begin - REXML::Document.new(out) - rescue REXML::ParseException - nil - end - - return if xml.blank? - - item = xml.elements["//rss//channel//item"] - return if item.blank? - - min_os = item.elements["sparkle:minimumSystemVersion"]&.text - min_os = "11" if min_os == "10.16" - return if min_os.blank? + content = Homebrew::Livecheck::Strategy.page_content(cask.livecheck.url)[:content] + return if content.blank? begin - MacOSVersion.new(min_os).strip_patch - rescue MacOSVersion::Error - nil + items = Homebrew::Livecheck::Strategy::Sparkle.sort_items( + Homebrew::Livecheck::Strategy::Sparkle.filter_items( + Homebrew::Livecheck::Strategy::Sparkle.items_from_content(content), + ), + ) + rescue + return end + return if items.blank? + + min_os = items[0]&.minimum_system_version&.strip_patch + # Big Sur is sometimes identified as 10.16, so we override it to the + # expected macOS version (11). + min_os = MacOSVersion.new("11") if min_os == "10.16" + min_os end sig { returns(T.nilable(MacOSVersion)) } diff --git a/Library/Homebrew/livecheck/strategy/sparkle.rb b/Library/Homebrew/livecheck/strategy/sparkle.rb index 9f52b8b88beac..b7dc1b7cf8a5f 100644 --- a/Library/Homebrew/livecheck/strategy/sparkle.rb +++ b/Library/Homebrew/livecheck/strategy/sparkle.rb @@ -21,6 +21,9 @@ class Sparkle # The `Regexp` used to determine if the strategy applies to the URL. URL_MATCH_REGEX = %r{^https?://}i.freeze + # Common `os` values used in appcasts to refer to macOS. + APPCAST_MACOS_STRINGS = ["macos", "osx"].freeze + # Whether the strategy can be applied to the provided URL. # # @param url [String] the URL to match against @@ -86,19 +89,29 @@ def self.items_from_content(content) enclosure = item.elements["enclosure"] if enclosure - url = enclosure["url"] - short_version = enclosure["shortVersionString"] - version = enclosure["version"] - os = enclosure["os"] + url = enclosure["url"].presence + short_version = enclosure["shortVersionString"].presence + version = enclosure["version"].presence + os = enclosure["os"].presence end - title = item.elements["title"]&.text&.strip - link = item.elements["link"]&.text&.strip + title = item.elements["title"]&.text&.strip&.presence + link = item.elements["link"]&.text&.strip&.presence url ||= link - channel = item.elements["channel"]&.text&.strip - release_notes_link = item.elements["releaseNotesLink"]&.text&.strip - short_version ||= item.elements["shortVersionString"]&.text&.strip - version ||= item.elements["version"]&.text&.strip + channel = item.elements["channel"]&.text&.strip&.presence + release_notes_link = item.elements["releaseNotesLink"]&.text&.strip&.presence + short_version ||= item.elements["shortVersionString"]&.text&.strip&.presence + version ||= item.elements["version"]&.text&.strip&.presence + + minimum_system_version_text = + item.elements["minimumSystemVersion"]&.text&.strip&.gsub(/\A\D+|\D+\z/, "")&.presence + if minimum_system_version_text.present? + minimum_system_version = begin + MacOSVersion.new(minimum_system_version_text) + rescue MacOSVersion::Error + nil + end + end pub_date = item.elements["pubDate"]&.text&.strip&.presence&.then do |date_string| Time.parse(date_string) @@ -114,18 +127,6 @@ def self.items_from_content(content) bundle_version = BundleVersion.new(short_version, version) if short_version || version - next if os && !((os == "osx") || (os == "macos")) - - if (minimum_system_version = item.elements["minimumSystemVersion"]&.text&.gsub(/\A\D+|\D+\z/, "")) - macos_minimum_system_version = begin - MacOSVersion.new(minimum_system_version).strip_patch - rescue MacOSVersion::Error - nil - end - - next if macos_minimum_system_version&.prerelease? - end - data = { title: title, link: link, @@ -146,6 +147,33 @@ def self.items_from_content(content) end.compact end + # Filters out items that aren't suitable for Homebrew. + # + # @param items [Array] appcast items + # @return [Array] + sig { params(items: T::Array[Item]).returns(T::Array[Item]) } + def self.filter_items(items) + items.select do |item| + # Omit items with an explicit `os` value that isn't macOS + next false if item.os && APPCAST_MACOS_STRINGS.none?(item.os) + + # Omit items for prerelease macOS versions + next false if item.minimum_system_version&.strip_patch&.prerelease? + + true + end.compact + end + + # Sorts items from newest to oldest. + # + # @param items [Array] appcast items + # @return [Array] + sig { params(items: T::Array[Item]).returns(T::Array[Item]) } + def self.sort_items(items) + items.sort_by { |item| [item.pub_date, item.bundle_version] } + .reverse + end + # Uses `#items_from_content` to identify versions from the Sparkle # appcast content or, if a block is provided, passes the content to # the block to handle matching. @@ -161,7 +189,7 @@ def self.items_from_content(content) ).returns(T::Array[String]) } def self.versions_from_content(content, regex = nil, &block) - items = items_from_content(content).sort_by { |item| [item.pub_date, item.bundle_version] }.reverse + items = sort_items(filter_items(items_from_content(content))) return [] if items.blank? item = items.first diff --git a/Library/Homebrew/test/livecheck/strategy/sparkle_spec.rb b/Library/Homebrew/test/livecheck/strategy/sparkle_spec.rb index 7eaaff5288e65..86a890151b7ac 100644 --- a/Library/Homebrew/test/livecheck/strategy/sparkle_spec.rb +++ b/Library/Homebrew/test/livecheck/strategy/sparkle_spec.rb @@ -28,6 +28,17 @@ def create_appcast_xml(items_str = "") # `Sparkle::Item` objects. let(:item_hashes) do { + # The 1.2.4 version is only used in tests as the basis for an item that + # should be excluded (after modifications). + v124: { + title: "Version 1.2.4", + release_notes_link: "https://www.example.com/example/1.2.4.html", + pub_date: "Fri, 02 Jan 2021 01:23:45 +0000", + url: "https://www.example.com/example/example-1.2.4.tar.gz", + short_version: "1.2.4", + version: "124", + minimum_system_version: "10.10", + }, v123: { title: "Version 1.2.3", release_notes_link: "https://www.example.com/example/1.2.3.html", @@ -128,6 +139,16 @@ def create_appcast_xml(items_str = "") EOS + # Set the first item in a copy of `appcast` to a bad `minimumSystemVersion` + # value, to test `MacOSVersion::Error` handling. + bad_macos_version = appcast.sub( + v123_item, + v123_item.sub( + /()[^<]+?