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
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/ffi-1.9.25/lib/ffi/tools/generator.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.4.0/gems/ffi-1.9.18/lib/ffi/tools/generator.rb
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.5.0/gems/ffi-1.10.0/lib/ffi/tools/generator.rb
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.6.0/gems/ffi-1.10.0/lib/ffi/tools/generator.rb
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.5.0/gems/ffi-1.9.25/lib/ffi/tools/generator.rb
dadapush_client-1.0.1 vendor/bundle/ruby/2.3.0/gems/ffi-1.9.25/lib/ffi/tools/generator.rb
tdiary-5.0.13 vendor/bundle/gems/ffi-1.10.0/lib/ffi/tools/generator.rb
approveapi-1.0.8 vendor/bundle/ruby/2.6.0/gems/ffi-1.10.0/lib/ffi/tools/generator.rb
tdiary-5.0.12.1 vendor/bundle/gems/ffi-1.10.0/lib/ffi/tools/generator.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.5.0/gems/ffi-1.9.25/lib/ffi/tools/generator.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.5.0/gems/ffi-1.10.0/lib/ffi/tools/generator.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.6.0/gems/ffi-1.10.0/lib/ffi/tools/generator.rb
approveapi-1.0.5 vendor/bundle/ruby/2.6.0/gems/ffi-1.10.0/lib/ffi/tools/generator.rb
alimentos-alu0100945645-0.1.0 vendor/bundle/ruby/2.3.0/gems/ffi-1.9.25/lib/ffi/tools/generator.rb
alimentos-alu0100945645-1.0.0 vendor/bundle/ruby/2.3.0/gems/ffi-1.9.25/lib/ffi/tools/generator.rb
vagrant-unbundled-2.2.3.0 vendor/bundle/ruby/2.5.0/gems/ffi-1.9.25/lib/ffi/tools/generator.rb
vagrant-unbundled-2.2.3.0 vendor/bundle/ruby/2.5.0/gems/ffi-1.10.0/lib/ffi/tools/generator.rb
ffi-1.10.0-x64-mingw32 lib/ffi/tools/generator.rb
ffi-1.10.0-x86-mingw32 lib/ffi/tools/generator.rb
ffi-1.10.0 lib/ffi/tools/generator.rb