Class: Pwrake::Graphviz

Inherits:
Object
  • Object
show all
Defined in:
lib/pwrake/misc/graphviz.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraphviz

Returns a new instance of Graphviz.



5
6
7
8
9
10
11
12
13
14
# File 'lib/pwrake/misc/graphviz.rb', line 5

def initialize
  @nodes = []
  @edges = []
  # @node_id = {}
  @filenode_id = {}
  @tasknode_id = {}
  @node_name = {}
  @count = 0
  @traced = {}
end

Instance Attribute Details

#filenode_idObject (readonly)

Returns the value of attribute filenode_id.



16
17
18
# File 'lib/pwrake/misc/graphviz.rb', line 16

def filenode_id
  @filenode_id
end

#node_nameObject (readonly)

Returns the value of attribute node_name.



16
17
18
# File 'lib/pwrake/misc/graphviz.rb', line 16

def node_name
  @node_name
end

#tasknode_idObject (readonly)

Returns the value of attribute tasknode_id.



16
17
18
# File 'lib/pwrake/misc/graphviz.rb', line 16

def tasknode_id
  @tasknode_id
end

Instance Method Details

#push_fileedge(name, target) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pwrake/misc/graphviz.rb', line 69

def push_fileedge( name, target )
  if target
    if n2 = @tasknode_id[target]
      n1 = @filenode_id[name]
    elsif n1 = @tasknode_id[name]
      n2 = @filenode_id[target]
    else
      n1 = @filenode_id[name]
      n2 = @filenode_id[target]
    end
    @edges.push "#{n1} -> #{n2};"
  end
end

#push_filenode(name) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/pwrake/misc/graphviz.rb', line 48

def push_filenode( name )
  if @filenode_id[name].nil?
    tag = "T#{@count}"
    @count += 1
    @filenode_id[name] = tag
    @node_name[tag] = name
    @nodes.push "#{tag} [label=\"#{trim(name)}\", shape=box];"
  end
end

#push_taskedge(name) ⇒ Object



83
84
85
86
87
88
# File 'lib/pwrake/misc/graphviz.rb', line 83

def push_taskedge( name )
  if n1 = @tasknode_id[name]
    n2 = @filenode_id[name]
    @edges.push "#{n1} -> #{n2};"
  end
end

#push_tasknode(name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/pwrake/misc/graphviz.rb', line 58

def push_tasknode( name )
  if @tasknode_id[name].nil?
    tag = "T#{@count}"
    @count += 1
    @tasknode_id[name] = tag
    @node_name[tag] = name
    label = Rake.application[name].comment
    @nodes.push "#{tag} [label=\"#{label}\", shape=ellipse];"
  end
end

#trace(name = :default, target = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pwrake/misc/graphviz.rb', line 18

def trace( name = :default, target = nil )
  traced_cond = @traced[name]

  task = Rake.application[name]

  #if task.kind_of?(Rake::FileTask)
  if task.kind_of?(Rake::Task)
    push_filenode( name )
    if !task.actions.empty? and !traced_cond
      push_tasknode( name )
      push_taskedge( name )
    end
    push_fileedge( name, target )
    target = name
  end
  @traced[name] = true

  if !traced_cond
    task.prerequisites.each_with_index do |prereq,i|
      trace( prereq, target )
    end
  end
end

#trim(name) ⇒ Object



42
43
44
45
46
# File 'lib/pwrake/misc/graphviz.rb', line 42

def trim( name )
  name = name.to_s
  name = File.basename(name)
  name.sub(/H\d+/,'').sub(/object\d+/,"")
end

#write(file) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/pwrake/misc/graphviz.rb', line 90

def write(file)
  open(file, "w") do |w|
    #w.puts "digraph sample {\ngraph [size=\"12,100\",ranksep=1.5,nodesep=0.2];"
    w.puts "digraph sample {"
    w.puts "graph [size=\"70,70\", rankdir=LR];"
    @nodes.each do |x|
      w.puts x
    end
    @edges.each do |x|
      w.puts x
    end
    w.puts "}"
  end
end