Class: Dependency

Inherits:
Object show all
Includes:
Dependable
Defined in:
dependency.rb

Overview

This class is part of an internal API. This class may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

A dependency on another Homebrew formula.

Direct Known Subclasses

UsesFromMacOSDependency

Constant Summary

Constants included from Dependable

Dependable::KEEP_BUT_PRUNE_RECURSIVE_DEPS, Dependable::PRUNE, Dependable::RESERVED_TAGS, Dependable::SKIP

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dependable

#build?, #implicit?, #no_linkage?, #option_tags, #optional?, #options, #prune_from_option?, #prune_if_build_and_not_dependent?, #recommended?, #required?, #test?

Constructor Details

#initialize(name, tags = []) ⇒ 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.

Parameters:



26
27
28
29
30
31
32
33
34
# File 'dependency.rb', line 26

def initialize(name, tags = [])
  @name = name
  @tags = T.let(Array(tags), T::Array[T.any(Symbol, String)])
  @tap = T.let(nil, T.nilable(Tap))

  return unless (tap_with_name = Tap.with_formula_name(name))

  @tap, = tap_with_name
end

Class Attribute Details

.expand_stack ⇒ Array<String, Symbol>? (readonly)

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:



232
233
234
# File 'dependency.rb', line 232

def expand_stack
  @expand_stack
end

Instance Attribute Details

#name ⇒ String (readonly)

The name used in the formula's dependency declaration.

Returns:



17
18
19
# File 'dependency.rb', line 17

def name
  @name
end

#tags ⇒ Array<Symbol, String, Array<T.untyped>> (readonly)

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:



23
24
25
# File 'dependency.rb', line 23

def tags
  @tags
end

#tap ⇒ Tap? (readonly)

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:



20
21
22
# File 'dependency.rb', line 20

def tap
  @tap
end

Class Method Details

.action(dependent, dep, &block) ⇒ Symbol?

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.

CaskDependent may not be initialized yet, so we don't use a runtime sig

Parameters:

Returns:



314
315
316
317
318
319
320
# File 'dependency.rb', line 314

def action(dependent, dep, &block)
  if block
    yield dependent, dep
  elsif dep.optional? || dep.recommended?
    Dependable::PRUNE unless T.cast(dependent, Formula).build.with?(dep)
  end
end

.cache(key, cache_timestamp: nil) ⇒ Hash{String, 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.

Parameters:

  • key (String, nil)
  • cache_timestamp (Time, nil) (defaults to: nil)

Returns:



339
340
341
342
343
344
345
346
347
348
349
# File 'dependency.rb', line 339

def cache(key, cache_timestamp: nil)
  @cache = T.let(@cache, T.nilable(T::Hash[Symbol, T.untyped]))
  @cache ||= { timestamped: {}, not_timestamped: {} }

  if cache_timestamp
    @cache[:timestamped][cache_timestamp] ||= {}
    @cache[:timestamped][cache_timestamp][key] ||= {}
  else
    @cache[:not_timestamped][key] ||= {}
  end
end

.clear_cache ⇒ 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.



352
353
354
355
356
357
358
# File 'dependency.rb', line 352

def clear_cache
  return unless @cache

  # No need to clear the timestamped cache as it's timestamped, and doing so causes problems in `expand`.
  # See https://github.com/Homebrew/brew/pull/20896#issuecomment-3419257460
  @cache[:not_timestamped].clear
end

.delete_timestamped_cache_entry(key, cache_timestamp) ⇒ 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.

Parameters:



361
362
363
364
365
366
367
# File 'dependency.rb', line 361

def delete_timestamped_cache_entry(key, cache_timestamp)
  return unless @cache
  return unless (timestamp_entry = @cache[:timestamped][cache_timestamp])

  timestamp_entry.delete(key)
  @cache[:timestamped].delete(cache_timestamp) if timestamp_entry.empty?
end

.expand(dependent, deps = dependent.deps, cache_key: nil, root_cache_key: cache_key, cache_timestamp: nil, formula_cache: nil, &block) ⇒ Array<Dependency>

This method is part of an internal API. This method may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Expand the dependencies of each dependent recursively, optionally yielding [dependent, dep] pairs to allow callers to apply arbitrary filters to the list. The default filter, which is applied when a block is not given, omits optionals and recommends based on what the dependent has asked for root_cache_key caches the expansion of dependent itself and defaults to cache_key, which is used for every recursive expansion.

Parameters:

Returns:

Raises:

  • (ArgumentError)


256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'dependency.rb', line 256

def expand(dependent, deps = dependent.deps, cache_key: nil, root_cache_key: cache_key, cache_timestamp: nil,
           formula_cache: nil, &block)
  raise ArgumentError, "formula_cache requires cache_key" if formula_cache && cache_key.blank?

  # Keep track dependencies to avoid infinite cyclic dependency recursion.
  @expand_stack ||= T.let([], T.nilable(T::Array[T.any(String, Symbol)]))
  @expand_stack.push dependent.name

  begin
    if root_cache_key.present? && (entry = cache(root_cache_key, cache_timestamp:)[cache_id dependent])
      return entry.dup
    end

    expanded_deps = []

    deps.each do |dep|
      next if dependent.name == dep.name

      case action(dependent, dep, &block)
      when Dependable::PRUNE
        next
      when Dependable::SKIP
        next if @expand_stack.include? dep.name

        expanded_deps.concat(expand(formula_for_dependency(dep, formula_cache),
                                    cache_key:, cache_timestamp:, formula_cache:,
                                    &block))
      when Dependable::KEEP_BUT_PRUNE_RECURSIVE_DEPS
        expanded_deps << dep
      else
        next if @expand_stack.include? dep.name

        dep_formula = formula_for_dependency(dep, formula_cache)
        expanded_deps.concat(expand(dep_formula, cache_key:, cache_timestamp:, formula_cache:, &block))

        # Fixes names for renamed/aliased formulae.
        dep = dep.dup_with_formula_name(dep_formula)
        expanded_deps << dep
      end
    end

    expanded_deps = merge_repeats(expanded_deps)
    cache(root_cache_key, cache_timestamp:)[cache_id dependent] = expanded_deps.dup if root_cache_key.present?
    expanded_deps
  ensure
    @expand_stack.pop
  end
end

.merge_repeats(all) ⇒ Array<Dependency>

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:



323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'dependency.rb', line 323

def merge_repeats(all)
  grouped = all.group_by(&:name)

  all.map(&:name).uniq.filter_map do |name|
    deps = grouped.fetch(name)
    dep  = deps.first
    next unless dep

    tags = merge_tags(deps)
    kwargs = {}
    kwargs[:bounds] = T.cast(dep, UsesFromMacOSDependency).bounds if dep.uses_from_macos?
    dep.class.new(name, tags, **kwargs)
  end
end

Instance Method Details

#dup_with_formula_name(formula) ⇒ T.self_type

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:

  • (T.self_type)


226
227
228
# File 'dependency.rb', line 226

def dup_with_formula_name(formula)
  self.class.new(formula.full_name.to_s, tags)
end

#formula_loaders ⇒ Array<Formulary::FormulaLoader>

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.



97
98
99
100
101
# File 'dependency.rb', line 97

def formula_loaders
  [Formulary.loader_for(name, warn: false)]
rescue TapFormulaAmbiguityError => e
  e.loaders
end

#formula_name? ⇒ 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 #name is a formula name rather than a path, URL or bottle that Formulary would load. Names from install receipts and bottle manifests can be written by sandboxed package code.

Returns:

  • (Boolean)


118
119
120
121
122
123
# File 'dependency.rb', line 118

def formula_name?
  formula_name = tap ? Utils.name_from_full_name(name) : name
  formula_name.match?(/\A#{HOMEBREW_TAP_FORMULA_NAME_REGEX}\z/o) &&
    Utils::INVALID_PATH_COMPONENTS.exclude?(formula_name) &&
    File.extname(formula_name) != ".rb" && !formula_name.match?(HOMEBREW_BOTTLES_EXTNAME_REGEX)
end

#installed?(minimum_version: nil, minimum_revision: nil, minimum_compatibility_version: nil, bottle_os_version: nil) ⇒ 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.

Parameters:

  • minimum_version (Version, nil) (defaults to: nil)
  • minimum_revision (Integer, nil) (defaults to: nil)
  • minimum_compatibility_version (Integer, nil) (defaults to: nil)
  • bottle_os_version (String, nil) (defaults to: nil)

Returns:

  • (Boolean)


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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'dependency.rb', line 133

def installed?(minimum_version: nil, minimum_revision: nil, minimum_compatibility_version: nil,
               bottle_os_version: nil)
  formula = begin
    to_installed_formula
  rescue FormulaUnavailableError
    nil
  end
  return false unless formula

  # If the opt prefix doesn't exist: we likely have an incomplete installation.
  return false unless formula.opt_prefix.exist?

  return true if formula.latest_version_installed?

  return false if minimum_version.blank? && minimum_compatibility_version.blank?

  installed_keg = formula.any_installed_keg
  return false unless installed_keg

  # If the keg name doesn't match, we may have moved from an alias to a full formula and need to upgrade.
  return false unless formula.possible_names.include?(installed_keg.name)

  # If both the formula and minimum dependency have a compatibility_version set,
  # and they match, the dependency is satisfied regardless of version/revision.
  if minimum_compatibility_version.present? && formula.compatibility_version.present?
    installed_tab = Tab.for_keg(installed_keg)
    installed_compatibility_version = installed_tab.source.dig("versions", "compatibility_version")

    # If installed version has same compatibility_version as required, it's compatible
    return true if installed_compatibility_version == minimum_compatibility_version &&
                   formula.compatibility_version == minimum_compatibility_version
  end

  return false if minimum_version.blank?

  installed_version = installed_keg.version

  # Tabs prior to 4.1.18 did not have revision or pkg_version fields.
  # As a result, we have to be more conversative when we do not have
  # a minimum revision from the tab and assume that if the formula has a
  # the same version and a non-zero revision that it needs upgraded.
  if minimum_revision.present?
    minimum_pkg_version = PkgVersion.new(minimum_version, minimum_revision)
    installed_version >= minimum_pkg_version
  elsif installed_version.version == minimum_version
    formula.revision.zero?
  else
    installed_version.version > minimum_version
  end
end

#missing_options ⇒ Options

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:



199
200
201
202
203
204
205
# File 'dependency.rb', line 199

def missing_options
  formula = to_installed_formula
  required = options
  required &= formula.options.to_a
  required -= Tab.for_formula(formula).used_options
  required
end

#opt_bin ⇒ Pathname

The bin directory under #opt_prefix.

Returns:



59
# File 'dependency.rb', line 59

def opt_bin = Utils::Path.formula_opt_bin(name)

#opt_include ⇒ Pathname

The include directory under #opt_prefix.

Returns:



69
# File 'dependency.rb', line 69

def opt_include = Utils::Path.formula_opt_include(name)

#opt_lib ⇒ Pathname

The lib directory under #opt_prefix.

Returns:



64
# File 'dependency.rb', line 64

def opt_lib = Utils::Path.formula_opt_lib(name)

#opt_libexec ⇒ Pathname

The libexec directory under #opt_prefix.

Returns:



74
# File 'dependency.rb', line 74

def opt_libexec = Utils::Path.formula_opt_libexec(name)

#opt_prefix ⇒ Pathname

The stable install path for the declared dependency name.

Returns:



54
# File 'dependency.rb', line 54

def opt_prefix = Utils::Path.formula_opt_prefix(name)

#option_names ⇒ 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:



208
209
210
# File 'dependency.rb', line 208

def option_names
  [Utils.name_from_full_name(name)].freeze
end

#satisfied?(minimum_version: nil, minimum_revision: nil, minimum_compatibility_version: nil, bottle_os_version: nil) ⇒ 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.

Parameters:

  • minimum_version (Version, nil) (defaults to: nil)
  • minimum_revision (Integer, nil) (defaults to: nil)
  • minimum_compatibility_version (Integer, nil) (defaults to: nil)
  • bottle_os_version (String, nil) (defaults to: nil)

Returns:

  • (Boolean)


192
193
194
195
196
# File 'dependency.rb', line 192

def satisfied?(minimum_version: nil, minimum_revision: nil,
               minimum_compatibility_version: nil, bottle_os_version: nil)
  installed?(minimum_version:, minimum_revision:, minimum_compatibility_version:, bottle_os_version:) &&
    missing_options.empty?
end

#to_formula ⇒ Formula

Loads the formula with the options declared on this dependency.

Returns:

Raises:



88
89
90
91
92
93
94
# File 'dependency.rb', line 88

def to_formula
  raise FormulaUnavailableError, name unless formula_name?

  formula = Formulary.factory(name, warn: false)
  formula.build = BuildOptions.new(options, formula.options)
  formula
end

#to_installed_formula ⇒ Formula

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:

Raises:



77
78
79
80
81
82
83
# File 'dependency.rb', line 77

def to_installed_formula
  raise FormulaUnavailableError, name unless formula_name?

  formula = Formulary.resolve(name)
  formula.build = BuildOptions.new(options, formula.options)
  formula
end

#untrusted? ⇒ 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.

Returns:

  • (Boolean)


104
105
106
107
108
109
110
111
112
113
# File 'dependency.rb', line 104

def untrusted?
  return false if Homebrew::EnvConfig.no_require_tap_trust?

  formula_loaders.any? do |loader|
    tap = loader.tap
    next false unless tap

    !Homebrew::Trust.trusted?(:formula, "#{tap}/#{loader.name}")
  end
end

#uses_from_macos? ⇒ 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.

Returns:

  • (Boolean)


213
214
215
# File 'dependency.rb', line 213

def uses_from_macos?
  false
end