From 2dc1e03aff439cc39f4779e482dbb74d917a524c Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 8 Feb 2012 12:41:18 -0500 Subject: [PATCH] also handle jst and css base files --- Guardfile | 2 +- features/process_with_sprockets.feature | 23 ++++++++++-- .../should_be_temp_file_for_local_path.rb | 4 +- features/support/env.rb | 37 ++++--------------- flowerbox-delivery.gemspec | 2 + lib/flowerbox/delivery/sprockets_handler.rb | 5 +++ lib/flowerbox/delivery/tilt.rb | 4 ++ lib/flowerbox/delivery/tilt/css_template.rb | 14 +++++++ lib/flowerbox/delivery/tilt/js_template.rb | 22 ++--------- lib/flowerbox/delivery/tilt/jst_template.rb | 14 +++++++ .../delivery/tilt/template_that_saves.rb | 24 ++++++++++++ 11 files changed, 96 insertions(+), 55 deletions(-) create mode 100644 lib/flowerbox/delivery/tilt/css_template.rb create mode 100644 lib/flowerbox/delivery/tilt/jst_template.rb create mode 100644 lib/flowerbox/delivery/tilt/template_that_saves.rb diff --git a/Guardfile b/Guardfile index bab3f01..af54542 100644 --- a/Guardfile +++ b/Guardfile @@ -18,7 +18,7 @@ guard 'rspec', :version => 2 do end -guard 'cucumber', :cli => '-f pretty' do +guard 'cucumber', :cli => '-f pretty -b' do watch(%r{^features/.+\.feature$}) watch(%r{^features/support/.+$}) { 'features' } watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' } diff --git a/features/process_with_sprockets.feature b/features/process_with_sprockets.feature index d770abb..279229f 100644 --- a/features/process_with_sprockets.feature +++ b/features/process_with_sprockets.feature @@ -10,7 +10,6 @@ Feature: Process files with Sprockets another file """ - @fakefs Scenario: Simple Sprockets work When I instantiate a Sprockets handler with the following asset directories: | dir | @@ -19,7 +18,6 @@ Feature: Process files with Sprockets | dir/other.js | | dir/file.js | - @fakefs Scenario: Require the file twice Given I have the file "dir/third.js" with the content: """ @@ -35,18 +33,35 @@ Feature: Process files with Sprockets | dir/file.js | | dir/third.js | - @realfs - Scenario: A CoffeeScript file + Scenario: Other File Sources Given I have the file "dir/third.js.coffee" with the content: """ #= require other for file in [ 'files' ] alert(file) """ + And I have the file "dir/fourth.jst.ejs" with the content: + """ + + """ + And I have the file "dir/five.css.scss" with the content: + """ + @mixin cat { + background: green; + } + h1 { @include cat } + """ When I instantiate a Sprockets handler with the following asset directories: | dir | And I work with the Sprockets asset "third" + And I work with the Sprockets asset "fourth" + And I work with the Sprockets asset "five" Then the handler should have the following files in order: | dir/other.js | | <%= temp_path_for("dir/third.js.coffee") %> | + | <%= temp_path_for("dir/fourth.jst.ejs") %> | + | <%= temp_path_for("dir/five.css.scss") %> | And there should be a temp file for the local path "dir/third.js.coffee" + And there should be a temp file for the local path "dir/fourth.jst.ejs" + And there should be a temp file for the local path "dir/five.css.scss" + diff --git a/features/step_definitions/then/temp_file/should_be_temp_file_for_local_path.rb b/features/step_definitions/then/temp_file/should_be_temp_file_for_local_path.rb index cd6d6fa..c8f2ba7 100644 --- a/features/step_definitions/then/temp_file/should_be_temp_file_for_local_path.rb +++ b/features/step_definitions/then/temp_file/should_be_temp_file_for_local_path.rb @@ -1,7 +1,7 @@ Then /^there should be a temp file for the local path "([^"]*)"$/ do |path| - path.gsub!(%r{\.js.*$}, '.js') + parts = path.split('.')[0..1].join('.') - @temp_file_path = Dir[".tmp/**/#{path}"].first + @temp_file_path = Dir[".tmp/**/#{parts}"].first @temp_file_path.should_not be_nil end diff --git a/features/support/env.rb b/features/support/env.rb index 4c3258b..3408520 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -1,24 +1,13 @@ require 'flowerbox-delivery' require 'mocha' -require 'fakefs/safe' World(Mocha::API) -class FakeFS::File::Stat - def file? - File.file?(@file) - end -end - -class FakeFS::File - def self.executable?(file) - false - end -end - Before do mocha_setup + + FileUtils.rm_rf 'dir' end After do @@ -36,25 +25,13 @@ After do end @server.stop if @server + + FileUtils.rm_rf 'dir' end def temp_path_for(file) - File.join('.tmp/sprockets', File.expand_path(file.gsub(%r{\.js.*$}, '.js'))) + parts = file.split('.')[0..1].join('.') + + File.join('.tmp/sprockets', File.expand_path(parts)) end -Before('@fakefs') do - FakeFS.activate! -end - -After('@fakefs') do - FakeFS::FileSystem.clear - FakeFS.deactivate! -end - -Before('@realfs') do - FileUtils.rm_rf 'dir' -end - -After('@realfs') do - FileUtils.rm_rf 'dir' -end diff --git a/flowerbox-delivery.gemspec b/flowerbox-delivery.gemspec index ab2c832..a956c6e 100644 --- a/flowerbox-delivery.gemspec +++ b/flowerbox-delivery.gemspec @@ -22,6 +22,8 @@ Gem::Specification.new do |gem| gem.add_development_dependency 'fakefs' gem.add_development_dependency 'nokogiri' gem.add_development_dependency 'therubyracer' + gem.add_development_dependency 'sass' + gem.add_development_dependency 'ejs' gem.add_runtime_dependency 'rack' gem.add_runtime_dependency 'sprockets' diff --git a/lib/flowerbox/delivery/sprockets_handler.rb b/lib/flowerbox/delivery/sprockets_handler.rb index 3c76634..29bb471 100644 --- a/lib/flowerbox/delivery/sprockets_handler.rb +++ b/lib/flowerbox/delivery/sprockets_handler.rb @@ -1,4 +1,5 @@ require 'sprockets' +require 'sprockets/engines' module Flowerbox::Delivery class SprocketsHandler @@ -23,7 +24,11 @@ module Flowerbox::Delivery @environment = Sprockets::Environment.new @environment.unregister_postprocessor('application/javascript', Sprockets::SafetyColons) + @environment.unregister_bundle_processor('text/css', Sprockets::CharsetNormalizer) @environment.register_engine('.js', Flowerbox::Delivery::Tilt::JSTemplate) + @environment.register_engine('.css', Flowerbox::Delivery::Tilt::CSSTemplate) + @environment.register_engine('.jst', Flowerbox::Delivery::Tilt::JSTTemplate) + options[:asset_paths].each { |path| @environment.append_path(path) } @environment diff --git a/lib/flowerbox/delivery/tilt.rb b/lib/flowerbox/delivery/tilt.rb index 71a7d05..97d3fed 100644 --- a/lib/flowerbox/delivery/tilt.rb +++ b/lib/flowerbox/delivery/tilt.rb @@ -2,5 +2,9 @@ require 'sprockets' module Flowerbox::Delivery::Tilt autoload :JSTemplate, 'flowerbox/delivery/tilt/js_template' + autoload :JSTTemplate, 'flowerbox/delivery/tilt/jst_template' + autoload :CSSTemplate, 'flowerbox/delivery/tilt/css_template' + + autoload :TemplateThatSaves, 'flowerbox/delivery/tilt/template_that_saves' end diff --git a/lib/flowerbox/delivery/tilt/css_template.rb b/lib/flowerbox/delivery/tilt/css_template.rb new file mode 100644 index 0000000..7036447 --- /dev/null +++ b/lib/flowerbox/delivery/tilt/css_template.rb @@ -0,0 +1,14 @@ +require 'tilt' + +class Flowerbox::Delivery::Tilt::CSSTemplate < Tilt::Template + self.default_mime_type = "text/css" + + EXTENSION = "css" + + include Flowerbox::Delivery::Tilt::TemplateThatSaves + + def evaluate(scope, locals, &block) + handle_evaluate + end +end + diff --git a/lib/flowerbox/delivery/tilt/js_template.rb b/lib/flowerbox/delivery/tilt/js_template.rb index 31f6c75..6d26129 100644 --- a/lib/flowerbox/delivery/tilt/js_template.rb +++ b/lib/flowerbox/delivery/tilt/js_template.rb @@ -3,26 +3,12 @@ require 'tilt' class Flowerbox::Delivery::Tilt::JSTemplate < Tilt::Template self.default_mime_type = "application/javascript" - def prepare ; end + EXTENSION = "js" + + include Flowerbox::Delivery::Tilt::TemplateThatSaves def evaluate(scope, locals, &block) - case File.extname(file) - when '.js' - file - else - save - end - end - - def save - FileUtils.mkdir_p File.dirname(temp_file) - File.open(temp_file, 'wb') { |fh| fh.print data } - - temp_file - end - - def temp_file - File.join(Dir.pwd, ".tmp/sprockets", file.gsub(%r{(\.js)(.*)$}, '\1')) + handle_evaluate end end diff --git a/lib/flowerbox/delivery/tilt/jst_template.rb b/lib/flowerbox/delivery/tilt/jst_template.rb new file mode 100644 index 0000000..850f087 --- /dev/null +++ b/lib/flowerbox/delivery/tilt/jst_template.rb @@ -0,0 +1,14 @@ +require 'tilt' + +class Flowerbox::Delivery::Tilt::JSTTemplate < Sprockets::JstProcessor + EXTENSION = "jst" + + include Flowerbox::Delivery::Tilt::TemplateThatSaves + + def evaluate(scope, locals, &block) + super + + handle_evaluate + end +end + diff --git a/lib/flowerbox/delivery/tilt/template_that_saves.rb b/lib/flowerbox/delivery/tilt/template_that_saves.rb new file mode 100644 index 0000000..2df84be --- /dev/null +++ b/lib/flowerbox/delivery/tilt/template_that_saves.rb @@ -0,0 +1,24 @@ +module Flowerbox::Delivery::Tilt::TemplateThatSaves + def prepare ; end + + def handle_evaluate + case File.extname(file) + when '.js' + file + else + save + end + end + + def save + FileUtils.mkdir_p File.dirname(temp_file) + File.open(temp_file, 'wb') { |fh| fh.print data } + + temp_file + end + + def temp_file + File.join(Dir.pwd, ".tmp/sprockets", file.gsub(%r{(\.#{self.class::EXTENSION})(.*)$}, '\1')) + end +end +