Sha256: 46a314c29259ec0cc8ef7d92330d48be9bb50a31a571fdb696d39d515a66a74e

Contents?: true

Size: 1.38 KB

Versions: 231

Compression:

Stored size: 1.38 KB

Contents

module FFI

  # @private
  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, @ffi_name, $`.count("\n")

        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

231 entries across 173 versions & 29 rubygems

Version Path
sidekiq-statsd-0.1.0 vendor/ruby/1.9.1/gems/ffi-1.8.1/lib/ffi/tools/generator.rb
vagrant-shell-0.2.5 vendor/bundle/gems/ffi-1.8.1/lib/ffi/tools/generator.rb
yakg-0.0.8 vendor/gems/ruby/1.9.1/gems/ffi-1.8.1/lib/ffi/tools/generator.rb
yakg-0.0.8 vendor/gems/ruby/1.8/gems/ffi-1.8.1/lib/ffi/tools/generator.rb
yakg-0.0.8 vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/tools/generator.rb
librarian-puppet-0.9.9 vendor/gems/ruby/1.9.1/gems/ffi-1.3.1/lib/ffi/tools/generator.rb
yakg-0.0.6 vendor/gems/ruby/1.8/gems/ffi-1.8.1/lib/ffi/tools/generator.rb
yakg-0.0.6 vendor/gems/ruby/1.9.1/gems/ffi-1.8.1/lib/ffi/tools/generator.rb
yakg-0.0.4 vendor/gems/ruby/1.8/gems/ffi-1.8.1/lib/ffi/tools/generator.rb
yakg-0.0.3 vendor/gems/ruby/1.8/gems/ffi-1.8.1/lib/ffi/tools/generator.rb
ffi-1.8.1-x86-mingw32 lib/ffi/tools/generator.rb
ffi-1.8.1 lib/ffi/tools/generator.rb
ffi-1.7.0-x86-mingw32 lib/ffi/tools/generator.rb
ffi-1.7.0 lib/ffi/tools/generator.rb
ffi-1.7.0.dev lib/ffi/tools/generator.rb
ffi-1.6.0-x86-mingw32 lib/ffi/tools/generator.rb
ffi-1.6.0 lib/ffi/tools/generator.rb
ffi-1.5.0-x86-mingw32 lib/ffi/tools/generator.rb
ffi-1.5.0 lib/ffi/tools/generator.rb
vagrant-actionio-0.0.9 vendor/bundle/gems/ffi-1.4.0/lib/ffi/tools/generator.rb