Sha256: 45bab790f3121d32d5cb709a75c72f8518a74b249f5898318863978f5c8331ee

Contents?: true

Size: 1.73 KB

Versions: 8

Compression:

Stored size: 1.73 KB

Contents

require 'yaml'
require 'erb'

class RagelTask
  RL_OUTPUT_DIR = File.dirname(__FILE__) + "/../ragel/i18n"

  def initialize(lang, i18n)
    @lang     = lang
    @i18n     = i18n
    define_tasks
  end

  def define_tasks
    file target => [lang_ragel, common_ragel] do
      mkdir_p(File.dirname(target)) unless File.directory?(File.dirname(target))
      sh "ragel #{flags} #{lang_ragel} -o #{target}"
    end

    file lang_ragel => lang_erb do
      write(ERB.new(IO.read(lang_erb)).result(binding), lang_ragel)
    end

    file common_ragel => common_erb  do
      write(ERB.new(IO.read(common_erb)).result(binding), common_ragel)
    end
  end

  def target
    {
      'c'    => "ext/gherkin_lexer_#{@i18n.sanitized_key}/gherkin_lexer_#{@i18n.sanitized_key}.c",
      'java' => "java/src/main/java/gherkin/lexer/#{@i18n.sanitized_key.capitalize}.java",
      'rb'   => "lib/gherkin/rb_lexer/#{@i18n.sanitized_key}.rb",
      'csharp'   => "tmp/#{@i18n.sanitized_key}.cs"
    }[@lang]
  end

  def common_ragel
    RL_OUTPUT_DIR + "/lexer_common.#{@i18n.sanitized_key}.rl"
  end

  def common_erb
    File.dirname(__FILE__) + '/../ragel/lexer_common.rl.erb'
  end

  def lang_ragel
    RL_OUTPUT_DIR + "/#{@i18n.sanitized_key}.#{@lang}.rl"
  end

  def lang_erb
    File.dirname(__FILE__) + "/../ragel/lexer.#{@lang}.rl.erb"
  end

  def flags
    {
      'c'      => '-C',
      'java'   => '-J',
      'rb'     => '-R',
      'csharp' => '-A -L -T0'
    }[@lang]
  end

  def write(content, filename)
    mkdir_p(File.dirname(filename)) unless File.directory?(File.dirname(filename))
    File.open(filename, "wb") do |file|
      file.write(content)
    end
  end

  def ragel_list(keywords)
    "(#{keywords.map{|keyword| %{"#{keyword}"}}.join(' | ')})"
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gherkin-1.0.5-i386-mswin32 tasks/ragel_task.rb
gherkin-1.0.5-i386-mingw32 tasks/ragel_task.rb
gherkin-1.0.5-java tasks/ragel_task.rb
gherkin-1.0.5 tasks/ragel_task.rb
gherkin-1.0.4-i386-mswin32 tasks/ragel_task.rb
gherkin-1.0.4-i386-mingw32 tasks/ragel_task.rb
gherkin-1.0.4-java tasks/ragel_task.rb
gherkin-1.0.4 tasks/ragel_task.rb