Sha256: 448735038cbb8c8d8b1f49f8405ef42722b52f1a19459a9b1cd0385438e83952

Contents?: true

Size: 1.37 KB

Versions: 120

Compression:

Stored size: 1.37 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, @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

120 entries across 120 versions & 9 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/ffi-1.0.11/lib/ffi/tools/generator.rb
classiccms-0.7.4 vendor/bundle/gems/ffi-1.0.11/lib/ffi/tools/generator.rb
classiccms-0.7.3 vendor/bundle/gems/ffi-1.0.11/lib/ffi/tools/generator.rb
classiccms-0.7.2 vendor/bundle/gems/ffi-1.0.11/lib/ffi/tools/generator.rb
classiccms-0.7.1 vendor/bundle/gems/ffi-1.0.11/lib/ffi/tools/generator.rb
classiccms-0.7.0 vendor/bundle/gems/ffi-1.0.11/lib/ffi/tools/generator.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/ffi-1.2.0/lib/ffi/tools/generator.rb
ffi-1.2.1-x86-mingw32 lib/ffi/tools/generator.rb
ffi-1.2.1 lib/ffi/tools/generator.rb
classiccms-0.6.9 vendor/bundle/gems/ffi-1.0.11/lib/ffi/tools/generator.rb
ffi-1.2.0-x86-mingw32 lib/ffi/tools/generator.rb
ffi-1.2.0 lib/ffi/tools/generator.rb
ffi-1.2.0.pre6-x86-mingw32 lib/ffi/tools/generator.rb
ffi-1.2.0.pre6 lib/ffi/tools/generator.rb
ffi-1.2.0.pre5 lib/ffi/tools/generator.rb
ffi-1.2.0.pre4 lib/ffi/tools/generator.rb
classiccms-0.6.8 vendor/bundle/gems/ffi-1.0.11/lib/ffi/tools/generator.rb
ffi-1.2.0.pre3-x86-mingw32 lib/ffi/tools/generator.rb
ffi-1.2.0.pre3 lib/ffi/tools/generator.rb
classiccms-0.6.7 vendor/bundle/gems/ffi-1.0.11/lib/ffi/tools/generator.rb