Class: Dependency Private

Inherits:
Object show all
Extended by:
Cachable, Forwardable
Includes:
Dependable
Defined in:
dependency.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.

A dependency on another Homebrew formula.

Direct Known Subclasses

UsesFromMacOSDependency

Constant Summary

Constants included from Dependable

Dependable::RESERVED_TAGS

Instance Attribute Summary collapse

Attributes included from Dependable

#tags

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cachable

cache, clear_cache

Methods included from Dependable

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

Constructor Details

#initialize(name, tags = [], env_proc = DEFAULT_ENV_PROC, option_names = [name&.split("/")&.last]) ⇒ 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.

Returns a new instance of Dependency.

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
# File 'dependency.rb', line 19

def initialize(name, tags = [], env_proc = DEFAULT_ENV_PROC, option_names = [name&.split("/")&.last])
  raise ArgumentError, "Dependency must have a name!" unless name

  @name = name
  @tags = tags
  @env_proc = env_proc
  @option_names = option_names

  @tap = Tap.fetch(Regexp.last_match(1), Regexp.last_match(2)) if name =~ HOMEBREW_TAP_FORMULA_REGEX
end

Instance Attribute Details

#env_procObject (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.



14
15
16
# File 'dependency.rb', line 14

def env_proc
  @env_proc
end

#nameObject (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.



14
15
16
# File 'dependency.rb', line 14

def name
  @name
end

#option_namesObject (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.



14
15
16
# File 'dependency.rb', line 14

def option_names
  @option_names
end

#tapObject (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.



14
15
16
# File 'dependency.rb', line 14

def tap
  @tap
end

Class Method Details

._load(marshaled) ⇒ 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.



112
113
114
# File 'dependency.rb', line 112

def self._load(marshaled)
  new(*Marshal.load(marshaled)) # rubocop:disable Security/MarshalLoad
end

.action(dependent, dep, &block) ⇒ 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.



170
171
172
173
174
175
176
177
178
# File 'dependency.rb', line 170

def action(dependent, dep, &block)
  catch(:action) do
    if block
      yield dependent, dep
    elsif dep.optional? || dep.recommended?
      prune unless dependent.build.with?(dep)
    end
  end
end

.expand(dependent, deps = dependent.deps, cache_key: nil, &block) ⇒ 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.

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



127
128
129
130
131
132
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
# File 'dependency.rb', line 127

def expand(dependent, deps = dependent.deps, cache_key: nil, &block)
  # Keep track dependencies to avoid infinite cyclic dependency recursion.
  @expand_stack ||= []
  @expand_stack.push dependent.name

  if cache_key.present?
    cache[cache_key] ||= {}
    return cache[cache_key][cache_id dependent].dup if cache[cache_key][cache_id dependent]
  end

  expanded_deps = []

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

    case action(dependent, dep, &block)
    when :prune
      next
    when :skip
      next if @expand_stack.include? dep.name

      expanded_deps.concat(expand(dep.to_formula, cache_key: cache_key, &block))
    when :keep_but_prune_recursive_deps
      expanded_deps << dep
    else
      next if @expand_stack.include? dep.name

      dep_formula = dep.to_formula
      expanded_deps.concat(expand(dep_formula, cache_key: cache_key, &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[cache_key][cache_id dependent] = expanded_deps.dup if cache_key.present?
  expanded_deps
ensure
  @expand_stack.pop
end

.keep_but_prune_recursive_depsvoid

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.

Keep a dependency, but prune its dependencies.



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

def keep_but_prune_recursive_deps
  throw(:action, :keep_but_prune_recursive_deps)
end

.merge_repeats(all) ⇒ 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.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'dependency.rb', line 198

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

  all.map(&:name).uniq.map do |name|
    deps = grouped.fetch(name)
    dep  = deps.first
    tags = merge_tags(deps)
    option_names = deps.flat_map(&:option_names).uniq
    kwargs = {}
    kwargs[:bounds] = dep.bounds if dep.uses_from_macos?
    # TODO: simpify to just **kwargs when we require Ruby >= 2.7
    if kwargs.empty?
      dep.class.new(name, tags, dep.env_proc, option_names)
    else
      dep.class.new(name, tags, dep.env_proc, option_names, **kwargs)
    end
  end
end

.prunevoid

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.

Prune a dependency and its dependencies recursively.



182
183
184
# File 'dependency.rb', line 182

def prune
  throw(:action, :prune)
end

.skipvoid

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.

Prune a single dependency but do not prune its dependencies.



188
189
190
# File 'dependency.rb', line 188

def skip
  throw(:action, :skip)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

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.



34
35
36
# File 'dependency.rb', line 34

def ==(other)
  instance_of?(other.class) && name == other.name && tags == other.tags
end

#_dumpObject

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.

Define marshaling semantics because we cannot serialize @env_proc.



108
109
110
# File 'dependency.rb', line 108

def _dump(*)
  Marshal.dump([name, tags])
end

#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)


117
118
119
# File 'dependency.rb', line 117

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

#hashObject

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.



39
40
41
# File 'dependency.rb', line 39

def hash
  [name, tags].hash
end

#inspectString

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:



103
104
105
# File 'dependency.rb', line 103

def inspect
  "#<#{self.class.name}: #{name.inspect} #{tags.inspect}>"
end

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

Returns:

  • (Boolean)


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
# File 'dependency.rb', line 50

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

  return true if formula.latest_version_installed?

  return false if minimum_version.blank?

  installed_version = formula.any_installed_version
  return false unless installed_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(inherited_options) ⇒ 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.



84
85
86
87
88
89
90
91
# File 'dependency.rb', line 84

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

#modify_build_environmentObject

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.



93
94
95
# File 'dependency.rb', line 93

def modify_build_environment
  env_proc&.call
end

#satisfied?(inherited_options = [], minimum_version: nil, minimum_revision: 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.

Returns:

  • (Boolean)


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

def satisfied?(inherited_options = [], minimum_version: nil, minimum_revision: nil)
  installed?(minimum_version: minimum_version, minimum_revision: minimum_revision) &&
    missing_options(inherited_options).empty?
end

#to_formulaObject

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.



43
44
45
46
47
# File 'dependency.rb', line 43

def to_formula
  formula = Formulary.factory(name)
  formula.build = BuildOptions.new(options, formula.options)
  formula
end

#to_sObject

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.



30
31
32
# File 'dependency.rb', line 30

def to_s
  name
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)


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

def uses_from_macos?
  false
end