Class: Bibliothecary::Parsers::Elm

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

Class Method Summary collapse

Methods included from Analyser

included

Class Method Details

.analyse(folder_path, file_list) ⇒ Object



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

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



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

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



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

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



71
72
73
74
75
76
77
78
79
# File 'lib/bibliothecary/parsers/elm.rb', line 71

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

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

.parse_json_lock(manifest) ⇒ Object



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

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

.parse_json_manifest(manifest) ⇒ Object



57
58
59
# File 'lib/bibliothecary/parsers/elm.rb', line 57

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