Class: FFI::Generators::BodyGuard

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi2/generators.rb

Instance Method Summary collapse

Constructor Details

#initialize(subject, label, platform) ⇒ BodyGuard

Returns a new instance of BodyGuard.



19
20
21
22
23
# File 'lib/ffi2/generators.rb', line 19

def initialize(subject, label, platform)
  @subject = subject
  @label = label.gsub(/\W/, '-')
  @platform = platform
end

Instance Method Details

#handle(command, failure) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# 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

#performObject

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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Object



92
93
94
95
96
# File 'lib/ffi2/generators.rb', line 92

def remove(*names)
  names.each do |name|
    File.delete name if File.exists? name
  end
end