Class: Caracara::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/caracara/task.rb

Overview

Task class

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(steps, fixed_options = {}, args = {}) ⇒ Task

Initialize



12
13
14
15
16
# File 'lib/caracara/task.rb', line 12

def initialize(steps, fixed_options = {}, args = {})
  @steps = steps
  @fixed_options = fixed_options
  @options = args
end

Class Attribute Details

.fixed_optionsObject (readonly)

Static attributes



76
77
78
# File 'lib/caracara/task.rb', line 76

def fixed_options
  @fixed_options
end

.stepsObject (readonly)

Static attributes



76
77
78
# File 'lib/caracara/task.rb', line 76

def steps
  @steps
end

Class Method Details

.dir(name, &blk) ⇒ Object

Run steps inside a dir



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/caracara/task.rb', line 79

def dir(name, &blk)
  # Store the current steps and set a blank steps
  old, @steps = @steps, nil

  # Generate the block steps
  result = yield

  # Restore the old steps
  @steps = old

  # Put dir commands in the steps
  step "(cd #{name} && (#{result.join('\n')}))"
end

.init(args = {}) ⇒ Object

Initialize a task



127
128
129
130
# File 'lib/caracara/task.rb', line 127

def init(args = {})
  # Create a new instance
  new @steps, @fixed_options, args
end

.step(cmd, fixed_options = {}) ⇒ Object

Add a step



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/caracara/task.rb', line 94

def step(cmd, fixed_options = {})
  # Define the step
  @steps = [] if @steps.nil?

  # Define the step
  @fixed_options = {} if @fixed_options.nil?

  # Add a new step
  if cmd.is_a?(String)
    # Push step and get the index
    index = @steps.push(cmd).length - 1

    # Add fixed options
    @fixed_options[index] = fixed_options

    # Default return
    return @steps
  end

  # If it is another task
  cmd.steps.each do |task_cmd|
    # Push and get the index
    index = @steps.push(task_cmd).length - 1

    # Add fixed options
    @fixed_options[index] = fixed_options
  end

  # Default return
  @steps
end

Instance Method Details

#command(args = {}, escape = true) ⇒ Object

Generate the SSH command



39
40
41
# File 'lib/caracara/task.rb', line 39

def command(args = {}, escape = true)
  SSH.command compile(args), escape
end

#compile(args = {}) ⇒ Object

Compile the tasks



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/caracara/task.rb', line 24

def compile(args = {})
  # Merge args with defult options
  options = Utils.merge @options, args

  # Each the steps
  @steps.map.with_index do |step, index|
    # Append with the fixed options
    options = Utils.merge(options, @fixed_options[index]) unless @fixed_options[index].nil?

    # Compile the mustache template
    Mustache.render step, compile_options(options, options)
  end
end

#stepsObject

Steps



19
20
21
# File 'lib/caracara/task.rb', line 19

def steps
  @steps
end