deal with a coffeescript file
This commit is contained in:
parent
fb5018148f
commit
9829c40730
2
.gitignore
vendored
2
.gitignore
vendored
@ -16,3 +16,5 @@ test/tmp
|
|||||||
test/version_tmp
|
test/version_tmp
|
||||||
tmp
|
tmp
|
||||||
rerun.txt
|
rerun.txt
|
||||||
|
.tmp/
|
||||||
|
template.erb
|
||||||
|
@ -10,6 +10,7 @@ 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 |
|
||||||
@ -18,6 +19,7 @@ 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:
|
||||||
"""
|
"""
|
||||||
@ -33,3 +35,18 @@ Feature: Process files with Sprockets
|
|||||||
| dir/file.js |
|
| dir/file.js |
|
||||||
| dir/third.js |
|
| dir/third.js |
|
||||||
|
|
||||||
|
@realfs
|
||||||
|
Scenario: A CoffeeScript file
|
||||||
|
Given I have the file "dir/third.js.coffee" with the content:
|
||||||
|
"""
|
||||||
|
#= require other
|
||||||
|
for file in [ 'files' ]
|
||||||
|
alert(file)
|
||||||
|
"""
|
||||||
|
When I instantiate a Sprockets handler with the following asset directories:
|
||||||
|
| dir |
|
||||||
|
And I work with the Sprockets asset "third"
|
||||||
|
Then the handler should have the following files in order:
|
||||||
|
| dir/other.js |
|
||||||
|
| <%= temp_path_for("dir/third.js.coffee") %> |
|
||||||
|
And there should be a temp file for the local path "dir/third.js.coffee"
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
Given /^I have a CoffeeScript template processor for the file "([^"]*)"$/ do |file|
|
||||||
|
@template_processor = Flowerbox::Delivery::Tilt::CoffeeTemplate.new(file)
|
||||||
|
end
|
@ -3,3 +3,4 @@ Given /^I have the file "([^"]*)" with the content:$/ do |filename, string|
|
|||||||
|
|
||||||
File.open(filename, 'wb') { |fh| fh.print(string) }
|
File.open(filename, 'wb') { |fh| fh.print(string) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
|
require 'erb'
|
||||||
|
|
||||||
Then /^the handler should have the following files in order:$/ do |table|
|
Then /^the handler should have the following files in order:$/ do |table|
|
||||||
table.raw.collect(&:first).each_with_index do |path, index|
|
table.raw.flatten.each_with_index do |path, index|
|
||||||
|
path = ERB.new(path).result(binding)
|
||||||
|
|
||||||
@sprockets.files[index].should == Pathname(File.expand_path(path))
|
@sprockets.files[index].should == Pathname(File.expand_path(path))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
Then /^there should be a temp file for the local path "([^"]*)"$/ do |path|
|
||||||
|
path.gsub!(%r{\.js.*$}, '.js')
|
||||||
|
|
||||||
|
@temp_file_path = Dir[".tmp/**/#{path}"].first
|
||||||
|
|
||||||
|
@temp_file_path.should_not be_nil
|
||||||
|
end
|
@ -0,0 +1,7 @@
|
|||||||
|
Then /^the temp file should contain:$/ do |string|
|
||||||
|
file_lines = File.read(@temp_file_path).lines.collect(&:strip)
|
||||||
|
|
||||||
|
string.lines.each do |line|
|
||||||
|
file_lines.should include(line.strip)
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,3 @@
|
|||||||
|
Then /^the evaluated template should be the path to the temp file for "([^"]*)"$/ do |file|
|
||||||
|
@temp_file_path.should == @result
|
||||||
|
end
|
@ -0,0 +1,3 @@
|
|||||||
|
When /^I evaluate the template$/ do
|
||||||
|
@result = @template_processor.render
|
||||||
|
end
|
@ -11,10 +11,14 @@ class FakeFS::File::Stat
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class FakeFS::File
|
||||||
|
def self.executable?(file)
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
Before do
|
Before do
|
||||||
mocha_setup
|
mocha_setup
|
||||||
|
|
||||||
FakeFS.activate!
|
|
||||||
end
|
end
|
||||||
|
|
||||||
After do
|
After do
|
||||||
@ -24,8 +28,6 @@ After do
|
|||||||
mocha_teardown
|
mocha_teardown
|
||||||
end
|
end
|
||||||
|
|
||||||
FakeFS.deactivate!
|
|
||||||
|
|
||||||
if @running_server
|
if @running_server
|
||||||
@running_server[:server].shutdown
|
@running_server[:server].shutdown
|
||||||
@running_server = nil
|
@running_server = nil
|
||||||
@ -35,3 +37,24 @@ After do
|
|||||||
|
|
||||||
@server.stop if @server
|
@server.stop if @server
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def temp_path_for(file)
|
||||||
|
File.join('.tmp/sprockets', File.expand_path(file.gsub(%r{\.js.*$}, '.js')))
|
||||||
|
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
|
||||||
|
@ -21,6 +21,7 @@ Gem::Specification.new do |gem|
|
|||||||
gem.add_development_dependency 'mocha'
|
gem.add_development_dependency 'mocha'
|
||||||
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_runtime_dependency 'rack'
|
gem.add_runtime_dependency 'rack'
|
||||||
gem.add_runtime_dependency 'sprockets'
|
gem.add_runtime_dependency 'sprockets'
|
||||||
|
@ -4,6 +4,8 @@ module Flowerbox
|
|||||||
autoload :TemplateRenderer, 'flowerbox/delivery/template_renderer'
|
autoload :TemplateRenderer, 'flowerbox/delivery/template_renderer'
|
||||||
autoload :SprocketsHandler, 'flowerbox/delivery/sprockets_handler'
|
autoload :SprocketsHandler, 'flowerbox/delivery/sprockets_handler'
|
||||||
autoload :UniqueAssetList, 'flowerbox/delivery/unique_asset_list'
|
autoload :UniqueAssetList, 'flowerbox/delivery/unique_asset_list'
|
||||||
|
|
||||||
|
autoload :Tilt, 'flowerbox/delivery/tilt'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ module Flowerbox::Delivery
|
|||||||
end
|
end
|
||||||
|
|
||||||
def add(asset)
|
def add(asset)
|
||||||
@files.add(paths_for(asset))
|
paths_for(asset).each { |path| @files.add(path_for_compiled_asset(path)) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def paths_for(asset)
|
def paths_for(asset)
|
||||||
@ -22,8 +22,19 @@ module Flowerbox::Delivery
|
|||||||
return @environment if @environment
|
return @environment if @environment
|
||||||
|
|
||||||
@environment = Sprockets::Environment.new
|
@environment = Sprockets::Environment.new
|
||||||
|
@environment.unregister_postprocessor('application/javascript', Sprockets::SafetyColons)
|
||||||
|
@environment.register_engine('.js', Flowerbox::Delivery::Tilt::JSTemplate)
|
||||||
|
|
||||||
options[:asset_paths].each { |path| @environment.append_path(path) }
|
options[:asset_paths].each { |path| @environment.append_path(path) }
|
||||||
@environment
|
@environment
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def asset_for(*args)
|
||||||
|
environment.find_asset(*args)
|
||||||
|
end
|
||||||
|
|
||||||
|
def path_for_compiled_asset(path)
|
||||||
|
Pathname(asset_for(path, :bundle => false).to_s)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
6
lib/flowerbox/delivery/tilt.rb
Normal file
6
lib/flowerbox/delivery/tilt.rb
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
require 'sprockets'
|
||||||
|
|
||||||
|
module Flowerbox::Delivery::Tilt
|
||||||
|
autoload :JSTemplate, 'flowerbox/delivery/tilt/js_template'
|
||||||
|
end
|
||||||
|
|
28
lib/flowerbox/delivery/tilt/js_template.rb
Normal file
28
lib/flowerbox/delivery/tilt/js_template.rb
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
require 'tilt'
|
||||||
|
|
||||||
|
class Flowerbox::Delivery::Tilt::JSTemplate < Tilt::Template
|
||||||
|
self.default_mime_type = "application/javascript"
|
||||||
|
|
||||||
|
def prepare ; end
|
||||||
|
|
||||||
|
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'))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -1,7 +1,7 @@
|
|||||||
module Flowerbox::Delivery
|
module Flowerbox::Delivery
|
||||||
class UniqueAssetList < ::Array
|
class UniqueAssetList < ::Array
|
||||||
def add(files)
|
def add(files)
|
||||||
files.each { |file| self << file if !include?(file) }
|
[ files ].flatten.each { |file| self << file if !include?(file) }
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -7,16 +7,20 @@ describe Flowerbox::Delivery::SprocketsHandler do
|
|||||||
|
|
||||||
describe '#add' do
|
describe '#add' do
|
||||||
let(:asset) { 'asset' }
|
let(:asset) { 'asset' }
|
||||||
let(:paths) { [ 'paths' ] }
|
let(:path) { 'path' }
|
||||||
|
let(:paths) { [ path ] }
|
||||||
|
|
||||||
|
let(:pathname_path) { 'pathname path' }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
sprockets_handler.expects(:paths_for).with(asset).returns(paths)
|
sprockets_handler.expects(:paths_for).with(asset).returns(paths)
|
||||||
|
sprockets_handler.expects(:path_for_compiled_asset).with(path).returns(pathname_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should add the asset to the list of ones to work with' do
|
it 'should add the asset to the list of ones to work with' do
|
||||||
sprockets_handler.add(asset)
|
sprockets_handler.add(asset)
|
||||||
|
|
||||||
sprockets_handler.files.should == paths
|
sprockets_handler.files.should == [ pathname_path ]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
62
spec/flowerbox/delivery/tilt/js_template_spec.rb
Normal file
62
spec/flowerbox/delivery/tilt/js_template_spec.rb
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Flowerbox::Delivery::Tilt::JSTemplate do
|
||||||
|
let(:js_template) { described_class.new { '' } }
|
||||||
|
|
||||||
|
describe '#evaluate' do
|
||||||
|
subject { js_template.evaluate(Object.new, {}) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
js_template.stubs(:file).returns(file)
|
||||||
|
end
|
||||||
|
|
||||||
|
context '.js' do
|
||||||
|
let(:file) { 'file.js' }
|
||||||
|
|
||||||
|
it { should == file }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'other extension' do
|
||||||
|
let(:file) { 'file.coffee' }
|
||||||
|
let(:temp_file) { 'temp file' }
|
||||||
|
|
||||||
|
before do
|
||||||
|
js_template.expects(:save).returns(temp_file)
|
||||||
|
end
|
||||||
|
|
||||||
|
it { should == temp_file }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#save' do
|
||||||
|
include FakeFS::SpecHelpers
|
||||||
|
|
||||||
|
let(:temp_file) { 'dir/temp file' }
|
||||||
|
let(:data) { 'data' }
|
||||||
|
|
||||||
|
before do
|
||||||
|
js_template.stubs(:temp_file).returns(temp_file)
|
||||||
|
js_template.stubs(:data).returns(data)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should save the file to disk and return the temp path' do
|
||||||
|
js_template.save.should == temp_file
|
||||||
|
|
||||||
|
File.read(temp_file).should == data
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#temp_file' do
|
||||||
|
subject { js_template.temp_file }
|
||||||
|
|
||||||
|
let(:filename) { "#{root_filename}.ext" }
|
||||||
|
let(:root_filename) { "dir/file.js" }
|
||||||
|
|
||||||
|
before do
|
||||||
|
js_template.stubs(:file).returns(filename)
|
||||||
|
end
|
||||||
|
|
||||||
|
it { should == File.join(Dir.pwd, '.tmp/sprockets', root_filename) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -9,7 +9,8 @@ describe Flowerbox::Delivery::UniqueAssetList do
|
|||||||
let(:third) { Pathname.new('two') }
|
let(:third) { Pathname.new('two') }
|
||||||
|
|
||||||
it 'should not add assets already added' do
|
it 'should not add assets already added' do
|
||||||
unique_asset_list.add([ first, second, third ])
|
unique_asset_list.add(first)
|
||||||
|
unique_asset_list.add([ second, third ])
|
||||||
|
|
||||||
unique_asset_list.should == [ first, third ]
|
unique_asset_list.should == [ first, third ]
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user