Module: Homebrew::Livecheck::Strategy Private

Extended by:
T::Sig
Defined in:
livecheck/strategy.rb,
livecheck/strategy/git.rb,
livecheck/strategy/gnu.rb,
livecheck/strategy/npm.rb,
livecheck/strategy/cpan.rb,
livecheck/strategy/pypi.rb,
livecheck/strategy/xorg.rb,
livecheck/strategy/gnome.rb,
livecheck/strategy/apache.rb,
livecheck/strategy/hackage.rb,
livecheck/strategy/sparkle.rb,
livecheck/strategy/bitbucket.rb,
livecheck/strategy/launchpad.rb,
livecheck/strategy/page_match.rb,
livecheck/strategy/sourceforge.rb,
livecheck/strategy/header_match.rb,
livecheck/strategy/extract_plist.rb,
livecheck/strategy/github_latest.rb,
livecheck/strategy/electron_builder.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

The Livecheck::Strategy module contains the various strategies as well as some general-purpose methods for working with them. Within the context of the brew livecheck command, strategies are established procedures for finding new software versions at a given source.

Defined Under Namespace

Classes: Apache, Bitbucket, Cpan, ElectronBuilder, ExtractPlist, Git, GithubLatest, Gnome, Gnu, Hackage, HeaderMatch, Launchpad, Npm, PageMatch, Pypi, Sourceforge, Sparkle, Xorg

Class Method Summary collapse

Class Method Details

.from_symbol(symbol) ⇒ Strategy?

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.

Returns the Homebrew::Livecheck::Strategy that corresponds to the provided Symbol (or nil if there is no matching Homebrew::Livecheck::Strategy).

Parameters:

  • symbol (Symbol)

    the strategy name in snake case as a Symbol (e.g. :page_match)

Returns:



111
112
113
# File 'livecheck/strategy.rb', line 111

def from_symbol(symbol)
  strategies[symbol]
end

.from_url(url, livecheck_strategy: nil, url_provided: nil, regex_provided: nil, block_provided: nil) ⇒ Array

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.

Returns an array of strategies that apply to the provided URL.

Parameters:

  • url (String)

    the URL to check for matching strategies

  • livecheck_strategy (Symbol) (defaults to: nil)

    a Homebrew::Livecheck::Strategy symbol from the livecheck block

  • regex_provided (Boolean) (defaults to: nil)

    whether a regex is provided in the livecheck block

Returns:

  • (Array)


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'livecheck/strategy.rb', line 123

def from_url(url, livecheck_strategy: nil, url_provided: nil, regex_provided: nil, block_provided: nil)
  usable_strategies = strategies.values.select do |strategy|
    if strategy == PageMatch
      # Only treat the `PageMatch` strategy as usable if a regex is
      # present in the `livecheck` block
      next if !regex_provided && !block_provided
    elsif strategy.const_defined?(:PRIORITY) &&
          !strategy::PRIORITY.positive? &&
          from_symbol(livecheck_strategy) != strategy
      # Ignore strategies with a priority of 0 or lower, unless the
      # strategy is specified in the `livecheck` block
      next
    end

    strategy.respond_to?(:match?) && strategy.match?(url)
  end

  # Sort usable strategies in descending order by priority, using the
  # DEFAULT_PRIORITY when a strategy doesn't contain a PRIORITY constant
  usable_strategies.sort_by do |strategy|
    (strategy.const_defined?(:PRIORITY) ? -strategy::PRIORITY : -DEFAULT_PRIORITY)
  end
end

.page_content(url) ⇒ Hash{Symbol => T.untyped}

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.

Fetches the content at the URL and returns a hash containing the content and, if there are any redirections, the final URL. If curl encounters an error, the hash will contain a :messages array with the error message instead.

Parameters:

  • url (String)

    the URL of the content to check

Returns:



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'livecheck/strategy.rb', line 179

def self.page_content(url)
  original_url = url

  stderr = nil
  [:default, :browser].each do |user_agent|
    stdout, stderr, status = curl_with_workarounds(
      *PAGE_CONTENT_CURL_ARGS, url,
      **DEFAULT_CURL_OPTIONS,
      user_agent: user_agent
    )
    next unless status.success?

    # stdout contains the header information followed by the page content.
    # We use #scrub here to avoid "invalid byte sequence in UTF-8" errors.
    output = stdout.scrub

    # Separate the head(s)/body and identify the final URL (after any
    # redirections)
    max_iterations = 5
    iterations = 0
    output = output.lstrip
    while output.match?(%r{\AHTTP/[\d.]+ \d+}) && output.include?(HTTP_HEAD_BODY_SEPARATOR)
      iterations += 1
      raise "Too many redirects (max = #{max_iterations})" if iterations > max_iterations

      head_text, _, output = output.partition(HTTP_HEAD_BODY_SEPARATOR)
      output = output.lstrip

      location = head_text[/^Location:\s*(.*)$/i, 1]
      next if location.blank?

      location.chomp!
      # Convert a relative redirect URL to an absolute URL
      location = URI.join(url, location) unless location.match?(PageMatch::URL_MATCH_REGEX)
      final_url = location
    end

    data = { content: output }
    data[:final_url] = final_url if final_url.present? && final_url != original_url
    return data
  end

  /^(?<error_msg>curl: \(\d+\) .+)/ =~ stderr
  {
    messages: [error_msg.presence || "cURL failed without an error"],
  }
end

.page_headers(url) ⇒ Object

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.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'livecheck/strategy.rb', line 147

def self.page_headers(url)
  headers = []

  [:default, :browser].each do |user_agent|
    stdout, _, status = curl_with_workarounds(
      *PAGE_HEADERS_CURL_ARGS, url,
      **DEFAULT_CURL_OPTIONS,
      user_agent: user_agent
    )

    while stdout.match?(/\AHTTP.*\r$/)
      h, stdout = stdout.split("\r\n\r\n", 2)

      headers << h.split("\r\n").drop(1)
                  .map { |header| header.split(/:\s*/, 2) }
                  .to_h.transform_keys(&:downcase)
    end

    return headers if status.success?
  end

  headers
end