Class: Plumr::Command

Inherits:
Object
  • Object
show all
Includes:
Fitting
Defined in:
lib/plumr.rb

Overview

A fitting which represents a shell command

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, *args) ⇒ Command

Creates a Command which represents the specified command and a list of arguments args. Arguments may be strings, fittings, or Plumr::OUTPUT. Plumr::OUTPUT will be replaced by the name of the output file when the command is executed.



195
196
197
198
199
# File 'lib/plumr.rb', line 195

def initialize(command, *args) #:nodoc:
  @command = command.dup.freeze
  @args = args.freeze
  @sources = @args.flatten.select { |arg| Fitting === arg }.uniq
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



189
190
191
# File 'lib/plumr.rb', line 189

def args
  @args
end

#commandObject (readonly)

Returns the value of attribute command.



188
189
190
# File 'lib/plumr.rb', line 188

def command
  @command
end

Instance Method Details

#cleanupObject



219
220
221
222
# File 'lib/plumr.rb', line 219

def cleanup
  @sources.each { |source| source.cleanup }
  self
end

#cleanup_temp(filename) ⇒ Object



232
233
234
235
236
237
238
# File 'lib/plumr.rb', line 232

def cleanup_temp(filename)
  begin
    ::File.unlink(filename)
  rescue Exception
  end
  self
end

#generate(output_file) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/plumr.rb', line 201

def generate(output_file)
  input_files = {}
  ok = false
  begin
    @sources.each_with_index do |fitting, index|
      input_files[fitting] = fitting.generate_temp(output_file, index)
    end
    ok = system(@command, *substitute_args(output_file, input_files, @args))
    raise CommandError, "#{@command} exited with status #{$?}" unless ok
  ensure
    cleanup_temp(output_file) unless ok
    input_files.each_pair do |fitting, input_file|
      fitting.cleanup_temp(input_file)
    end
  end
  self
end

#generate_temp(prefix, index) ⇒ Object



224
225
226
227
228
229
230
# File 'lib/plumr.rb', line 224

def generate_temp(prefix, index)
  # FIXME: need better tempfile generation, but I'm loath to use stdlib's
  # Thread.critical-based tempfile stuff
  output_file = "#{prefix.sub(/\.plumr-temp$/,'')}.#{index}.plumr-temp"
  generate(output_file)
  output_file
end

#isolateObject



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/plumr.rb', line 248

def isolate
  dependencies = Set.new
  args = []
  @args.each do |arg|
    case arg
    when Fitting
      arg_source, arg_dependencies = arg.isolate
      dependencies.merge arg_dependencies
      args.push arg_source
    else
      args.push arg
    end
  end
  [ with_replaced_args(*args), dependencies ]
end

#older_than?(timestamp) ⇒ Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/plumr.rb', line 244

def older_than?(timestamp)
  @sources.all? { |source| source.older_than? timestamp }
end

#up_to_date?Boolean

Returns:

  • (Boolean)


240
241
242
# File 'lib/plumr.rb', line 240

def up_to_date?
  @sources.all? { |source| source.up_to_date? }
end