Module: Bibliothecary::Analyser::ClassMethods

Defined in:
lib/bibliothecary/analyser.rb

Instance Method Summary collapse

Instance Method Details

#analyse(folder_path, file_list) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/bibliothecary/analyser.rb', line 44

def analyse(folder_path, file_list)
  file_list.map do |path|
    filename = path.gsub(folder_path, '').gsub(/^\//, '')
    contents = File.open(path).read
    analyse_contents(filename, contents)
  end.compact
end

#analyse_contents(filename, contents) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bibliothecary/analyser.rb', line 52

def analyse_contents(filename, contents)
  begin
    dependencies = parse_file(filename, contents)
    if dependencies && dependencies.any?
      {
        platform: platform_name,
        path: filename,
        dependencies: dependencies,
        kind: determine_kind(filename)
      }
    else
      nil
    end
  rescue
    nil
  end
end

#determine_kind(filename) ⇒ Object



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

def determine_kind(filename)
  mapping.each do |regex, details|
    if filename.match(regex)
      return details[:kind]
    end
  end
  return []
end

#map_dependencies(hash, key, type) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/bibliothecary/analyser.rb', line 34

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

#match?(filename) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/bibliothecary/analyser.rb', line 16

def match?(filename)
  mapping.keys.any?{|regex| filename.match(regex) }
end

#parse_file(filename, contents) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/bibliothecary/analyser.rb', line 7

def parse_file(filename, contents)
  mapping.each do |regex, details|
    if filename.match(regex)
      return send(details[:parser], contents)
    end
  end
  return []
end

#parse_json_runtime_manifest(file_contents) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/bibliothecary/analyser.rb', line 24

def parse_json_runtime_manifest(file_contents)
  JSON.parse(file_contents).fetch('dependencies',[]).map do |name, requirement|
    {
      name: name,
      requirement: requirement,
      type: 'runtime'
    }
  end
end

#parse_ruby_manifest(manifest) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/bibliothecary/analyser.rb', line 79

def parse_ruby_manifest(manifest)
  manifest.dependencies.inject([]) do |deps, dep|
    deps.push({
      name: dep.name,
      requirement: dep.requirement.to_s,
      type: dep.type
    })
  end.uniq
end

#platform_nameObject



20
21
22
# File 'lib/bibliothecary/analyser.rb', line 20

def platform_name
  self.name.to_s.split('::').last.downcase
end