Class: Bibliothecary::Parsers::Packagist

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

Constant Summary collapse

PLATFORM_NAME =
'Packagist'

Class Method Summary collapse

Class Method Details

.analyse(folder_path, file_list) ⇒ Object



19
20
21
22
# File 'lib/bibliothecary/parsers/packagist.rb', line 19

def self.analyse(folder_path, file_list)
  [analyse_composer_json(folder_path, file_list),
  analyse_composer_lock(folder_path, file_list)]
end

.analyse_composer_json(folder_path, file_list) ⇒ Object



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

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

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

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

.analyse_composer_lock(folder_path, file_list) ⇒ Object



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

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

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

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

.map_dependencies(hash, key, type) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/bibliothecary/parsers/packagist.rb', line 69

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/packagist.rb', line 8

def self.parse(filename, file_contents)
  json = JSON.parse(file_contents)
  if filename.match(/^composer\.json$/)
    parse_manifest(json)
  elsif filename.match(/^composer\.lock$/)
    parse_lockfile(json)
  else
    []
  end
end

.parse_lockfile(manifest) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/bibliothecary/parsers/packagist.rb', line 54

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

.parse_manifest(manifest) ⇒ Object



64
65
66
67
# File 'lib/bibliothecary/parsers/packagist.rb', line 64

def self.parse_manifest(manifest)
  map_dependencies(manifest, 'require', 'runtime') +
  map_dependencies(manifest, 'require-dev', 'development')
end