Class: Capitate::TaskNode

Inherits:
Object
  • Object
show all
Includes:
Plugins::Base
Defined in:
lib/capitate/task_node.rb

Overview

Task node in the capistrano namespace, task hierarchy.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Plugins::Base

#indent_doc, #load_all_tasks, #relative_to_root, #root, #task_tree, #unindent, #usage

Constructor Details

#initialize(name, parent = nil) ⇒ TaskNode

Create task node with name and parent For root not use name = “top”

Options

name

Node name (namespace name)

parent

Parent node



15
16
17
18
19
20
# File 'lib/capitate/task_node.rb', line 15

def initialize(name, parent = nil)
  @name = name
  @parent = parent
  @nodes = []
  @tasks = []
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/capitate/task_node.rb', line 6

def name
  @name
end

#nodesObject (readonly)

Returns the value of attribute nodes.



6
7
8
# File 'lib/capitate/task_node.rb', line 6

def nodes
  @nodes
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/capitate/task_node.rb', line 6

def parent
  @parent
end

#tasksObject (readonly)

Returns the value of attribute tasks.



6
7
8
# File 'lib/capitate/task_node.rb', line 6

def tasks
  @tasks
end

Class Method Details

.populate_with_task(top_node, task) ⇒ Object

Create nodes and and task to node.

If task is “mysql:centos:install”, the nodes will look like:

(top node) -> (mysql node) -> (centos node; w/ tasks: install)

Options

top_node

Node to start at

task

Task to add



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/capitate/task_node.rb', line 245

def self.populate_with_task(top_node, task)
  node_names = task.namespace.fully_qualified_name.split(":")
  
  node = top_node
  
  node_names.each do |name|
    parent_node = node
    node = parent_node.find(name)
    if !node
      node = self.new(name, parent_node)
      parent_node.add_node(node)
    end
  end
  node.add_task(task)      
end

Instance Method Details

#add_node(task_node) ⇒ Object

Add “child” node.

Options

task_node

Node



27
28
29
# File 'lib/capitate/task_node.rb', line 27

def add_node(task_node)
  @nodes << task_node
end

#add_task(task) ⇒ Object

Add task to this node.

Options

task

Add task associated with this node (namespace).



48
49
50
# File 'lib/capitate/task_node.rb', line 48

def add_task(task)
  @tasks << task
end

#each_node(level = 0, &block) ⇒ Object

Iterate over ALL “child” nodes, depth first. Yields |node, level|.

Options

level

Current level



68
69
70
71
72
73
# File 'lib/capitate/task_node.rb', line 68

def each_node(level = 0, &block)
  sorted_nodes.each do |node|
    yield(node, level)
    node.each_node(level + 1, &block)
  end
end

#find(name) ⇒ Object

Find node with name (namespace).

Options

name

Name to look for



36
37
38
39
40
41
# File 'lib/capitate/task_node.rb', line 36

def find(name)
  @nodes.each do |node| 
    return node if node.name == name
  end
  nil
end

#full_name(delimeter = "-") ⇒ Object

Get the full name, using delimeter

Options

delimeter

Delimeter

Examples

node.full_name(":") => "mysql:centos"  # On node mysql centos node (top -> mysql -> centos)


83
84
85
86
87
88
89
90
91
# File 'lib/capitate/task_node.rb', line 83

def full_name(delimeter = "-")
  if parent
    parent_name = parent.full_name
    return [ parent_name, name ].compact.join(delimeter)
  end
  
  # Return nil for top node
  name == "top" ? nil : name
end

#sorted_nodesObject

Get “child” nodes (sorted).



53
54
55
# File 'lib/capitate/task_node.rb', line 53

def sorted_nodes
  nodes.sort_by(&:name)
end

#sorted_tasksObject

Get tasks (sorted).



58
59
60
# File 'lib/capitate/task_node.rb', line 58

def sorted_tasks
  tasks.sort_by(&:fully_qualified_name) # { |t| t.name.to_s }
end

#to_s(level = 0) ⇒ Object

Node to string.



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/capitate/task_node.rb', line 216

def to_s(level = 0)
  spaces = "     "
  indent = (0...level).collect { spaces }.join("") 
  
  # Ignore top node (level = -1)
  if level >= 0
    t_indent = "\n#{indent} #{spaces} - "
    task_names = tasks.collect(&:name).sort_by(&:to_s).join(t_indent)
    s = "#{indent} -- #{name}:#{t_indent}#{task_names}\n"
  else
    s = ""
  end
  
  sorted_nodes.each do |node|    
    s += node.to_s(level + 1)
  end
  s
end

#write_doc(dir, file_name = nil, title = nil, options = {}, &block) ⇒ Object

Write textile documentation for node (recursively).

Uses task desc attribute for task details.

Options

dir

Dir to write to

file_name

File name to write to, defaults to full name

title

Title and h1 for page, defaults to name

options

Options



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/capitate/task_node.rb', line 103

def write_doc(dir, file_name = nil, title = nil, options = {}, &block)
  
  file_name ||= full_name
  title ||= full_name(":")
  
  
  
  path = "#{dir}/#{file_name}.txt"
  puts "%10s %-30s" % [ "create", path ]
  
  File.open(path, "w") do |file|
    file.puts "h1. #{title}\n\n"      
    
    #
    # Breadcrumb generate
    #
    bc_full_name = full_name(":")
    links = []
    if bc_full_name
      names = bc_full_name.split(":")
      links << " #{names.pop} " # Pop off current
      
      while(!names.empty?) do          
        links << %{ "#{names.last}":#{names.join("-")}.html }
        names.pop
      end
    end
    
    # Write breadcrumb
    file.puts %{ "home":../index.html > "recipes":index.html > #{links.reverse.join(" > ")} }
  
    # Write task count
    # count = 0
    # each_node do |snode, level|
    #   count += snode.tasks.length
    # end
    # file.puts %{\n\n*#{count}* tasks!\n\n}
    
    #
    # Namespace
    #
    unless nodes.empty?
      file.puts "\n\nh2. Namespaces\n\n"
      each_node do |snode, level|
        #li_level = (0..level).collect { "*" }.join
        li_level = "*"
        if snode.tasks.length > 0
          file.puts %{#{li_level} "#{snode.full_name(":")}":#{snode.full_name}.html (#{snode.tasks.length}) \n}                  
        end
      end        
    end
    
    #
    # Tasks
    # 
    unless tasks.empty?
      file.puts "\n\nh2. Tasks\n\n"
      sorted_tasks.each do |task|
        file.puts %{* "#{task.fully_qualified_name}":##{task.fully_qualified_name} \n}                  
      end        
    end
    
    #
    # Task details
    #
    unless tasks.empty?
      file.puts "\n\nh2. Task documentation\n\n"        
      sorted_tasks.each do |task|
        file.puts %{<div class="recipe">\n\n}
        options = ""
        options = "<span class='options'>, #{task.options.inspect}</span>" unless task.options.blank?
        file.puts "h3(##{task.fully_qualified_name}). #{task.fully_qualified_name}#{options}\n\n"
        file.puts "#{unindent(task.desc)}\n\n"
        if task.arguments
          file.puts "h4. Parameters\n\n"
          file.puts "<dl>\n"
          task.arguments.each do |arg|
            file.puts "<dt>#{arg[:name]}</dt>"
            file.puts "<dd>#{unindent(arg[:desc])}</dd>" if arg.has_key?(:desc)
            if arg.has_key?(:default_desc)
              file.puts "<dd class='default'>Defaults to @#{arg[:default_desc]}@</dd>"
            elsif arg.has_key?(:default)
              file.puts "<dd class='default'>Defaults to @#{arg[:default].inspect}@</dd>"
            end           
            
            if arg.has_key?(:set)
              file.puts "<dd class='default'>Defaults to @#{arg[:set].inspect}@ (cap setting)</dd>"
            end
               
            file.puts "<dd class='example'>Example: @set :#{arg[:name]}, #{arg[:example]}@" if arg.has_key?(:example)
          end 
        end
        file.puts "</dl>\n\n"
        file.puts "</div>\n\n\n"
      end
    end
    
    #
    # Write doc (recursively for "child" namespace)
    #
    sorted_nodes.each do |snode|
      snode.write_doc(dir, nil, nil, options)
    end
    
    # If block then let it do stuff with open file
    if block_given?
      file.puts "\n\n"
      yield(file) 
    end      
  end
end