Sha256: 5dcc4956bd68e70b984746436f2bf1d0b0cbc072173d122625209c74b16bf721

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

module RbPlusPlus
  module Writers
    # Writer that takes a builder and writes out the code in
    # one single file
    class SingleFileWriter < Base

      def initialize(builder, working_dir)
        super

        @includes = []
        @declarations = []
        @registrations = []

        # Keep track of the code generated by the global nodes
        @global_hpp = []
        @global_cpp = []
      end

      def write
        process_code(builder)

        filename = builder.name
        cpp_file = File.join(working_dir, "#{filename}.rb.cpp")

        File.open(cpp_file, "w+") do |cpp|

          cpp.puts @includes.flatten.compact.uniq.sort.reverse.join("\n")
          cpp.puts @global_hpp.flatten.compact.join("\n")
          cpp.puts @declarations.flatten.compact.join("\n")
          cpp.puts @global_cpp.flatten.compact.join("\n")
          cpp.puts @registrations.flatten.compact.join("\n")
          cpp.puts "}" # Yeah, need to figure this one out

        end
      end

      protected

      # What we do here is to go through the builder heirarchy
      # and push all the code from children up to the parent,
      # ending up with all the code in the top-level builder
      def process_code(builder)
        builder.write

        @includes << builder.includes
        @declarations << builder.declarations
        @registrations << builder.registrations

        # Process the globals
        builder.global_nodes.each do |g|
          g.write
          @includes << g.includes
          @global_hpp << g.declarations
          @global_cpp << g.registrations
        end

        builder.nodes.each do |b|
          process_code(b)
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rbplusplus-0.9 lib/rbplusplus/writers/single_file_writer.rb