Module: Rake::Pipeline

Defined in:
lib/rake_pipeline.rb

Overview

Include the step_dependencies and step_def methods to simplify Pipelines. Steps depend on the step strictly above by default. The output of the step is save marshaled, except for Strings which are save as text. The input of the step, the output of the previous step if availabe is accessed with the input method

Example

step_def :text do

"Text to revert"

end

step_def :revert do

text = input
text.reverse

end

Constant Summary collapse

NON_ASCII_PRINTABLE =
/[^\x20-\x7e\s]/
@@steps =
[]

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object



190
191
192
193
194
195
196
# File 'lib/rake_pipeline.rb', line 190

def method_missing(symbol, *args)
  if $_current_job.methods.include? symbol.to_s
    $_current_job.send(symbol, *args)
  else
    raise "Method #{ symbol } not found"
  end
end

Instance Method Details

#infile(task, step = nil, &block) ⇒ Object



146
147
148
149
150
151
# File 'lib/rake_pipeline.rb', line 146

def infile(task, step = nil,  &block)
  filename = input_filename(task, step)
  File.open(filename) do |f|
    block.call(f)
  end
end

#info(values = {}) ⇒ Object

Add values to the info file



204
205
206
207
208
209
# File 'lib/rake_pipeline.rb', line 204

def info(values = {})
  info = Rake::Pipeline::Info.load_info(@@current_task)
  info = info.merge values
  Rake::Pipeline::Info.save_info(@@current_task, info)
  info
end

#input(step = nil) ⇒ Object

Load the input data from the previous step



185
186
187
# File 'lib/rake_pipeline.rb', line 185

def input(step = nil)
  load_input(@@current_task, step) if @@current_task
end

#input_filename(task = nil, step = nil) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/rake_pipeline.rb', line 131

def input_filename(task = nil, step = nil)
  task ||= @@current_task
  if step.nil?
    task.prerequisites.first
  else
    File.join(step_dir(step, task.name), job_name)
  end
end

#is_binary?(file) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
114
115
# File 'lib/rake_pipeline.rb', line 111

def is_binary?(file)
  binary = file.read(1024) =~ NON_ASCII_PRINTABLE
  file.rewind
  binary
end

#job_nameObject



211
212
213
# File 'lib/rake_pipeline.rb', line 211

def job_name
  File.basename(@@current_task.name)
end

#load_input(task, step = nil) ⇒ Object



160
161
162
163
164
165
166
167
168
# File 'lib/rake_pipeline.rb', line 160

def load_input(task, step = nil)
  infile(task, step) do |f|
    if is_binary?(f)
      Marshal.load(f) 
    else
      f.read
    end
  end
end

#outfile(task, &block) ⇒ Object



153
154
155
156
157
158
# File 'lib/rake_pipeline.rb', line 153

def outfile(task, &block)
  filename = output_filename(task)
  File.open(filename, 'w') do |f|
    block.call(f)
  end
end

#output_filename(task = nil) ⇒ Object



140
141
142
143
# File 'lib/rake_pipeline.rb', line 140

def output_filename(task = nil)
  task ||= @@current_task
  task.name
end

#save_output(task, output) ⇒ Object



171
172
173
174
175
176
177
178
179
180
# File 'lib/rake_pipeline.rb', line 171

def save_output(task, output)
  case 
  when output.nil?
    nil
  when String === output
    outfile(task){|f| f.write output } 
  else
    outfile(task){|f| f.write Marshal.dump(output) } 
  end
end

#step(state, message = "") ⇒ Object

Announce steps



199
200
201
# File 'lib/rake_pipeline.rb', line 199

def step(state, message ="")
  puts "#{ state }: #{ message }"
end

#step_def(name, dependencies = nil, &block) ⇒ Object

Define a new step, it depends on the previously defined by default. It saves the output of the block so it can be loaded by the input method of the next step



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/rake_pipeline.rb', line 219

def step_def(name, dependencies = nil, &block)
  @@steps << name
  rule step_dependencies(name, dependencies) do |t| 

    # Save the task object to be able to load the input
    @@current_task = t
    
    output = block.call(t)
    
    save_output(t, output)
  end

end

#step_dependencies(*args) ⇒ Object



121
122
123
# File 'lib/rake_pipeline.rb', line 121

def step_dependencies(*args)
  Rake::Pipeline::Step.step_dependencies(*args)
end

#step_descriptionsObject



117
118
119
# File 'lib/rake_pipeline.rb', line 117

def step_descriptions
  Rake::Pipeline::Step.step_descriptions
end

#step_dir(step = nil, filename = nil) ⇒ Object



125
126
127
128
129
# File 'lib/rake_pipeline.rb', line 125

def step_dir(step = nil, filename = nil)
  filename ||= @@current_task.name
  info = Rake::Pipeline::Step.parse_filename(filename)
  "%s/%s" % [info[:prefix], step || info[:step]]
end

#stepsObject



105
106
107
# File 'lib/rake_pipeline.rb', line 105

def steps
  @@steps
end