| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
 | require 'dependency'
require 'dependencies'
require 'requirement'
require 'requirements'
require 'requirements/ld64_dependency'
require 'set'
## A dependency is a formula that another formula needs to install.
## A requirement is something other than a formula that another formula
## needs to be present. This includes external language modules,
## command-line tools in the path, or any arbitrary predicate.
##
## The `depends_on` method in the formula DSL is used to declare
## dependencies and requirements.
# This class is used by `depends_on` in the formula DSL to turn dependency
# specifications into the proper kinds of dependencies and requirements.
class DependencyCollector
  # Define the languages that we can handle as external dependencies.
  LANGUAGE_MODULES = Set[
    :chicken, :jruby, :lua, :node, :ocaml, :perl, :python, :python3, :rbx, :ruby
  ].freeze
  CACHE = {}
  def self.clear_cache
    CACHE.clear
  end
  attr_reader :deps, :requirements
  def initialize
    @deps = Dependencies.new
    @requirements = Requirements.new
  end
  def add(spec)
    case dep = fetch(spec)
    when Dependency
      @deps << dep
    when Requirement
      @requirements << dep
    end
    dep
  end
  def fetch(spec)
    CACHE.fetch(cache_key(spec)) { |key| CACHE[key] = build(spec) }
  end
  def cache_key(spec)
    if Resource === spec && spec.download_strategy == CurlDownloadStrategy
      File.extname(spec.url)
    else
      spec
    end
  end
  def build(spec)
    spec, tags = case spec
                 when Hash then destructure_spec_hash(spec)
                 else spec
                 end
    parse_spec(spec, Array(tags))
  end
  private
  def destructure_spec_hash(spec)
    spec.each { |o| return o }
  end
  def parse_spec(spec, tags)
    case spec
    when String
      parse_string_spec(spec, tags)
    when Resource
      resource_dep(spec, tags)
    when Symbol
      parse_symbol_spec(spec, tags)
    when Requirement, Dependency
      spec
    when Class
      parse_class_spec(spec, tags)
    else
      raise TypeError, "Unsupported type #{spec.class.name} for #{spec.inspect}"
    end
  end
  def parse_string_spec(spec, tags)
    if HOMEBREW_TAP_FORMULA_REGEX === spec
      TapDependency.new(spec, tags)
    elsif tags.empty?
      Dependency.new(spec, tags)
    elsif (tag = tags.first) && LANGUAGE_MODULES.include?(tag)
      LanguageModuleDependency.new(tag, spec, tags[1])
    else
      Dependency.new(spec, tags)
    end
  end
  def parse_symbol_spec(spec, tags)
    case spec
    when :x11        then X11Dependency.new(spec.to_s, tags)
    when :xcode      then XcodeDependency.new(tags)
    when :macos      then MinimumMacOSRequirement.new(tags)
    when :mysql      then MysqlDependency.new(tags)
    when :postgresql then PostgresqlDependency.new(tags)
    when :gpg        then GPGDependency.new(tags)
    when :fortran    then FortranDependency.new(tags)
    when :mpi        then MPIDependency.new(*tags)
    when :tex        then TeXDependency.new(tags)
    when :arch       then ArchRequirement.new(tags)
    when :hg         then MercurialDependency.new(tags)
    when :python     then PythonDependency.new(tags)
    when :python3    then Python3Dependency.new(tags)
    when :java       then JavaDependency.new(tags)
    when :ruby       then RubyRequirement.new(tags)
    when :osxfuse    then OsxfuseDependency.new(tags)
    when :tuntap     then TuntapDependency.new(tags)
    when :ant        then ant_dep(spec, tags)
    when :apr        then AprDependency.new(tags)
    # Tiger's ld is too old to properly link some software
    when :ld64       then LD64Dependency.new if MacOS.version < :leopard
    when :clt # deprecated
    when :autoconf, :automake, :bsdmake, :libtool # deprecated
      autotools_dep(spec, tags)
    when :cairo, :fontconfig, :freetype, :libpng, :pixman # deprecated
      Dependency.new(spec.to_s, tags)
    when :libltdl # deprecated
      tags << :run
      Dependency.new("libtool", tags)
    when :python2
      PythonDependency.new(tags)
    else
      raise ArgumentError, "Unsupported special dependency #{spec.inspect}"
    end
  end
  def parse_class_spec(spec, tags)
    if spec < Requirement
      spec.new(tags)
    else
      raise TypeError, "#{spec.inspect} is not a Requirement subclass"
    end
  end
  def autotools_dep(spec, tags)
    tags << :build unless tags.include? :run
    Dependency.new(spec.to_s, tags)
  end
  def ant_dep(spec, tags)
    if MacOS.version >= :mavericks
      Dependency.new(spec.to_s, tags)
    end
  end
  def resource_dep(spec, tags)
    tags << :build
    strategy = spec.download_strategy
    case
    when strategy <= CurlDownloadStrategy
      parse_url_spec(spec.url, tags)
    when strategy <= GitDownloadStrategy
      GitDependency.new(tags)
    when strategy <= MercurialDownloadStrategy
      MercurialDependency.new(tags)
    when strategy <= FossilDownloadStrategy
      Dependency.new("fossil", tags)
    when strategy <= BazaarDownloadStrategy
      Dependency.new("bazaar", tags)
    when strategy <= CVSDownloadStrategy
      Dependency.new("cvs", tags) if MacOS.version >= :mavericks || !MacOS::Xcode.provides_cvs?
    when strategy < AbstractDownloadStrategy
      # allow unknown strategies to pass through
    else
      raise TypeError,
        "#{strategy.inspect} is not an AbstractDownloadStrategy subclass"
    end
  end
  def parse_url_spec(url, tags)
    case File.extname(url)
    when '.xz'  then Dependency.new('xz', tags)
    when '.lz'  then Dependency.new('lzip', tags)
    when '.rar' then Dependency.new('unrar', tags)
    when '.7z'  then Dependency.new('p7zip', tags)
    end
  end
end
 |