Class: ImportGraph::Parser::ThirdParty

Inherits:
Object
  • Object
show all
Defined in:
lib/import_graph/parser/third_party.rb

Overview

Directories that use a third party autoloader (Zeitwerk) are not straightforward to parse for. In this case, rubrowser is used to scan for defined constants and if any file is found using that constant, we call it a dependent file.

Instance Method Summary collapse

Constructor Details

#initialize(dir_path) ⇒ ThirdParty

Returns a new instance of ThirdParty.



10
11
12
# File 'lib/import_graph/parser/third_party.rb', line 10

def initialize(dir_path)
  @dir_path = dir_path
end

Instance Method Details

#parseObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/import_graph/parser/third_party.rb', line 14

def parse
  return @dependency_graph unless @dependency_graph.nil?

  obj = run_rubrowser

  # Create a hash linking constant definitions with the file they were defined in
  definitions = {}
  obj['definitions'].each do |definition|
    definitions[definition['namespace']] = definition['file']
  end

  # Go through the relations, if a file uses a certain constant
  # add the file for that constant definition in the adjacency list
  @dependency_graph = {}
  obj['relations'].each do |reln|
    if definitions.key? reln['resolved_namespace']
      @dependency_graph[reln['file']] = [] unless @dependency_graph.key? reln['file']
      @dependency_graph[reln['file']] << definitions[reln['resolved_namespace']]
    end
  end

  @dependency_graph
end

#run_rubrowserObject

runs rubrowser and returns a Ruby hash with constant definitions and the files they were defined in. Also a list of relations, i.e Which constant was used in which file



41
42
43
44
# File 'lib/import_graph/parser/third_party.rb', line 41

def run_rubrowser
  json_output = `rubrowser -j #{@dir_path}`
  JSON.parse(json_output)
end