Class: BrewDG::DependencyManifest

Inherits:
Object
  • Object
show all
Defined in:
lib/brew_dg/dependency_manifest.rb

Constant Summary collapse

DEPENDENCY_TYPES =
%w(Build Required Recommended Optional)

Class Method Summary collapse

Class Method Details

.for_package(package) ⇒ Object



9
10
11
# File 'lib/brew_dg/dependency_manifest.rb', line 9

def self.for_package(package)
  %x(brew info #{package}).lines.map(&method(:from_output)).compact
end

.from_output(line) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/brew_dg/dependency_manifest.rb', line 13

def self.from_output(line)
  type_match = line.match(/^(\w+):/)
  type = type_match && type_match.captures.first

  if DEPENDENCY_TYPES.include?(type)
    manifest_header = "#{type}: "
    manifest_parts = line.split(manifest_header)

    if manifest_parts.size == 2
      manifest_body = manifest_parts.last
      dependencies = manifest_body.split(', ').map do |dependency|
        dependency.scan(/\w|\s|-|_/).join.strip
      end
    else
      dependencies = []
    end

    new(type: type.downcase.intern, dependencies: dependencies)
  end
end