4 def initialize(ffi_name, rb_name)
8 @name = File.basename rb_name, '.rb'
10 file = File.read @ffi_name
12 new_file = file.gsub(/^( *)@@@(.*?)@@@/m) do
17 original_lines = $2.count "\n"
22 @constants.each { |c| new_lines << c.to_ruby }
23 @structs.each { |s| new_lines << s.generate_layout }
25 new_lines = new_lines.join("\n").split "\n" # expand multiline blocks
26 new_lines = new_lines.map { |line| indent + line }
28 padding = original_lines - new_lines.length
29 new_lines += [nil] * padding if padding >= 0
34 open @rb_name, 'w' do |f|
35 f.puts "# This file is generated by rake. Do not edit."
41 def constants(options = {}, &block)
42 @constants << FFI::ConstGenerator.new(@name, options, &block)
46 @structs << FFI::StructGenerator.new(@name, &block)
50 # Utility converter for constants
53 proc { |obj| obj.to_s.inspect }