Class: Bibliothecary::Parsers::Pub

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

Constant Summary collapse

PLATFORM_NAME =
'Pub'

Class Method Summary collapse

Class Method Details

.analyse(folder_path, file_list) ⇒ Object



19
20
21
22
23
24
# File 'lib/bibliothecary/parsers/pub.rb', line 19

def self.analyse(folder_path, file_list)
  [
    analyse_yaml(folder_path, file_list),
    analyse_lockfile(folder_path, file_list)
  ]
end

.analyse_lockfile(folder_path, file_list) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bibliothecary/parsers/pub.rb', line 39

def self.analyse_lockfile(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^pubspec\.lock$/i) }
  return unless path

  manifest = YAML.load File.open(path).read

  {
    platform: PLATFORM_NAME,
    path: path,
    dependencies: parse_yaml_lockfile(manifest)
  }
end

.analyse_yaml(folder_path, file_list) ⇒ Object



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

def self.analyse_yaml(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^pubspec\.yaml$/i) }
  return unless path

  manifest = YAML.load File.open(path).read

  {
    platform: PLATFORM_NAME,
    path: path,
    dependencies: parse_yaml_manifest(manifest)
  }
end

.map_dependencies(hash, key, type) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/bibliothecary/parsers/pub.rb', line 67

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

.parse(filename, file_contents) ⇒ Object



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

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

.parse_yaml_lockfile(manifest) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/bibliothecary/parsers/pub.rb', line 57

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



52
53
54
55
# File 'lib/bibliothecary/parsers/pub.rb', line 52

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