Module: TraceViewer

Defined in:
lib/trace_viewer.rb,
lib/trace_viewer/line_detail.rb

Defined Under Namespace

Classes: LineDetail

Class Method Summary collapse

Class Method Details

.build_html(trace) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/trace_viewer.rb', line 25

def build_html(trace)
  template_name = File.join(File.dirname(__FILE__), "views", "index.html.haml")
  template_contents = File.read(template_name)

  engine = Haml::Engine.new(template_contents)
  engine.render(Object.new, :trace => trace)
end

.filtered_traceObject



58
59
60
61
62
63
# File 'lib/trace_viewer.rb', line 58

def filtered_trace
  caller.select {|line|
    # line !~ /\/trace_viewer/ &&
    line !~ /\/rspec/
  }
end

.load_and_parse_traceObject



33
34
35
36
37
38
39
40
# File 'lib/trace_viewer.rb', line 33

def load_and_parse_trace
  trace_lines = filtered_trace
  trace_lines.map {|line|
    parse_trace_line(line)
  }.select {|line|
    line.size == 2
  }
end

.load_function(filename, line_number) ⇒ Object



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

def load_function(filename,line_number)
  puts "loading parse tree for #{filename}"
  lines = File.read(filename)
  parser=RedParse.new(lines)
  tree=parser.parse
  walk_tree(tree) do |function_node|
    if (function_node.kind_of?(RedParse::MethodNode) && (line_number.between?(function_node.startline, function_node.endline)))
      source = lines.split("\n").slice(function_node.startline-1, function_node.endline-function_node.startline+1)
      return [function_node, source]
    end
  end
  nil
end

.map_trace_to_line_details(parsed_trace) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/trace_viewer.rb', line 42

def map_trace_to_line_details(parsed_trace)
  parsed_trace.map do |line|
    filename,line_number = line

    function_node, source = load_function(filename, line_number)

    detail = LineDetail.new
    detail.filename = filename
    detail.line_number = line_number
    detail.source = source
    detail.function_node = function_node

    detail
  end
end

.open_html(filename) ⇒ Object



13
14
15
# File 'lib/trace_viewer.rb', line 13

def open_html(filename)
  Launchy::Browser.run(filename)
end

.parse_trace_line(line) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/trace_viewer.rb', line 65

def parse_trace_line(line)
  path,line_number,function = line.split(':')
  line_number = line_number.to_i
  if (function)
    [path,line_number]
  else
    []
  end
end

.runObject



5
6
7
8
9
10
11
# File 'lib/trace_viewer.rb', line 5

def run()
  trace = load_and_parse_trace
  loaded_trace = map_trace_to_line_details(trace)
  html = build_html(loaded_trace)
  filename = store_html(html)
  open_html(filename)
end

.store_html(html) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/trace_viewer.rb', line 17

def store_html(html)
  filename = File.join(Dir.pwd, "trace_viewer_#{Time.now.to_i}.html")
  File.open(filename, "w") do |file|
    file.puts html
  end
  filename
end

.walk_tree(node, depth = 0, &block) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/trace_viewer.rb', line 89

def walk_tree(node,depth=0,&block)
  return if node.nil? || !node.kind_of?(RedParse::Node)
  node.each do |child|
    yield child if child.kind_of?(RedParse::MethodNode)
    walk_tree(child,depth+1,&block) unless depth >= 10
  end
end