diff --git a/lib/jasmine/headless.rb b/lib/jasmine/headless.rb index 58c11af..45ddf9d 100644 --- a/lib/jasmine/headless.rb +++ b/lib/jasmine/headless.rb @@ -1,5 +1,5 @@ require 'pathname' -require 'sprockets/engines' +require 'sprockets' module Jasmine::Headless autoload :CoffeeScriptCache, 'jasmine/headless/coffee_script_cache' @@ -14,6 +14,8 @@ module Jasmine::Headless autoload :TemplateWriter, 'jasmine/headless/template_writer' + autoload :CoffeeTemplate, 'jasmine/headless/coffee_template' + autoload :Report, 'jasmine/headless/report' autoload :ReportMessage, 'jasmine/headless/report_message' @@ -25,4 +27,21 @@ module Jasmine::Headless end require 'jasmine/headless/errors' -Sprockets::Engines + +# register haml-sprockets if it's available... +%w{haml-sprockets}.each do |library| + begin + require library + rescue LoadError + end +end + +# ...and unregister ones we don't want/need +module Sprockets + %w{less sass scss erb str}.each do |extension| + @engines.delete(".#{extension}") + end + + register_engine '.coffee', Jasmine::Headless::CoffeeTemplate +end + diff --git a/lib/jasmine/headless/coffee_template.rb b/lib/jasmine/headless/coffee_template.rb new file mode 100644 index 0000000..fbc8c95 --- /dev/null +++ b/lib/jasmine/headless/coffee_template.rb @@ -0,0 +1,29 @@ +require 'tilt/template' + +module Jasmine::Headless + class CoffeeTemplate < Tilt::Template + def prepare ; end + + def evaluate(scope, locals, &block) + begin + cache = Jasmine::Headless::CoffeeScriptCache.new(file) + source = cache.handle + if cache.cached? + %{ + } + else + %{} + end + rescue CoffeeScript::CompilationError => ne + puts "[%s] %s: %s" % [ 'coffeescript'.color(:red), file.color(:yellow), "#{ne.message}".color(:white) ] + raise ne + rescue StandardError => e + puts "[%s] Error in compiling file: %s" % [ 'coffeescript'.color(:red), file.color(:yellow) ] + raise e + end + end + end +end + diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index e577fef..b1311db 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -92,7 +92,7 @@ module Jasmine::Headless TestFile.new(file, source_root).dependencies.each { |type, name| add_dependency(type, name, source_root) } end - EXTENSION_FILTER = %r{\.(js|css|coffee|jst.*)$} + EXTENSION_FILTER = %r{(#{(%w{.js .css} + Sprockets.engine_extensions).join('|')})$} def add_dependency(type, file, source_root) case type diff --git a/lib/jasmine/headless/test_file.rb b/lib/jasmine/headless/test_file.rb index 52167f8..528acd4 100644 --- a/lib/jasmine/headless/test_file.rb +++ b/lib/jasmine/headless/test_file.rb @@ -1,13 +1,6 @@ require 'rainbow' require 'sprockets' -%w{haml-sprockets}.each do |library| - begin - require library - rescue LoadError - end -end - module Jasmine::Headless class TestFile attr_reader :path, :source_root @@ -21,38 +14,7 @@ module Jasmine::Headless end def to_html - case File.extname(path) - when '.coffee' - begin - cache = Jasmine::Headless::CoffeeScriptCache.new(path) - source = cache.handle - if cache.cached? - %{ - } - else - %{} - end - rescue CoffeeScript::CompilationError => ne - puts "[%s] %s: %s" % [ 'coffeescript'.color(:red), path.color(:yellow), ne.message.dup.to_s.color(:white) ] - raise ne - rescue StandardError => e - puts "[%s] Error in compiling file: %s" % [ 'coffeescript'.color(:red), path.color(:yellow) ] - raise e - end - when '.js' - %{} - when '.css' - %{} - when '.jst' - to_jst(read) - else - case path - when %r{\.jst(\..*)$} - to_jst(Sprockets.engines($1).new { read }.evaluate(self, {})) - end - end + process_data_by_filename(path) end def dependencies @@ -73,13 +35,28 @@ module Jasmine::Headless end private - def to_jst(data) - %{} - end - def read File.read(path) end + + def process_data_by_filename(path, data = nil) + case extension = File.extname(path) + when '' + data || '' + when '.js' + data || %{} + when '.css' + data || %{} + else + if engine = Sprockets.engines(extension) + data = engine.new(path) { data || read }.render(self) + + process_data_by_filename(path.gsub(%r{#{extension}$}, ''), data) + else + data || '' + end + end + end end end diff --git a/spec/lib/jasmine/headless/coffee_template_spec.rb b/spec/lib/jasmine/headless/coffee_template_spec.rb new file mode 100644 index 0000000..f07e769 --- /dev/null +++ b/spec/lib/jasmine/headless/coffee_template_spec.rb @@ -0,0 +1,55 @@ +require 'spec_helper' + +describe Jasmine::Headless::CoffeeTemplate do + let(:data) { 'data' } + let(:path) { 'path.coffee' } + + let(:template) { described_class.new(path) { data } } + + subject { template.render } + + let(:handle_expectation) { Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:handle) } + + context 'compilation error' do + let(:error) { CoffeeScript::CompilationError.new("fail") } + + before do + handle_expectation.raises(error) + end + + it 'should pass along the error' do + expect { subject }.to raise_error(CoffeeScript::CompilationError) + end + end + + context 'compiles fine' do + let(:source) { 'source' } + + before do + Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:cached?).returns(cache_return) + handle_expectation.returns(source) + end + + context 'cached' do + let(:file_path) { 'dir/file.js' } + let(:cache_return) { true } + + before do + Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:cache_file).returns(file_path) + end + + it 'should return the cached file' do + subject.should include(%{}) + end + end + + context 'not cached' do + let(:cache_return) { false } + + it 'should return the generated js' do + subject.should include(%{}) + end + end + end +end + diff --git a/spec/lib/jasmine/headless/test_file_spec.rb b/spec/lib/jasmine/headless/test_file_spec.rb index 14f053b..b19ed21 100644 --- a/spec/lib/jasmine/headless/test_file_spec.rb +++ b/spec/lib/jasmine/headless/test_file_spec.rb @@ -25,83 +25,45 @@ describe Jasmine::Headless::TestFile do it { should == %{} } end - context '.coffee' do - let(:path) { 'path.coffee' } - - let(:handle_expectation) { Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:handle) } - - context 'compilation error' do - let(:error) { CoffeeScript::CompilationError.new("fail") } - - before do - handle_expectation.raises(error) - end - - it 'should pass along the error' do - expect { subject }.to raise_error(CoffeeScript::CompilationError) - end - end - - context 'compiles fine' do - let(:source) { 'source' } - - before do - Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:cached?).returns(cache_return) - handle_expectation.returns(source) - end - - context 'cached' do - let(:file_path) { 'dir/file.js' } - let(:cache_return) { true } - - before do - Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:cache_file).returns(file_path) - end - - it 'should return the cached file' do - subject.should include(%{}) - end - end - - context 'not cached' do - let(:cache_return) { false } - - it 'should return the generated js' do - subject.should include(%{}) - end - end - end - end - - context '.jst' do + context 'with tilt template' do include FakeFS::SpecHelpers - let(:path) { 'file.jst' } let(:content) { 'content' } before do File.open(path, 'wb') { |fh| fh.print content } end - it 'should use the JST template processor to get the processed file' do - subject.should include('JST["file"]') - subject.should include(content) + let(:klass) do + Class.new(Tilt::Template) do + def prepare ; end + + def evaluate(scope, locals, &block) + "#{file} made it #{data}" + end + end end - end - - context '.jst.*' do - include FakeFS::SpecHelpers - - let(:path) { 'file.jst.ejs' } - let(:content) { 'content' } before do - File.open(path, 'wb') { |fh| fh.print content } + Sprockets.stubs(:engines).with('.tilt').returns(klass) end - it 'should use the JST template processor to get the processed file' do - subject.should include('JST["file"]') - subject.should include(content) + context '.tilt' do + let(:path) { 'path.tilt' } + + it { should == "#{path} made it #{content}" } + end + + context '.tilt.tilt' do + let(:path) { 'path.tilt.tilt' } + + it { should == "path.tilt made it #{path} made it #{content}" } + end + + context '.js.tilt' do + let(:path) { 'path.js.tilt' } + + it { should == "#{path} made it #{content}" } end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d73f675..a95be66 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,6 +3,7 @@ require 'fakefs/spec_helpers' RSpec.configure do |c| c.mock_with :mocha + c.backtrace_clean_patterns = [] c.before(:each) do Jasmine::Headless::CacheableAction.enabled = false