Class: Dependencytree::TreeMain

Inherits:
Object
  • Object
show all
Defined in:
lib/dependencytree/treemain.rb

Overview

Contains the main method for handling the program flow.

Class Method Summary collapse

Class Method Details

.handle_path(options, consumer, path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dependencytree/treemain.rb', line 15

def self.handle_path(options, consumer, path)
  if options[:ignore].match(path)
    return
  end
  if File.directory?(path)
    STDERR.puts path if options[:verbose]
    Dir.entries(path).each { |x|
      resolved = File.join(path, x)
      handle_path(options, consumer, resolved) if File.directory?(resolved) && x != "." && x != ".."
      handle_path(options, consumer, resolved) if File.file?(resolved) && options[:pattern].match(resolved)
    }
  elsif File.file?(path)
    STDERR.puts path if options[:verbose]
    @@log.debug("Handling path #{path}")
    tree = Parser::CurrentRuby.parse_file(path)
    @@log.debug("Parsed tree: #{tree}") if @@log.debug?
    consumer.visit(path, tree)
  end
end

.mainObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dependencytree/treemain.rb', line 35

def self.main
  options = {}
  options[:ignore] = /^$/
  options[:pattern] = /.*\.rb/
  OptionParser.new do |opt|
    opt.on("-v", "--verbose", "Verbose output") do |o|
      options[:verbose] = true
    end
    opt.on("-d", "--debug", "Log debugging output to file 'dependencytree.log'") do |o|
      options[:debug] = true
    end
    opt.on("-p", "--pattern[=OPTIONAL]", "Pattern to accept source codes with (default: #{options[:pattern].to_s})") do |o|
      options[:pattern] = /#{o}/
    end
    opt.on("-i", "--ignore[=OPTIONAL]", "Paths to not load (default: #{options[:ignore].to_s})") do |o|
      options[:ignore] = /#{o}/
    end
    opt.on("-o", "--output[=OPTIONAL]", "Output path for the JSON file") do |o|
      options[:output] = o
    end
    opt.on_tail("-h", "--help", "Show this message") do
      puts opt
      return
    end
  end.parse!

  if options[:debug]
    log = Logger.new('dependencytree.log')
    log.level = Logger::WARN
  else
    log = Logger.new(STDOUT)
    log.level = Logger::WARN
  end
  @@log = log

  treeinterpreter = TreeInterpreter.new(log)
  ARGV.each do |path|
    handle_path(options, treeinterpreter, File.absolute_path(path))
  end

  dependencyresolver = DependencyResolver.new(log, treeinterpreter.classes_and_modules)
  dependencyresolver.resolve_references

  json = dependencyresolver.classes_and_modules.to_json
  if options[:output]
    File.write(options[:output], json)
  else
    puts json
  end
end