brew/Library/Homebrew/dependency.rb

193 lines
4.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "dependable"
# A dependency on another Homebrew formula.
class Dependency
extend Forwardable
include Dependable
attr_reader :name, :tags, :env_proc, :option_names
2019-04-19 21:46:20 +09:00
DEFAULT_ENV_PROC = proc {}.freeze
def initialize(name, tags = [], env_proc = DEFAULT_ENV_PROC, option_names = [name])
raise ArgumentError, "Dependency must have a name!" unless name
@name = name
@tags = tags
@env_proc = env_proc
@option_names = option_names
end
def to_s
name
end
def ==(other)
2014-11-21 16:08:37 -06:00
instance_of?(other.class) && name == other.name && tags == other.tags
end
2016-09-23 18:13:48 +02:00
alias eql? ==
def hash
2014-11-21 16:08:37 -06:00
name.hash ^ tags.hash
end
def to_formula
formula = Formulary.factory(name)
formula.build = BuildOptions.new(options, formula.options)
formula
end
def installed?
to_formula.latest_version_installed?
end
def satisfied?(inherited_options)
installed? && missing_options(inherited_options).empty?
end
2014-08-23 22:41:35 -05:00
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
def modify_build_environment
2017-09-24 19:24:46 +01:00
env_proc&.call
end
2013-06-07 20:36:13 -05:00
def inspect
"#<#{self.class.name}: #{name.inspect} #{tags.inspect}>"
2013-06-07 20:36:13 -05:00
end
# Define marshaling semantics because we cannot serialize @env_proc
def _dump(*)
Marshal.dump([name, tags])
end
def self._load(marshaled)
new(*Marshal.load(marshaled)) # rubocop:disable Security/MarshalLoad
end
2013-05-10 23:45:05 -05:00
class << self
# Expand the dependencies of dependent recursively, optionally yielding
# `[dependent, dep]` pairs to allow callers to apply arbitrary filters to
2013-05-10 23:45:05 -05:00
# the list.
# The default filter, which is applied when a block is not given, omits
# optionals and recommendeds based on what the dependent has asked for.
def expand(dependent, deps = dependent.deps, &block)
# Keep track dependencies to avoid infinite cyclic dependency recursion.
@expand_stack ||= []
@expand_stack.push dependent.name
expanded_deps = []
deps.each do |dep|
next if dependent.name == dep.name
2013-07-22 21:36:11 -05:00
case action(dependent, dep, &block)
when :prune
next
2013-07-22 21:36:11 -05:00
when :skip
next if @expand_stack.include? dep.name
2018-09-17 02:45:00 +02:00
expanded_deps.concat(expand(dep.to_formula, &block))
when :keep_but_prune_recursive_deps
expanded_deps << dep
2013-05-10 23:45:05 -05:00
else
next if @expand_stack.include? dep.name
2018-09-17 02:45:00 +02:00
expanded_deps.concat(expand(dep.to_formula, &block))
expanded_deps << dep
2013-05-10 23:45:05 -05:00
end
end
merge_repeats(expanded_deps)
ensure
@expand_stack.pop
2013-05-10 23:45:05 -05:00
end
def action(dependent, dep, &_block)
2013-07-22 21:36:11 -05:00
catch(:action) do
if block_given?
yield dependent, dep
elsif dep.optional? || dep.recommended?
prune unless dependent.build.with?(dep)
end
end
2013-05-10 23:45:05 -05:00
end
2013-07-22 21:48:53 -05:00
# Prune a dependency and its dependencies recursively
2013-05-10 23:45:05 -05:00
def prune
2013-07-22 21:36:11 -05:00
throw(:action, :prune)
end
2013-07-22 21:48:53 -05:00
# Prune a single dependency but do not prune its dependencies
2013-07-22 21:36:11 -05:00
def skip
throw(:action, :skip)
2013-05-10 23:45:05 -05:00
end
# Keep a dependency, but prune its dependencies
def keep_but_prune_recursive_deps
throw(:action, :keep_but_prune_recursive_deps)
end
2014-11-21 16:08:37 -06:00
def merge_repeats(all)
grouped = all.group_by(&:name)
all.map(&:name).uniq.map do |name|
deps = grouped.fetch(name)
2014-11-21 16:08:37 -06:00
dep = deps.first
tags = merge_tags(deps)
option_names = deps.flat_map(&:option_names).uniq
dep.class.new(name, tags, dep.env_proc, option_names)
end
end
private
def merge_tags(deps)
other_tags = deps.flat_map(&:option_tags).uniq
other_tags << :test if deps.flat_map(&:tags).include?(:test)
merge_necessity(deps) + merge_temporality(deps) + other_tags
end
def merge_necessity(deps)
# Cannot use `deps.any?(&:required?)` here due to its definition.
if deps.any? { |dep| !dep.recommended? && !dep.optional? }
[] # Means required dependency.
elsif deps.any?(&:recommended?)
[:recommended]
else # deps.all?(&:optional?)
[:optional]
end
end
def merge_temporality(deps)
# Means both build and runtime dependency.
return [] unless deps.all?(&:build?)
2018-09-17 02:45:00 +02:00
[:build]
end
end
end
2014-02-28 11:16:55 -06:00
class TapDependency < Dependency
2014-12-29 12:20:03 +00:00
attr_reader :tap
def initialize(name, tags = [], env_proc = DEFAULT_ENV_PROC, option_names = [name.split("/").last])
@tap = Tap.fetch(name.rpartition("/").first)
super(name, tags, env_proc, option_names)
2014-02-28 11:16:55 -06:00
end
def installed?
super
rescue FormulaUnavailableError
false
end
end