Sha256: 83f7ed67e992224c7cf13e7d6896471ddecd763a04768d5bf4db8ca822dae863

Contents?: true

Size: 1.34 KB

Versions: 49

Compression:

Stored size: 1.34 KB

Contents

module FFI
  class Generator

    def initialize(ffi_name, rb_name, options = {})
      @ffi_name = ffi_name
      @rb_name = rb_name
      @options = options
      @name = File.basename rb_name, '.rb'

      file = File.read @ffi_name

      new_file = file.gsub(/^( *)@@@(.*?)@@@/m) do
        @constants = []
        @structs = []

        indent = $1
        original_lines = $2.count "\n"

        instance_eval $2

        new_lines = []
        @constants.each { |c| new_lines << c.to_ruby }
        @structs.each { |s| new_lines << s.generate_layout }

        new_lines = new_lines.join("\n").split "\n" # expand multiline blocks
        new_lines = new_lines.map { |line| indent + line }

        padding = original_lines - new_lines.length
        new_lines += [nil] * padding if padding >= 0

        new_lines.join "\n"
      end

      open @rb_name, 'w' do |f|
        f.puts "# This file is generated by rake. Do not edit."
        f.puts
        f.puts new_file
      end
    end

    def constants(options = {}, &block)
      @constants << FFI::ConstGenerator.new(@name, @options.merge(options), &block)
    end

    def struct(options = {}, &block)
      @structs << FFI::StructGenerator.new(@name, @options.merge(options), &block)
    end

    ##
    # Utility converter for constants

    def to_s
      proc { |obj| obj.to_s.inspect }
    end

  end
end

Version data entries

49 entries across 49 versions & 6 rubygems

Version Path
ffi-ffi-0.5.0 lib/ffi/tools/generator.rb
remogatto-ffi-0.5.0 lib/ffi/tools/generator.rb
ffi-0.6.4 lib/ffi/tools/generator.rb
resque-pool-0.3.0 vendor/bundle/ruby/1.8/gems/ffi-0.6.3/lib/ffi/tools/generator.rb
resque-pool-0.3.0.beta.2 vendor/bundle/ruby/1.8/gems/ffi-0.6.3/lib/ffi/tools/generator.rb
ffi-1.0.7-x86-mingw32 lib/ffi/tools/generator.rb
ffi-1.0.7 lib/ffi/tools/generator.rb
ffi-1.0.6-x86-mingw32 lib/ffi/tools/generator.rb
ffi-1.0.6 lib/ffi/tools/generator.rb
ffi-1.0.5-x86-mingw32 lib/ffi/tools/generator.rb
ffi-1.0.5 lib/ffi/tools/generator.rb
ffi-1.0.4-x86-mingw32 lib/ffi/tools/generator.rb
ffi-1.0.4 lib/ffi/tools/generator.rb
ffi-1.0.3-x86-mingw32 lib/ffi/tools/generator.rb
ffi-1.0.3 lib/ffi/tools/generator.rb
ffi-1.0.2-x86-mingw32 lib/ffi/tools/generator.rb
ffi-1.0.2 lib/ffi/tools/generator.rb
ffi-1.0.1-x86-mingw32 lib/ffi/tools/generator.rb
ffi-1.0.1 lib/ffi/tools/generator.rb
ffi-1.0.0 lib/ffi/tools/generator.rb