Class: ImportGraph::Parser::Main

Inherits:
Object
  • Object
show all
Includes:
Util, RuboCop::AST
Defined in:
lib/import_graph/parser/main.rb

Overview

Main entry point for parsing that coordinates all the parsers

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#build_match_object, #cleanup_absolute_path

Constructor Details

#initialize(dir_path, config) ⇒ Main

skipcq: RB-LI1087

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/import_graph/parser/main.rb', line 12

def initialize(dir_path, config)
  @dir_path = File.expand_path(dir_path)
  raise ArgumentError, "No such directory #{@dir_path}" unless Dir.exist? @dir_path

  @files_list = Dir.glob(File.join(dir_path, '**', '*.rb'))
  @files_list = @files_list.map { |path| File.expand_path(path) }
  @path_manager = PathManager.new(@dir_path)

  @parsers = [RequireRelative, Require, Load, Autoload]
  @third_party = config[:third_party] || false
  @file_trees = gen_syntax_trees
end

Instance Attribute Details

#file_treesObject (readonly)

Returns the value of attribute file_trees.



9
10
11
# File 'lib/import_graph/parser/main.rb', line 9

def file_trees
  @file_trees
end

Instance Method Details

#add_edge(graph, from, to) ⇒ Object



47
48
49
50
51
52
# File 'lib/import_graph/parser/main.rb', line 47

def add_edge(graph, from, to)
  to = cleanup_absolute_path(@dir_path, to) if to.start_with? @dir_path
  graph[from] = Set.new unless graph.key? from

  graph[from].add to
end

#combine_graphs(main, third_party) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/import_graph/parser/main.rb', line 54

def combine_graphs(main, third_party)
  return if third_party.empty? || third_party.nil?

  third_party.each_key do |from|
    third_party[from].each do |to|
      add_edge(
        main,
        cleanup_absolute_path(@dir_path, from),
        cleanup_absolute_path(@dir_path, to)
      )
    end
  end
end

#gen_syntax_treesObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/import_graph/parser/main.rb', line 68

def gen_syntax_trees
  file_trees_list =
    Parallel.map(@files_list) do |file_path|
      {
        path: cleanup_absolute_path(@dir_path, file_path),
        tree:
          ProcessedSource.new(File.read(file_path), RUBY_VERSION.to_f).ast
      }
    end

  file_trees_list.each_with_object({}) do |file_pair, obj|
    obj[file_pair[:path]] = file_pair[:tree]
  end
end

#parseObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/import_graph/parser/main.rb', line 25

def parse
  graph = {}
  matches = Set.new

  @parsers.each do |parser_klass|
    parser = parser_klass.new(@file_trees)
    local_matches = parser.parse

    matches.merge(local_matches)
  end
  third_party_graph = {}
  third_party_graph = ThirdParty.new(@dir_path).parse if @third_party == true

  matches = @path_manager.resolve_paths(matches)
  matches.each do |match|
    add_edge(graph, match[:from], match[:to])
  end

  combine_graphs(graph, third_party_graph)
  DepGraph.new(graph, @dir_path)
end