Class: Bibliothecary::Runner

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

Overview

A class that allows bibliothecary to run with multiple configurations at once, rather than with one global

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Runner

Returns a new instance of Runner.



5
6
7
# File 'lib/bibliothecary/runner.rb', line 5

def initialize(configuration)
  @configuration = configuration
end

Instance Method Details

#analyse(path, ignore_unparseable_files: true) ⇒ Object Also known as: analyze



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bibliothecary/runner.rb', line 9

def analyse(path, ignore_unparseable_files: true)
  info_list = load_file_info_list(path)

  info_list = info_list.reject { |info| info.package_manager.nil? } if ignore_unparseable_files

  # Each package manager needs to see its entire list so it can
  # associate related manifests and lockfiles for example.
  analyses = package_managers.map do |pm|
    matching_infos = info_list.select { |info| info.package_manager == pm }
    pm.analyse_file_info(matching_infos)
  end
  analyses = analyses.flatten.compact

  info_list.select { |info| info.package_manager.nil? }.each do |info|
    analyses.push(Bibliothecary::Analyser::create_error_analysis('unknown', info.relative_path, 'unknown',
                                                                 'No parser for this file type'))
  end

  analyses
end

#analyse_file(file_path, contents) ⇒ Object Also known as: analyze_file



103
104
105
106
107
# File 'lib/bibliothecary/runner.rb', line 103

def analyse_file(file_path, contents)
  package_managers.select { |pm| pm.match?(file_path, contents) }.map do |pm|
    pm.analyse_contents(file_path, contents)
  end.flatten.uniq.compact
end

#applicable_package_managers(info) ⇒ Object



36
37
38
39
# File 'lib/bibliothecary/runner.rb', line 36

def applicable_package_managers(info)
  managers = package_managers.select { |pm| pm.match_info?(info) }
  managers.length > 0 ? managers : [nil]
end

#find_manifests(path) ⇒ Object



91
92
93
# File 'lib/bibliothecary/runner.rb', line 91

def find_manifests(path)
  RelatedFilesInfo.create_from_file_infos(load_file_info_list(path).reject { |info| info.package_manager.nil? })
end

#find_manifests_from_contents(file_path_contents_hash) ⇒ Object



99
100
101
# File 'lib/bibliothecary/runner.rb', line 99

def find_manifests_from_contents(file_path_contents_hash)
  RelatedFilesInfo.create_from_file_infos(load_file_info_list_from_contents(file_path_contents_hash).reject { |info| info.package_manager.nil? })
end

#find_manifests_from_paths(paths) ⇒ Object



95
96
97
# File 'lib/bibliothecary/runner.rb', line 95

def find_manifests_from_paths(paths)
  RelatedFilesInfo.create_from_file_infos(load_file_info_list_from_paths(paths).reject { |info| info.package_manager.nil? })
end

#identify_manifests(file_list) ⇒ Object

this skips manifests sometimes because it doesn’t look at file contents and can’t establish from only regexes that the thing is a manifest. We exclude rather than include ambiguous filenames because this API is used by libraries.io and we don’t want to download all .xml files from GitHub.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/bibliothecary/runner.rb', line 115

def identify_manifests(file_list)
  ignored_dirs_with_slash = ignored_dirs.map { |d| if d.end_with?("/") then d else d + "/" end }
  allowed_file_list = file_list.reject do |f|
    ignored_dirs.include?(f) || f.start_with?(*ignored_dirs_with_slash)
  end
  allowed_file_list = allowed_file_list.reject{|f| ignored_files.include?(f)}
  package_managers.map do |pm|
    allowed_file_list.select do |file_path|
      # this is a call to match? without file contents, which will skip
      # ambiguous filenames that are only possibly a manifest
      pm.match?(file_path)
    end
  end.flatten.uniq.compact
end

#ignored_dirsObject



130
131
132
# File 'lib/bibliothecary/runner.rb', line 130

def ignored_dirs
  @configuration.ignored_dirs
end

#ignored_filesObject



134
135
136
# File 'lib/bibliothecary/runner.rb', line 134

def ignored_files
  @configuration.ignored_files
end

#load_file_info_list(path) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bibliothecary/runner.rb', line 75

def load_file_info_list(path)
  file_list = []

  Find.find(path) do |subpath|
    info = FileInfo.new(path, subpath)

    Find.prune if FileTest.directory?(subpath) && ignored_dirs.include?(info.relative_path)
    next unless FileTest.file?(subpath)
    next if ignored_files.include?(info.relative_path)

    add_files_to_list(file_list, info)
  end

  file_list
end

#load_file_info_list_from_contents(file_path_contents_hash) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bibliothecary/runner.rb', line 45

def load_file_info_list_from_contents(file_path_contents_hash)
  # Parses an array of format [{file_path: "", contents: ""},] to match
  #  on both filename matches, and one content_match patterns.
  file_list = []

  file_path_contents_hash.each do |file|
    info = FileInfo.new(nil, file[:file_path], file[:contents])

    next if ignored_files.include?(info.relative_path)

    add_files_to_list(file_list, info)
  end

  file_list
end

#load_file_info_list_from_paths(paths) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bibliothecary/runner.rb', line 61

def load_file_info_list_from_paths(paths)
  file_list = []

  paths.each do |path|
    info = FileInfo.new(nil, path)

    next if ignored_files.include?(info.relative_path)

    add_files_to_list(file_list, info)
  end

  file_list
end

#load_file_list(path) ⇒ Object

deprecated; use load_file_info_list.



32
33
34
# File 'lib/bibliothecary/runner.rb', line 32

def load_file_list(path)
  load_file_info_list(path).map { |info| info.full_path }
end

#package_managersObject



41
42
43
# File 'lib/bibliothecary/runner.rb', line 41

def package_managers
  Bibliothecary::Parsers.constants.map{|c| Bibliothecary::Parsers.const_get(c) }.sort_by{|c| c.to_s.downcase }
end