Class: Homebrew::Livecheck::Strategy::Sparkle Private
- Extended by:
- Homebrew::Livecheck::Strategic
- Defined in:
- livecheck/strategy/sparkle.rb
Overview
This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.
The Sparkle strategy fetches content at a URL and parses it as a Sparkle appcast in XML format.
This strategy is not applied automatically and it's necessary to use
strategy :sparkle in a livecheck block to apply it.
Defined Under Namespace
Classes: Item
Constant Summary collapse
- PRIORITY =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
A priority of zero causes livecheck to skip the strategy. We do this for Homebrew::Livecheck::Strategy::Sparkle so we can selectively apply it when appropriate.
0- URL_MATCH_REGEX =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
The
Regexpused to determine if the strategy applies to the URL. %r{^https?://}i- APPCAST_MACOS_STRINGS =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
Common
osvalues used in appcasts to refer to macOS. T.let(["macos", "osx"].freeze, T::Array[String])
Class Method Summary collapse
-
.filter_items(items) ⇒ Array<Item>
private
Filters out items that aren't suitable for Homebrew.
-
.find_versions(url:, regex: nil, content: nil, options: Options.new, &block) ⇒ Hash{Symbol => T.anything}
private
Checks the content at the URL for new versions.
-
.items_from_content(content) ⇒ Array<Item>
private
Identifies version information from a Sparkle appcast.
-
.match?(url) ⇒ Boolean
private
Whether the strategy can be applied to the provided URL.
-
.sort_items(items) ⇒ Array<Item>
private
Sorts items from newest to oldest.
-
.versions_from_content(content, regex = nil, &block) ⇒ Array<String>
private
Uses
#items_from_contentto identify versions from the Sparkle appcast content or, if a block is provided, passes the content to the block to handle matching.
Methods included from Homebrew::Livecheck::Strategic
Class Method Details
.filter_items(items) ⇒ Array<Item>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Filters out items that aren't suitable for Homebrew.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'livecheck/strategy/sparkle.rb', line 170 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 if (minimum_system_version = item.minimum_system_version) && minimum_system_version.strip_patch.prerelease? next false end true end.compact end |
.find_versions(url:, regex: nil, content: nil, options: Options.new, &block) ⇒ Hash{Symbol => T.anything}
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Checks the content at the URL for new versions.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'livecheck/strategy/sparkle.rb', line 247 def self.find_versions(url:, regex: nil, content: nil, options: Options.new, &block) if regex.present? && !block_given? raise ArgumentError, "#{Utils.demodulize(name)} only supports a regex when using a `strategy` block" end match_data = { matches: {}, regex:, url: } match_data[:cached] = true if content return match_data if url.blank? unless match_data[:cached] match_data.merge!(Strategy.page_content(url, options:)) content = match_data[:content] end return match_data if content.blank? versions_from_content(content, regex, &block).each do |version_text| match_data[:matches][version_text] = Version.new(version_text) end match_data end |
.items_from_content(content) ⇒ Array<Item>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Identifies version information from a Sparkle appcast.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'livecheck/strategy/sparkle.rb', line 91 def self.items_from_content(content) require "rexml/document" xml = Xml.parse_xml(content) # Remove prefixes, so we can reliably identify elements and attributes xml.root&.each_recursive do |node| node.prefix = "" node.attributes.each_attribute do |attribute| attribute.prefix = "" end end xml.get_elements("//rss//channel//item").filter_map do |item| enclosure = item.elements["enclosure"] if enclosure url = enclosure["url"].presence short_version = enclosure["shortVersionString"].presence version = enclosure["version"].presence os = enclosure["os"].presence end title = Xml.element_text(item, "title") link = Xml.element_text(item, "link") url ||= link channel = Xml.element_text(item, "channel") release_notes_link = Xml.element_text(item, "releaseNotesLink") short_version ||= Xml.element_text(item, "shortVersionString") version ||= Xml.element_text(item, "version") minimum_system_version_text = Xml.element_text(item, "minimumSystemVersion")&.gsub(/\A\D+|\D+\z/, "") if minimum_system_version_text.present? minimum_system_version = begin MacOSVersion.new(minimum_system_version_text) rescue MacOSVersion::Error nil end end pub_date = Xml.element_text(item, "pubDate")&.then do |date_string| Time.parse(date_string) rescue ArgumentError # Omit unparsable strings (e.g. non-English dates) nil end if (match = title&.match(/(\d+(?:\.\d+)*)\s*(\([^)]+\))?\Z/)) short_version ||= match[1] version ||= match[2] end bundle_version = BundleVersion.new(short_version, version) if short_version || version data = { title:, link:, channel:, release_notes_link:, pub_date:, os:, url:, bundle_version:, minimum_system_version:, }.compact next if data.empty? # Set a default `pub_date` (for sorting) if one isn't provided data[:pub_date] ||= Time.new(0) Item.new(**data) end end |
.match?(url) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Whether the strategy can be applied to the provided URL.
33 34 35 |
# File 'livecheck/strategy/sparkle.rb', line 33 def self.match?(url) URL_MATCH_REGEX.match?(url) end |
.sort_items(items) ⇒ Array<Item>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Sorts items from newest to oldest.
190 191 192 193 |
# File 'livecheck/strategy/sparkle.rb', line 190 def self.sort_items(items) items.sort_by { |item| [item.pub_date, item.bundle_version] } .reverse end |
.versions_from_content(content, regex = nil, &block) ⇒ Array<String>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
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.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'livecheck/strategy/sparkle.rb', line 209 def self.versions_from_content(content, regex = nil, &block) items = sort_items(filter_items(items_from_content(content))) return [] if items.empty? item = T.must(items.first) if block block_return_value = case block.parameters[0] when [:opt, :item], [:rest], [:req] regex.present? ? yield(item, regex) : yield(item) when [:opt, :items] regex.present? ? yield(items, regex) : yield(items) else raise "First argument of Sparkle `strategy` block must be `item` or `items`" end return Strategy.handle_block_return(block_return_value) end version = item.bundle_version&.nice_version version.present? ? [version] : [] end |