Class: Bibliothecary::Parsers::Pub

Inherits:
Object
  • Object
show all
Includes:
Analyser
Defined in:
lib/bibliothecary/parsers/pub.rb

Class Method Summary collapse

Methods included from Analyser

included

Class Method Details

.map_dependencies(hash, key, type) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/bibliothecary/parsers/pub.rb', line 37

def self.map_dependencies(hash, key, type)
  hash.fetch(key,[]).map do |name, requirement|
    {
      name: name,
      requirement: requirement,
      type: type
    }
  end
end

.parse(filename, path) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/bibliothecary/parsers/pub.rb', line 8

def self.parse(filename, path)
  if filename.match(/^pubspec\.yaml$/i)
    file_contents = File.open(path).read
    yaml = YAML.load file_contents
    parse_yaml_manifest(yaml)
  elsif filename.match(/^pubspec\.lock$/i)
    file_contents = File.open(path).read
    yaml = YAML.load file_contents
    parse_yaml_lockfile(yaml)
  else
    []
  end
end

.parse_yaml_lockfile(manifest) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/bibliothecary/parsers/pub.rb', line 27

def self.parse_yaml_lockfile(manifest)
  manifest.fetch('packages', []).map do |name, dep|
    {
      name: name,
      requirement: dep['version'],
      type: 'runtime'
    }
  end
end

.parse_yaml_manifest(manifest) ⇒ Object



22
23
24
25
# File 'lib/bibliothecary/parsers/pub.rb', line 22

def self.parse_yaml_manifest(manifest)
  map_dependencies(manifest, 'dependencies', 'runtime') +
  map_dependencies(manifest, 'dev_dependencies', 'development')
end