Class: Bibliothecary::Parsers::Elm

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

Constant Summary collapse

PLATFORM_NAME =
'elm'

Class Method Summary collapse

Class Method Details

.analyse(folder_path, file_list) ⇒ Object



20
21
22
23
# File 'lib/bibliothecary/parsers/elm.rb', line 20

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

.analyse_json(folder_path, file_list) ⇒ Object



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

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

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

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

.analyse_json_lock(folder_path, file_list) ⇒ Object



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

def self.analyse_json_lock(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^elm-stuff\/exact-dependencies\.json$/) }
  return unless path

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

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

.map_dependencies(hash, key, type) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/bibliothecary/parsers/elm.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
18
# File 'lib/bibliothecary/parsers/elm.rb', line 8

def self.parse(filename, file_contents)
  if filename.match(/^elm-package\.json$|^elm_dependencies\.json$/)
    json = JSON.parse file_contents
    parse_json_manifest(json)
  elsif filename.match(/^elm-stuff\/exact-dependencies\.json$/)
    json = JSON.parse file_contents
    parse_json_lock(json)
  else
    []
  end
end

.parse_json_lock(manifest) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/bibliothecary/parsers/elm.rb', line 59

def self.parse_json_lock(manifest)
  manifest.map do |name, requirement|
    {
      name: name,
      requirement: requirement,
      type: 'runtime'
    }
  end
end

.parse_json_manifest(manifest) ⇒ Object



55
56
57
# File 'lib/bibliothecary/parsers/elm.rb', line 55

def self.parse_json_manifest(manifest)
  map_dependencies(manifest, 'dependencies', 'runtime')
end