From aa40010fbdf3273068c7d9fcec6be8d4ab3a687b Mon Sep 17 00:00:00 2001 From: Sungwon Lee Date: Thu, 12 Jan 2012 05:36:43 +0900 Subject: [PATCH] fix bug of dust_template, add dust_cache --- lib/jasmine/headless.rb | 2 ++ lib/jasmine/headless/dust_cache.rb | 36 +++++++++++++++++++ lib/jasmine/headless/dust_template.rb | 51 +++++++++++---------------- 3 files changed, 59 insertions(+), 30 deletions(-) create mode 100644 lib/jasmine/headless/dust_cache.rb diff --git a/lib/jasmine/headless.rb b/lib/jasmine/headless.rb index 0a60efa..cb2dba7 100644 --- a/lib/jasmine/headless.rb +++ b/lib/jasmine/headless.rb @@ -8,6 +8,7 @@ module Jasmine autoload :CommandLine, 'jasmine/headless/command_line' autoload :CoffeeScriptCache, 'jasmine/headless/coffee_script_cache' + autoload :DustCache, 'jasmine/headless/dust_cache' autoload :SpecFileAnalyzer, 'jasmine/headless/spec_file_analyzer' autoload :CacheableAction, 'jasmine/headless/cacheable_action' autoload :VERSION, 'jasmine/headless/version' @@ -23,6 +24,7 @@ module Jasmine autoload :FileChecker, 'jasmine/headless/file_checker' autoload :CoffeeTemplate, 'jasmine/headless/coffee_template' + autoload :DustTemplate, 'jasmine/headless/dust_template' autoload :JSTemplate, 'jasmine/headless/js_template' autoload :JSTTemplate, 'jasmine/headless/jst_template' autoload :CSSTemplate, 'jasmine/headless/css_template' diff --git a/lib/jasmine/headless/dust_cache.rb b/lib/jasmine/headless/dust_cache.rb new file mode 100644 index 0000000..eb7cf0b --- /dev/null +++ b/lib/jasmine/headless/dust_cache.rb @@ -0,0 +1,36 @@ +require 'execjs' +require 'digest/sha1' +require 'fileutils' + +module Jasmine + module Headless + class DustCache < CacheableAction + class << self + def cache_type + "dust" + end + end + + module Source + def self.path + @path ||= File.expand_path('../../../../vendor/assets/javascripts/dust-full-for-compile.js', __FILE__) + end + + def self.contents + @contents ||= File.read(path) + end + + def self.context + @context ||= ExecJS.compile(contents) + end + + end + + def action + template_root = "app/assets/javascripts/templates/" + template_name = file.split(template_root).last.split('.',2).first + Source.context.call("dust.compile", File.read(file), template_name) + end + end + end +end diff --git a/lib/jasmine/headless/dust_template.rb b/lib/jasmine/headless/dust_template.rb index f79edba..3aade1d 100644 --- a/lib/jasmine/headless/dust_template.rb +++ b/lib/jasmine/headless/dust_template.rb @@ -4,38 +4,29 @@ require 'execjs' module Jasmine::Headless class DustTemplate < Tilt::Template include Jasmine::Headless::FileChecker - - module Source - def self.path - @path ||= File.expand_path('../../../vendor/assets/javascripts/dust-full-for-compile.js', __FILE__) + + self.default_mime_type = 'application/javascript' + + def prepare; end + + def evaluate(scope, locals, &block) + if bad_format?(file) + alert_bad_format(file) + return '' end - - def self.contents - @contents ||= File.read(path) - end - - def self.context - @context ||= ExecJS.compile(contents) - end - - end - - class DustTemplate < ::Tilt::Template - - def self.default_mime_type - 'application/javascript' - end - - def prepare - end - - def evaluate(scope, locals, &block) - template_root = Dust.config.template_root - template_name = file.split(template_root).last.split('.',2).first - Source.context.call("dust.compile", data, template_name) + begin + cache = Jasmine::Headless::DustCache.new(file) + source = cache.handle + if cache.cached? + %{ + } + else + %{} + end + rescue StandardError => e + puts "[%s] Error in compiling file: %s" % [ 'coffeescript'.color(:red), file.color(:yellow) ] + raise e end end - end end -