Sha256: 27c47daa6827771851cc051d14a8179aa411ce8de67bc8c4d8d8c69d362e4c98

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module MRubyTools
  def self.rb2c(rb_filename, indent: '  ')
    c_str = File.read(rb_filename)
    size = c_str.size
    c_str = c_str.gsub("\n", '\n').gsub('"', '\"')
    c_str = File.read(rb_filename).gsub("\n", '\n').gsub('"', '\"')
    [ "/* #{rb_filename} */",
      'mrb_load_nstring(mrb, "' + c_str + '", ' + "#{size});",
      "check_exc(mrb, \"#{rb_filename}\");",
    ].map { |s| indent + s }.join("\n")
  end

  def self.mruby_src_dir(env_var = 'MRUBY_SRC')
    mruby_src_dir = ENV[env_var]
    raise "env: MRUBY_SRC is required" unless mruby_src_dir
    raise "bad MRUBY_SRC #{mruby_src_dir}" unless File.directory? mruby_src_dir
    mruby_inc_dir = File.join(mruby_src_dir, 'include')
    raise "bad MRUBY_SRC #{mruby_inc_dir}" unless File.directory? mruby_inc_dir
    mruby_src_dir
  end

  def self.args(argv = ARGV)
    rb_files = []
    outfile = nil
    cfile = nil
    verbose = false

    while !argv.empty?
      arg = argv.shift
      if arg == '-o'
        outfile = argv.shift
        raise "no outfile provided with -o" unless outfile
        raise "#{outfile} is misnamed" if File.extname(outfile) == '.rb'
      elsif arg == '-c'
        cfile = File.open(argv.shift || 'generated.c', "w")
      elsif arg == '-v'
        verbose = true
      else
        rb_files << arg
      end
    end

    { verbose: verbose,
      cfile: cfile,
      outfile: outfile || 'outfile',
      rb_files: rb_files }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mruby_tools-0.0.1.1 lib/mruby_tools.rb