class FFI::Generators::BodyGuard

Public Class Methods

new(subject, label, platform) click to toggle source
# File lib/ffi2/generators.rb, line 19
def initialize(subject, label, platform)
  @subject = subject
  @label = label.gsub(/\W/, '-')
  @platform = platform
end

Public Instance Methods

handle(command, failure) click to toggle source
# File lib/ffi2/generators.rb, line 79
def handle(command, failure)
  result = `#{command}`
  Process.waitpid $?.pid rescue nil

  unless $?.success?
    result = result.split("\n").map { |l| "\t#{l}" }.join "\n"
    msg = "#{@subject.send failure}:\n#{result}"
    raise BodyGuardError, msg
  end

  result
end
perform() click to toggle source

Orchestrates a workflow by querying the subject at each stage and cleans up if problems arise before raising an exception.

Expects the subject to respond to the following methods:

#source io
  Where io is an IO instance used to create the source for later
  stages.

#prepare name, target
  Where name is the source file name and target is the file that would
  be created by the prepare process. The method should return the
  command to run.

#prepare_failed
  The method should return the error message for #raise to which will
  be appended the output of running the command returned by #prepare.

#process target
  Where target is the same as passed to #prepare. The method should
  return the command to run. If no further options or changes are
  needed, #process should just return target.

#process_failed
  The method should return the error message for #raise to which will
  be appended the output of running the command returned by #process.

The perform method returns the result of running the command returned by the process method.

# File lib/ffi2/generators.rb, line 56
def perform
  begin
    name = "rbx-ffi-generators-#{@label}"
    source = File.expand_path name + @platform.source_ext
    target = File.expand_path name + @platform.executable_ext

    File.open source, "wb" do |f|
      @subject.source f
    end

    if preparer = @subject.prepare(source, target)
      handle preparer, :prepare_failed
    else
      target = source
    end

    processor = @subject.process target
    return handle(processor, :process_failed)
  ensure
    remove source, target
  end
end
remove(*names) click to toggle source
# File lib/ffi2/generators.rb, line 92
def remove(*names)
  names.each do |name|
    File.delete name if File.exists? name
  end
end