Module: Homebrew::Livecheck::Packages Private

Extended by:
T::Helpers
Included in:
Homebrew::Livecheck
Defined in:
livecheck/packages.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.

Package names, references and URLs used by livecheck.

Instance Method Summary collapse

Instance Method Details

#checkable_urls(package_or_resource) ⇒ 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.

Returns an Array containing the formula/cask/resource URLs that can be used by livecheck.

Parameters:

Returns:



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'livecheck/packages.rb', line 180

def checkable_urls(package_or_resource)
  urls = []

  case package_or_resource
  when Formula
    if (stable = package_or_resource.stable)
      urls << stable.url
      urls.concat(stable.mirrors)
    end
    head = package_or_resource.head
    urls << head.url if head
    urls << package_or_resource.homepage if package_or_resource.homepage
  when Cask::Cask
    urls << package_or_resource.url.to_s if package_or_resource.url
    urls << package_or_resource.homepage if package_or_resource.homepage
  when Resource
    urls << package_or_resource.url
  else
    T.absurd(package_or_resource)
  end

  urls.compact.uniq
end

#current_version(package_or_resource) ⇒ Version?

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.

Parameters:

Returns:



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'livecheck/packages.rb', line 117

def current_version(package_or_resource)
  case package_or_resource
  when Formula
    if package_or_resource.head_only?
      commit = package_or_resource.any_installed_version&.version&.commit
      Version.new(commit) if commit
    else
      package_or_resource.stable&.version
    end
  when Cask::Cask
    Version.new(package_or_resource.version)
  when Resource
    package_or_resource.version
  else
    T.absurd(package_or_resource)
  end
end

#livecheck_url_to_string(livecheck_url, package_or_resource) ⇒ 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.

Parameters:

Returns:



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'livecheck/packages.rb', line 155

def livecheck_url_to_string(livecheck_url, package_or_resource)
  livecheck_url_string = case livecheck_url
  when String
    livecheck_url
  when :url
    if package_or_resource.is_a?(Cask::Cask) || package_or_resource.is_a?(Resource)
      package_or_resource.url&.to_s
    end
  when :head
    package_or_resource.head&.url if package_or_resource.is_a?(Formula)
  when :stable
    package_or_resource.stable&.url if package_or_resource.is_a?(Formula)
  when :homepage
    package_or_resource.homepage unless package_or_resource.is_a?(Resource)
  end

  if livecheck_url.is_a?(Symbol) && !livecheck_url_string
    raise ArgumentError, "`url #{livecheck_url.inspect}` does not reference a checkable URL"
  end

  livecheck_url_string
end

#load_other_tap_strategies(formulae_and_casks_to_check) ⇒ void

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.

This method returns an undefined value.

Uses formulae_and_casks_to_check to identify taps in use other than homebrew/core and homebrew/cask and loads strategies from them.

Parameters:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'livecheck/packages.rb', line 15

def load_other_tap_strategies(formulae_and_casks_to_check)
  other_taps = {}
  formulae_and_casks_to_check.each do |formula_or_cask|
    tap = formula_or_cask.tap
    next unless tap
    next if tap.core_tap?
    next if tap.core_cask_tap?
    next if other_taps[tap.name]

    other_taps[tap.name] = tap
  end
  other_taps = other_taps.sort.to_h

  other_taps.each_value do |tap|
    tap_strategy_path = "#{tap.path}/livecheck/strategy"
    Dir["#{tap_strategy_path}/*.rb"].each { require(it) } if Dir.exist?(tap_strategy_path)
  end
end

#package_or_resource_name(package_or_resource, full_name: false) ⇒ 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.

Parameters:

Returns:



136
137
138
139
140
141
142
143
144
145
146
147
# File 'livecheck/packages.rb', line 136

def package_or_resource_name(package_or_resource, full_name: false)
  case package_or_resource
  when Formula
    formula_name(package_or_resource, full_name:)
  when Cask::Cask
    cask_name(package_or_resource, full_name:)
  when Resource
    package_or_resource.name.to_s
  else
    T.absurd(package_or_resource)
  end
end

#resolve_livecheck_reference(formula_or_cask, first_formula_or_cask = formula_or_cask, references = [], full_name: false, debug: false) ⇒ Array<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.

Resolve formula/cask references in livecheck blocks to a final formula or cask.

Parameters:

Returns:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'livecheck/packages.rb', line 45

def resolve_livecheck_reference(
  formula_or_cask,
  first_formula_or_cask = formula_or_cask,
  references = [],
  full_name: false,
  debug: false
)
  # Check the `livecheck` block for a formula or cask reference
  livecheck = formula_or_cask.livecheck
  livecheck_formula = livecheck.formula
  livecheck_cask = livecheck.cask
  return [nil, references] if livecheck_formula.blank? && livecheck_cask.blank?

  # Load the referenced formula or cask
  referenced_formula_or_cask = Homebrew::API.with_no_api_env do
    if livecheck_formula
      Formulary.factory(livecheck_formula)
    elsif livecheck_cask
      Cask::CaskLoader.load(livecheck_cask)
    else
      raise "livecheck formula or cask not found"
    end
  end

  # Error if a `livecheck` block references a formula/cask that was already
  # referenced (or itself)
  if referenced_formula_or_cask == first_formula_or_cask ||
     referenced_formula_or_cask == formula_or_cask ||
     references.include?(referenced_formula_or_cask)
    if debug
      # Print the chain of references for debugging
      puts "Reference Chain:"
      puts package_or_resource_name(first_formula_or_cask, full_name:)

      references << referenced_formula_or_cask
      references.each do |ref_formula_or_cask|
        puts package_or_resource_name(ref_formula_or_cask, full_name:)
      end
    end

    raise "Circular formula/cask reference encountered"
  end
  references << referenced_formula_or_cask

  # Check the referenced formula/cask for a reference
  next_referenced_formula_or_cask, next_references = resolve_livecheck_reference(
    referenced_formula_or_cask,
    first_formula_or_cask,
    references,
    full_name:,
    debug:,
  )

  # Returning references along with the final referenced formula/cask
  # allows us to print the chain of references in the debug output
  [
    next_referenced_formula_or_cask || referenced_formula_or_cask,
    next_references,
  ]
end

#use_homebrew_curl?(formula_or_cask, 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.

livecheck should fetch a URL using brewed curl if the formula/cask contains a stable/url or head URL using: :homebrew_curl that shares the same host or uses it as a parent domain.

Parameters:

Returns:

  • (Boolean)


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'livecheck/packages.rb', line 208

def use_homebrew_curl?(formula_or_cask, url)
  host = url_host(url)
  return false unless host

  homebrew_curl_hosts = case formula_or_cask
  when Formula
    [formula_or_cask.stable, formula_or_cask.head].filter_map do |spec|
      next unless spec
      next if spec.using != :homebrew_curl
      next unless (spec_url = spec.url)

      url_host(spec_url)
    end
  when Cask::Cask
    cask_url = formula_or_cask.url
    return false if cask_url&.using != :homebrew_curl

    [url_host(cask_url.to_s)].compact
  end

  homebrew_curl_hosts.any? do |homebrew_curl_host|
    host == homebrew_curl_host ||
      host.end_with?(".#{homebrew_curl_host}") ||
      homebrew_curl_host.end_with?(".#{host}")
  end
end