Class: Bibliothecary::Parsers::CPAN

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

Constant Summary collapse

PLATFORM_NAME =
'cpan'

Class Method Summary collapse

Class Method Details

.analyse(folder_path, file_list) ⇒ Object



21
22
23
24
# File 'lib/bibliothecary/parsers/cpan.rb', line 21

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

.analyse_json(folder_path, file_list) ⇒ Object



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

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

  manifest = JSON.parse File.open(path).read

  {
    platform: PLATFORM_NAME,
    path: path,
    dependencies: parse_json_manifest(manifest)
  }
rescue
  []
end

.analyse_yaml(folder_path, file_list) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bibliothecary/parsers/cpan.rb', line 41

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

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

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

.map_dependencies(hash, key, type) ⇒ Object



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

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



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

def self.parse(filename, file_contents)
  if filename.match(/^META\.json$/i)
    json = JSON.parse file_contents
    parse_json_manifest(json)
  elsif filename.match(/^META\.yml$/i)
    yaml = YAML.load file_contents
    parse_yaml_manifest(yaml)
  else
    []
  end
end

.parse_json_manifest(manifest) ⇒ Object



56
57
58
59
60
# File 'lib/bibliothecary/parsers/cpan.rb', line 56

def self.parse_json_manifest(manifest)
  manifest['prereqs'].map do |group, deps|
    map_dependencies(deps, 'requires', 'runtime')
  end.flatten
end

.parse_yaml_manifest(manifest) ⇒ Object



62
63
64
# File 'lib/bibliothecary/parsers/cpan.rb', line 62

def self.parse_yaml_manifest(manifest)
  map_dependencies(manifest, 'requires', 'runtime')
end