also handle jst and css base files

This commit is contained in:
John Bintz 2012-02-08 12:41:18 -05:00
parent 9829c40730
commit 2dc1e03aff
11 changed files with 96 additions and 55 deletions

View File

@ -18,7 +18,7 @@ guard 'rspec', :version => 2 do
end end
guard 'cucumber', :cli => '-f pretty' do guard 'cucumber', :cli => '-f pretty -b' do
watch(%r{^features/.+\.feature$}) watch(%r{^features/.+\.feature$})
watch(%r{^features/support/.+$}) { 'features' } watch(%r{^features/support/.+$}) { 'features' }
watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' } watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' }

View File

@ -10,7 +10,6 @@ Feature: Process files with Sprockets
another file another file
""" """
@fakefs
Scenario: Simple Sprockets work Scenario: Simple Sprockets work
When I instantiate a Sprockets handler with the following asset directories: When I instantiate a Sprockets handler with the following asset directories:
| dir | | dir |
@ -19,7 +18,6 @@ Feature: Process files with Sprockets
| dir/other.js | | dir/other.js |
| dir/file.js | | dir/file.js |
@fakefs
Scenario: Require the file twice Scenario: Require the file twice
Given I have the file "dir/third.js" with the content: Given I have the file "dir/third.js" with the content:
""" """
@ -35,18 +33,35 @@ Feature: Process files with Sprockets
| dir/file.js | | dir/file.js |
| dir/third.js | | dir/third.js |
@realfs Scenario: Other File Sources
Scenario: A CoffeeScript file
Given I have the file "dir/third.js.coffee" with the content: Given I have the file "dir/third.js.coffee" with the content:
""" """
#= require other #= require other
for file in [ 'files' ] for file in [ 'files' ]
alert(file) alert(file)
""" """
And I have the file "dir/fourth.jst.ejs" with the content:
"""
<template><%= data %></template>
"""
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: When I instantiate a Sprockets handler with the following asset directories:
| dir | | dir |
And I work with the Sprockets asset "third" 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: Then the handler should have the following files in order:
| dir/other.js | | dir/other.js |
| <%= temp_path_for("dir/third.js.coffee") %> | | <%= 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/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"

View File

@ -1,7 +1,7 @@
Then /^there should be a temp file for the local path "([^"]*)"$/ do |path| 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 @temp_file_path.should_not be_nil
end end

View File

@ -1,24 +1,13 @@
require 'flowerbox-delivery' require 'flowerbox-delivery'
require 'mocha' require 'mocha'
require 'fakefs/safe'
World(Mocha::API) 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 Before do
mocha_setup mocha_setup
FileUtils.rm_rf 'dir'
end end
After do After do
@ -36,25 +25,13 @@ After do
end end
@server.stop if @server @server.stop if @server
FileUtils.rm_rf 'dir'
end end
def temp_path_for(file) 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 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

View File

@ -22,6 +22,8 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'fakefs' gem.add_development_dependency 'fakefs'
gem.add_development_dependency 'nokogiri' gem.add_development_dependency 'nokogiri'
gem.add_development_dependency 'therubyracer' gem.add_development_dependency 'therubyracer'
gem.add_development_dependency 'sass'
gem.add_development_dependency 'ejs'
gem.add_runtime_dependency 'rack' gem.add_runtime_dependency 'rack'
gem.add_runtime_dependency 'sprockets' gem.add_runtime_dependency 'sprockets'

View File

@ -1,4 +1,5 @@
require 'sprockets' require 'sprockets'
require 'sprockets/engines'
module Flowerbox::Delivery module Flowerbox::Delivery
class SprocketsHandler class SprocketsHandler
@ -23,7 +24,11 @@ module Flowerbox::Delivery
@environment = Sprockets::Environment.new @environment = Sprockets::Environment.new
@environment.unregister_postprocessor('application/javascript', Sprockets::SafetyColons) @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('.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) } options[:asset_paths].each { |path| @environment.append_path(path) }
@environment @environment

View File

@ -2,5 +2,9 @@ require 'sprockets'
module Flowerbox::Delivery::Tilt module Flowerbox::Delivery::Tilt
autoload :JSTemplate, 'flowerbox/delivery/tilt/js_template' 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 end

View File

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

View File

@ -3,26 +3,12 @@ require 'tilt'
class Flowerbox::Delivery::Tilt::JSTemplate < Tilt::Template class Flowerbox::Delivery::Tilt::JSTemplate < Tilt::Template
self.default_mime_type = "application/javascript" self.default_mime_type = "application/javascript"
def prepare ; end EXTENSION = "js"
include Flowerbox::Delivery::Tilt::TemplateThatSaves
def evaluate(scope, locals, &block) def evaluate(scope, locals, &block)
case File.extname(file) handle_evaluate
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'))
end end
end end

View File

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

View File

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