This repository has been archived by the owner on May 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
/
ragel_task.rb
executable file
·124 lines (103 loc) · 3.42 KB
/
ragel_task.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
require 'yaml'
require 'erb'
require 'rbconfig'
class RagelTask
begin
# Support Rake >= 0.9.0
require 'rake/dsl_definition'
include Rake::DSL
rescue LoadError
end
RL_OUTPUT_DIR = File.dirname(__FILE__) + "/../ragel/i18n"
UGLIFYJS = File.dirname(__FILE__) + "/../js/node_modules/uglify-js/bin/uglifyjs"
def initialize(lang, i18n)
@lang = lang
@i18n = i18n
define_tasks
end
def define_tasks
deps = [lang_ragel, common_ragel]
deps.unshift(UGLIFYJS) if(@lang == 'js')
sed = RbConfig::CONFIG['host_os'] =~ /linux/i ? "sed -i" : "sed -i ''"
min_target = target.gsub(/\.js$/, '.min.js')
file target => deps do
mkdir_p(File.dirname(target)) unless File.directory?(File.dirname(target))
ragel = @lang == 'js' ? 'js/ragel-osx' : 'ragel'
sh "#{ragel} #{flags} #{lang_ragel} -o #{target}"
# Remove absolute paths from ragel-generated sources
sh %{#{sed} "s|#{Dir.pwd}/tasks/../||" #{target}}
if(@lang == 'js')
# Ragel chokes if we put the escaped triple quotes in .rl, so we'll do the replace with sed after the fact. Lots of backslashes!!
sh %{#{sed} 's/ESCAPED_TRIPLE_QUOTE/\\\\\\\\\\\\"\\\\\\\\\\\\"\\\\\\\\\\\\"/' #{target}}
sh %{#{sed} 's/const/var/' #{target}}
# Minify
sh %{node #{UGLIFYJS} #{target} > #{min_target}}
end
if(@lang == 'rb')
# Prettify the code so we don't get indentation warnings
sh %{rbeautify #{target} > tmp.rb}
sh %{mv tmp.rb #{target}}
# rbeautify has a bug with class << self ... end alignment. Fix it.
sh %{perl -i -0pe 's/ end\n self._/ end\n self._/g' #{target}}
end
end
if(@lang != 'java')
CLEAN.include(target)
end
if(@lang == 'js')
CLEAN.include(min_target)
end
file UGLIFYJS do
unless File.exist?(UGLIFYJS)
Dir.chdir('js') do
sh "npm install"
end
end
end
file lang_ragel => lang_erb do
write(ERB.new(IO.read(lang_erb)).result(binding), lang_ragel)
end
CLEAN.include(lang_ragel)
file common_ragel => common_erb do
write(ERB.new(IO.read(common_erb)).result(binding), common_ragel)
end
CLEAN.include(common_ragel)
end
def target
{
'c' => "ext/gherkin_lexer_#{@i18n.underscored_iso_code}/gherkin_lexer_#{@i18n.underscored_iso_code}.c",
'java' => "java/src/main/java/gherkin/lexer/#{@i18n.underscored_iso_code.capitalize}.java",
'rb' => "lib/gherkin/lexer/#{@i18n.underscored_iso_code}.rb",
'js' => "js/lib/gherkin/lexer/#{@i18n.underscored_iso_code}.js"
}[@lang]
end
def common_ragel
RL_OUTPUT_DIR + "/lexer_common.#{@i18n.underscored_iso_code}.rl"
end
def common_erb
File.dirname(__FILE__) + '/../ragel/lexer_common.rl.erb'
end
def lang_ragel
RL_OUTPUT_DIR + "/#{@i18n.underscored_iso_code}.#{@lang}.rl"
end
def lang_erb
File.dirname(__FILE__) + "/../ragel/lexer.#{@lang}.rl.erb"
end
def flags
{
'c' => '-C',
'java' => '-J',
'rb' => '-R',
'js' => '-E'
}[@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