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:



50
51
52
# File 'livecheck/strategy.rb', line 50

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)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'livecheck/strategy.rb', line 62

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.

Parameters:

  • url (String)

    the URL of the content to check

Returns:



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

def self.page_content(url)
  original_url = url

  # Manually handling `URI#open` redirections allows us to detect the
  # resolved URL while also supporting HTTPS to HTTP redirections (which
  # are normally forbidden by `OpenURI`).
  begin
    content = URI.parse(url).open(redirect: false, &:read)
  rescue OpenURI::HTTPRedirect => e
    url = e.uri.to_s
    retry
  end

  data = { content: content }
  data[:final_url] = url unless url == original_url
  data
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.



86
87
88
89
90
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
# File 'livecheck/strategy.rb', line 86

def self.page_headers(url)
  headers = []

  [:default, :browser].each do |user_agent|
    args = [
      "--head",                 # Only work with the response headers
      "--request", "GET",       # Use a GET request (instead of HEAD)
      "--silent",               # Silent mode
      "--location",             # Follow redirects
      "--connect-timeout", "5", # Max time allowed for connection (secs)
      "--max-time", "10"        # Max time allowed for transfer (secs)
    ]

    stdout, _, status = curl_with_workarounds(
      *args, url,
      print_stdout: false, print_stderr: false,
      debug: false, verbose: false,
      user_agent: user_agent, retry: false
    )

    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