lib/coffeescript.rb in capucine-0.1.7 vs lib/coffeescript.rb in capucine-0.2.0

- old
+ new

@@ -1,40 +1,56 @@ module Capucine - class Coffee + class CoffeeScript require 'packr' require 'coffee-script' - require 'fssm' - def self.compile_dir input, output - settings = Capucine.settings + def initialize(capucine) + @cap = capucine + end + def run_once(file = nil) + + unless file + files = Dir.glob(File.join @cap.settings.working_dir, @cap.settings.conf['coffeescript_output_dir'], '*') + FileUtils.rm_r files + self.compile_dir @cap.settings.conf['coffeescript_files_dir'], @cap.settings.conf['coffeescript_output_dir'] + else + self.compile_dir file, @cap.settings.conf['coffeescript_output_dir'] + end + + puts "[coffee] - Compiled" + end + + def compile_dir(input, output) + if File.extname(input) == '.coffee' coffee_files = input else - coffee_files = File.join settings.working_dir, input, "**/**.coffee" + coffee_files = File.join @cap.settings.working_dir, input, "**/**.coffee" end Dir.glob(coffee_files).each do |file_coffee_o| file_coffee = File.expand_path file_coffee_o - base_in_dir = File.join settings.working_dir, input + base_in_dir = File.join @cap.settings.working_dir, input - relative_path = file_coffee.gsub(File.join(Capucine.settings.working_dir, Capucine.settings.config['coffeescript_files_dir'] ),'') + relative_path = file_coffee.gsub(File.join(@cap.settings.working_dir, @cap.settings.conf['coffeescript_files_dir'] ),'') + relative_path = relative_path.gsub(/\.coffee$/, '.js') relative_path_min = relative_path.gsub(/\.js$/, '.min.js') - file_out = File.join settings.working_dir, output, relative_path - file_out_min = File.join settings.working_dir, output, relative_path_min + file_out = File.join @cap.settings.working_dir, output, relative_path + file_out_min = File.join @cap.settings.working_dir, output, relative_path_min relative_coffee_file = file_coffee.gsub(base_in_dir, '') - opts = {:bare => settings.config['coffeescript_bare']} + opts = {:bare => @cap.settings.conf['coffeescript_bare']} coffee_output_min = "" error = false begin - coffee_output = CoffeeScript.compile(File.read(file_coffee), opts) + coffee_output = ::CoffeeScript.compile(File.read(file_coffee), opts) rescue Exception => e error = true message = "#{e.message}" coffee_output = "var message = \"CoffeeScript Error (#{relative_coffee_file.gsub(/^\//, '')}) => \";" coffee_output += "message += \"#{message}\";" @@ -55,41 +71,30 @@ f2.close end end - def self.run_once file = nil - settings = Capucine.settings - unless file - files = Dir.glob(File.join settings.working_dir, settings.config['coffeescript_output_dir'], '*') - FileUtils.rm_r files - self.compile_dir settings.config['coffeescript_files_dir'], settings.config['coffeescript_output_dir'] + def run_watch + require 'fssm' - else - self.compile_dir file, settings.config['coffeescript_output_dir'] - end + files_to_lookat = File.join @cap.settings.working_dir, @cap.settings.conf['coffeescript_files_dir'] + js_generated_dir = File.join @cap.settings.working_dir, @cap.settings.conf['coffeescript_output_dir'] - puts "[coffee] - Compiled" - end + coffee = @cap.coffee - def self.proc_watch - settings = Capucine.settings - - files_to_lookat = File.join settings.working_dir, settings.config['coffeescript_files_dir'] - js_generated_dir = File.join settings.working_dir, settings.config['coffeescript_output_dir'] - coffee_thread = Thread.new { + FSSM.monitor(files_to_lookat, :directories => true) do update do |b, r| file = File.join b, r - Capucine::Coffee.run_once file if File.extname(r) == '.coffee' + coffee.run_once file if File.extname(r) == '.coffee' # COFFEE end create do |b, r| file = File.join b, r - Capucine::Coffee.run_once file if File.extname(r) == '.coffee' + coffee.run_once file if File.extname(r) == '.coffee' # COFFEE end delete do |b, r| file_name = File.expand_path File.join(b, r) @@ -113,9 +118,10 @@ end } return coffee_thread + end end