diff options
| author | Jack Nagel | 2013-06-03 15:08:46 -0500 |
|---|---|---|
| committer | Jack Nagel | 2013-06-03 16:47:27 -0500 |
| commit | 3937d2bb848951d40a04bf2de7b0a1cf554d9846 (patch) | |
| tree | b393f64e12a84dcb84229a8130e235ccff024e69 /Library | |
| parent | ceb01c3124f76ad08b039d9377b2f27c568f70c5 (diff) | |
| download | brew-3937d2bb848951d40a04bf2de7b0a1cf554d9846.tar.bz2 | |
Refactor Requirement.expand
Diffstat (limited to 'Library')
| -rw-r--r-- | Library/Homebrew/requirement.rb | 96 |
1 files changed, 50 insertions, 46 deletions
diff --git a/Library/Homebrew/requirement.rb b/Library/Homebrew/requirement.rb index f8a6f056b..a5e936742 100644 --- a/Library/Homebrew/requirement.rb +++ b/Library/Homebrew/requirement.rb @@ -120,60 +120,64 @@ class Requirement end end - # Expand the requirements of dependent recursively, optionally yielding - # [dependent, req] 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 recommendeds based on what the dependent has asked for. - def self.expand(dependent, &block) - reqs = ComparableSet.new - - formulae = dependent.recursive_dependencies.map(&:to_formula) - formulae.unshift(dependent) - - formulae.map(&:requirements).each do |requirements| - requirements.each do |req| - prune = catch(:prune) do - if block_given? - yield dependent, req - elsif req.optional? || req.recommended? - Requirement.prune unless dependent.build.with?(req.name) + class << self + # Expand the requirements of dependent recursively, optionally yielding + # [dependent, req] 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 recommendeds based on what the dependent has asked for. + def expand(dependent, &block) + reqs = ComparableSet.new + + formulae = dependent.recursive_dependencies.map(&:to_formula) + formulae.unshift(dependent) + + formulae.map(&:requirements).each do |requirements| + requirements.each do |req| + if prune?(dependent, req, &block) + next + # TODO: Do this in a cleaner way, perhaps with another type of + # dependency type. + elsif req.class.default_formula + dependent.class.depends_on(req.class.default_formula) + next + else + reqs << req end end + end - next if prune - - # TODO: Do this in a cleaner way, perhaps with another type of - # dependency type. - if req.class.default_formula - dependent.class.depends_on(req.class.default_formula) - next - end - - reqs << req + # We special case handling of X11Dependency and its subclasses to + # ensure the correct dependencies are present in the final list. + # If an X11Dependency is present after filtering, we eliminate + # all X11Dependency::Proxy objects from the list. If there aren't + # any X11Dependency objects, then we eliminate all but one of the + # proxy objects. + proxy = unless reqs.any? { |r| r.instance_of?(X11Dependency) } + reqs.find { |r| r.kind_of?(X11Dependency::Proxy) } + end + + reqs.reject! do |r| + r.kind_of?(X11Dependency::Proxy) end - end - # We special case handling of X11Dependency and its subclasses to - # ensure the correct dependencies are present in the final list. - # If an X11Dependency is present after filtering, we eliminate - # all X11Dependency::Proxy objects from the list. If there aren't - # any X11Dependency objects, then we eliminate all but one of the - # proxy objects. - proxy = unless reqs.any? { |r| r.instance_of?(X11Dependency) } - reqs.find { |r| r.kind_of?(X11Dependency::Proxy) } + reqs << proxy unless proxy.nil? + reqs end - reqs.reject! do |r| - r.kind_of?(X11Dependency::Proxy) + def prune?(dependent, req, &block) + catch(:prune) do + if block_given? + yield dependent, req + elsif req.optional? || req.recommended? + prune unless dependent.build.with?(req.name) + end + end end - reqs << proxy unless proxy.nil? - reqs - end - - # Used to prune requirements when calling expand with a block. - def self.prune - throw(:prune, true) + # Used to prune requirements when calling expand with a block. + def prune + throw(:prune, true) + end end end |
