fix bug of dust_template, add dust_cache

This commit is contained in:
Sungwon Lee 2012-01-12 05:36:43 +09:00
parent 72b0c80b48
commit aa40010fbd
3 changed files with 59 additions and 30 deletions

View File

@ -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'

View File

@ -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

View File

@ -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?
%{<script type="text/javascript" src="#{cache.cache_file}"></script>
<script type="text/javascript">window.CSTF['#{File.split(cache.cache_file).last}'] = '#{file}';</script>}
else
%{<script type="text/javascript">#{source}</script>}
end
rescue StandardError => e
puts "[%s] Error in compiling file: %s" % [ 'coffeescript'.color(:red), file.color(:yellow) ]
raise e
end
end
end
end