Class: Kango::ScriptCompiler
- Inherits:
-
Object
- Object
- Kango::ScriptCompiler
- Defined in:
- lib/kango/script_compiler.rb
Class Method Summary collapse
-
.coffee_userscript_commentblock(coffee_file) ⇒ Object
Extract userscript comment block from CoffeeScript file.
-
.compile(file, options) ⇒ Object
Compile the script to javascript.
Instance Method Summary collapse
- #compile ⇒ Object
-
#initialize(dir_glob, extension, compiler) ⇒ ScriptCompiler
constructor
A new instance of ScriptCompiler.
- #script_regex ⇒ Object
Constructor Details
#initialize(dir_glob, extension, compiler) ⇒ ScriptCompiler
Returns a new instance of ScriptCompiler.
5 6 7 8 9 |
# File 'lib/kango/script_compiler.rb', line 5 def initialize dir_glob, extension, compiler @compiler = compiler @extension = extension @files = Dir.glob("#{dir_glob}.#{extension}") end |
Class Method Details
.coffee_userscript_commentblock(coffee_file) ⇒ Object
Extract userscript comment block from CoffeeScript file.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/kango/script_compiler.rb', line 39 def self.coffee_userscript_commentblock(coffee_file) comment = "" File.open(coffee_file) do |f| while line = f.gets break unless line =~ /^#/ comment += line.gsub(/^#/, '//') end end comment end |
.compile(file, options) ⇒ Object
Compile the script to javascript
32 33 34 35 |
# File 'lib/kango/script_compiler.rb', line 32 def self.compile file, compiler = [:with] compiler.compile File.read(file), :bare => true end |
Instance Method Details
#compile ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/kango/script_compiler.rb', line 15 def compile @files.each do |file| script = file.gsub(script_regex, 'js').split('/').last path = File.join('src', 'common', script) puts "[ INFO] Compiling #{file} to #{path.to_s}" FileUtils.mkdir_p File.dirname(path) File.open(path.to_s, 'w') do |f| if @compiler.to_s == 'CoffeeScript' f.puts Kango::ScriptCompiler.coffee_userscript_commentblock file end f.puts Kango::ScriptCompiler.compile file, with: @compiler end end end |
#script_regex ⇒ Object
11 12 13 |
# File 'lib/kango/script_compiler.rb', line 11 def script_regex /(^#{@extension}|#{@extension}$)/ end |