Patch for unsupported formats in sprockets #90

Merged
scottdavis merged 1 commits from master into master 2011-12-05 23:52:15 +00:00
11 changed files with 108 additions and 3 deletions

View File

@ -2,6 +2,9 @@ require 'pathname'
module Jasmine module Jasmine
module Headless module Headless
EXCLUDED_FORMATS = %w{less sass scss erb str}
autoload :CommandLine, 'jasmine/headless/command_line' autoload :CommandLine, 'jasmine/headless/command_line'
autoload :CoffeeScriptCache, 'jasmine/headless/coffee_script_cache' autoload :CoffeeScriptCache, 'jasmine/headless/coffee_script_cache'
@ -17,10 +20,13 @@ module Jasmine
autoload :TemplateWriter, 'jasmine/headless/template_writer' autoload :TemplateWriter, 'jasmine/headless/template_writer'
autoload :FileChecker, 'jasmine/headless/file_checker'
autoload :CoffeeTemplate, 'jasmine/headless/coffee_template' autoload :CoffeeTemplate, 'jasmine/headless/coffee_template'
autoload :JSTemplate, 'jasmine/headless/js_template' autoload :JSTemplate, 'jasmine/headless/js_template'
autoload :JSTTemplate, 'jasmine/headless/jst_template' autoload :JSTTemplate, 'jasmine/headless/jst_template'
autoload :CSSTemplate, 'jasmine/headless/css_template' autoload :CSSTemplate, 'jasmine/headless/css_template'
autoload :NilTemplate, 'jasmine/headless/nil_template'
autoload :Report, 'jasmine/headless/report' autoload :Report, 'jasmine/headless/report'
autoload :ReportMessage, 'jasmine/headless/report_message' autoload :ReportMessage, 'jasmine/headless/report_message'

View File

@ -3,11 +3,17 @@ require 'rainbow'
module Jasmine::Headless module Jasmine::Headless
class CoffeeTemplate < Tilt::Template class CoffeeTemplate < Tilt::Template
include Jasmine::Headless::FileChecker
self.default_mime_type = 'application/javascript' self.default_mime_type = 'application/javascript'
def prepare ; end def prepare ; end
def evaluate(scope, locals, &block) def evaluate(scope, locals, &block)
if bad_format?(file)
alert_bad_format(file)
return ''
end
begin begin
cache = Jasmine::Headless::CoffeeScriptCache.new(file) cache = Jasmine::Headless::CoffeeScriptCache.new(file)
source = cache.handle source = cache.handle

View File

@ -2,11 +2,16 @@ require 'tilt/template'
module Jasmine::Headless module Jasmine::Headless
class CSSTemplate < Tilt::Template class CSSTemplate < Tilt::Template
include Jasmine::Headless::FileChecker
self.default_mime_type = 'text/css' self.default_mime_type = 'text/css'
def prepare ; end def prepare ; end
def evaluate(scope, locals, &block) def evaluate(scope, locals, &block)
if bad_format?(file)
alert_bad_format(file)
return ''
end
file ? %{<link rel="stylesheet" href="#{file}" type="text/css" />} : data file ? %{<link rel="stylesheet" href="#{file}" type="text/css" />} : data
end end
end end

View File

@ -0,0 +1,10 @@
module Jasmine::Headless::FileChecker
def bad_format?(file)
return if file.nil?
::Jasmine::Headless::EXCLUDED_FORMATS.any? {|format| file.include?(".#{format}") }
end
def alert_bad_format(file)
puts "[%s] %s: %s" % [ 'Skipping File'.color(:red), file.color(:yellow), "unsupported format".color(:white) ]
end
end

View File

@ -6,6 +6,7 @@ require 'sprockets'
require 'sprockets/engines' require 'sprockets/engines'
module Jasmine::Headless module Jasmine::Headless
class FilesList class FilesList
class << self class << self
def vendor_asset_paths def vendor_asset_paths
@ -39,8 +40,8 @@ module Jasmine::Headless
# ...and unregister ones we don't want/need # ...and unregister ones we don't want/need
Sprockets.instance_eval do Sprockets.instance_eval do
%w{less sass scss erb str}.each do |extension| EXCLUDED_FORMATS.each do |extension|
@engines.delete(".#{extension}") register_engine ".#{extension}", Jasmine::Headless::NilTemplate
end end
register_engine '.coffee', Jasmine::Headless::CoffeeTemplate register_engine '.coffee', Jasmine::Headless::CoffeeTemplate

View File

@ -2,11 +2,16 @@ require 'tilt/template'
module Jasmine::Headless module Jasmine::Headless
class JSTemplate < Tilt::Template class JSTemplate < Tilt::Template
include Jasmine::Headless::FileChecker
self.default_mime_type = 'application/javascript' self.default_mime_type = 'application/javascript'
def prepare ; end def prepare ; end
def evaluate(scope, locals, &block) def evaluate(scope, locals, &block)
if bad_format?(file)
alert_bad_format(file)
return ''
end
if data[%r{^<script type="text/javascript"}] if data[%r{^<script type="text/javascript"}]
data data
else else

View File

@ -2,7 +2,12 @@ require 'sprockets/jst_processor'
module Jasmine::Headless module Jasmine::Headless
class JSTTemplate < Sprockets::JstProcessor class JSTTemplate < Sprockets::JstProcessor
include Jasmine::Headless::FileChecker
def evaluate(*args) def evaluate(*args)
if bad_format?(file)
alert_bad_format(file)
return ''
end
%{<script type="text/javascript">#{super}</script>} %{<script type="text/javascript">#{super}</script>}
end end
end end

View File

@ -0,0 +1,14 @@
require 'tilt/template'
require 'rainbow'
module Jasmine::Headless
class NilTemplate < Tilt::Template
def prepare ; end
def evaluate(scope, locals, &block)
return ''
end
end
end

View File

@ -4,8 +4,8 @@ describe 'sprockets' do
it 'should pull in the code via sprockets' do it 'should pull in the code via sprockets' do
files = %x{bin/jasmine-headless-webkit -l -j spec/jasmine/with_sprockets_includes/with_sprockets_includes.yml} files = %x{bin/jasmine-headless-webkit -l -j spec/jasmine/with_sprockets_includes/with_sprockets_includes.yml}
$?.exitstatus.should == 0 $?.exitstatus.should == 0
files.lines.to_a.should contain_in_order_in_file_list( files.lines.to_a.should contain_in_order_in_file_list(
'assets/application.js.erb: unsupported format',
'vendor/assets/javascripts/jquery.js', 'vendor/assets/javascripts/jquery.js',
'templates/that.jst.ejs', 'templates/that.jst.ejs',
'templates/this.jst', 'templates/this.jst',

View File

@ -0,0 +1,22 @@
require 'spec_helper'
describe Jasmine::Headless::FileChecker do
include FakeFS::SpecHelpers
let(:test_class) do
object = Object.new
object.class.send(:include, Jasmine::Headless::FileChecker)
object
end
context "bad_format?" do
it "should return false wth correct format" do
test_class.bad_format?('foobar.js').should be_false
end
it "should return false wth wrong format" do
test_class.bad_format?('foobar.js.erb').should be_true
end
end
end

View File

@ -0,0 +1,31 @@
require 'spec_helper'
describe Jasmine::Headless::NilTemplate do
include FakeFS::SpecHelpers
let(:template) { described_class.new(file) { data } }
let(:file) { 'file' }
let(:data) { '' }
subject { template.render }
before do
File.open(file, 'wb') if file
end
context "no file'" do
let(:file) { nil }
it { should == data }
end
context 'file' do
it { should == '' }
end
context 'script as first thing' do
let(:data) { '' }
it { should == data }
end
end