Class: Homebrew::Livecheck::Strategy::ExtractPlist Private

Inherits:
Object
  • Object
show all
Extended by:
Homebrew::Livecheck::Strategic
Defined in:
livecheck/strategy/extract_plist.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 ExtractPlist strategy downloads the file at a URL and extracts versions from contained .plist files using UnversionedCaskChecker.

In practice, this strategy operates by downloading very large files, so it's both slow and data-intensive. As such, the ExtractPlist strategy should only be used as an absolute last resort.

This strategy is not applied automatically and it's necessary to use strategy :extract_plist 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::ExtractPlist 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 Regexp used to determine if the strategy applies to the URL.

%r{^https?://}i

Class Method Summary collapse

Methods included from Homebrew::Livecheck::Strategic

find_versions, match?

Class Method Details

.cask_with_url(cask, url, url_options) ⇒ Cask::Cask

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.

Creates a copy of the cask with the artifact URL replaced by the provided URL, using the provided url_options. This will error if url_options contains any non-nil values with keys that aren't found in the Cask::URL.initialize keyword parameters.

Parameters:

  • cask (Cask::Cask)

    the cask to copy and modify to use the provided URL and options

  • url (String)

    the replacement URL

  • url_options (Hash{Symbol => T.untyped})

    options to use when replacing the URL

Returns:



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
# File 'livecheck/strategy/extract_plist.rb', line 101

def self.cask_with_url(cask, url, url_options)
  # Collect the `Cask::URL` initializer keyword parameter symbols
  @cask_url_kw_params ||= T.let(
    T::Utils.signature_for_method(
      Cask::URL.instance_method(:initialize),
    ).parameters.filter_map { |type, sym| sym if type == :key },
    T.nilable(T::Array[Symbol]),
  )

  # Collect `livecheck` block URL options supported by `Cask::URL`
  unused_opts = []
  url_kwargs = url_options.select do |key, value|
    next if value.nil?

    unless @cask_url_kw_params.include?(key)
      unused_opts << key
      next
    end

    true
  end

  unless unused_opts.empty?
    raise ArgumentError,
          "Cask `url` does not support `#{unused_opts.join("`, `")}` " \
          "#{Utils.pluralize("option", unused_opts.length)} from " \
          "`livecheck` block"
  end

  # Create a copy of the cask that overrides the artifact URL with the
  # provided URL and supported `livecheck` block URL options
  cask_copy = Cask::CaskLoader.load(cask.sourcefile_path)
  cask_copy.allow_reassignment = true
  cask_copy.url(url, **url_kwargs)
  cask_copy
end

.find_versions(cask:, url: nil, 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.

Uses UnversionedCaskChecker on the provided cask to identify versions from plist files.

Parameters:

  • cask (Cask::Cask)

    the cask to check for version information

  • url (String, nil) (defaults to: nil)

    an alternative URL to check for version information

  • regex (Regexp, nil) (defaults to: nil)

    a regex for use in a strategy block

  • content (String, nil) (defaults to: nil)

    content to check instead of fetching

  • options (Options) (defaults to: Options.new)

    options to modify behavior

  • block (Proc, nil)

Returns:



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'livecheck/strategy/extract_plist.rb', line 158

def self.find_versions(cask:, url: nil, 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: }

  items = if content
    match_data[:cached] = true
    Json.parse_json(content).transform_values do |obj|
      short_version = obj.dig("bundle_version", "short_version")
      version = obj.dig("bundle_version", "version")
      Item.new(bundle_version: BundleVersion.new(short_version, version))
    end
  else
    unversioned_cask_checker = if url.present? && url != cask.url.to_s
      UnversionedCaskChecker.new(cask_with_url(cask, url, options.url_options))
    else
      UnversionedCaskChecker.new(cask)
    end

    unversioned_cask_checker.all_versions.transform_values { |v| Item.new(bundle_version: v) }
  end
  return match_data if items.blank?

  versions_from_content(items, regex, &block).each do |version_text|
    match_data[:matches][version_text] = Version.new(version_text)
  end

  require "json"
  match_data[:content] = JSON.generate(items.transform_values(&:to_h)) unless match_data[:cached]
  match_data
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.

Parameters:

  • url (String)

    the URL to match against

Returns:

  • (Boolean)


57
58
59
# File 'livecheck/strategy/extract_plist.rb', line 57

def self.match?(url)
  URL_MATCH_REGEX.match?(url)
end

.versions_from_content(items, 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.

Identify versions from Items produced using UnversionedCaskChecker version information.

Parameters:

  • items (Hash{String => Item})

    a hash of Items containing version information

  • regex (Regexp, nil) (defaults to: nil)

    a regex for use in a strategy block

  • block (Proc, nil)

Returns:



74
75
76
77
78
79
80
81
82
83
# File 'livecheck/strategy/extract_plist.rb', line 74

def self.versions_from_content(items, regex = nil, &block)
  if block
    block_return_value = regex.present? ? yield(items, regex) : yield(items)
    return Strategy.handle_block_return(block_return_value)
  end

  items.filter_map do |_key, item|
    item.bundle_version.nice_version
  end.uniq
end