From b1e99d46c648b92954463a4a0026d26fe57053a8 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 8 Mar 2012 10:14:45 -0500 Subject: [PATCH] more hacking --- lib/flowerbox/delivery/sprockets_handler.rb | 7 +++--- lib/flowerbox/delivery/tilt.rb | 2 ++ lib/flowerbox/delivery/tilt/css_template.rb | 4 ---- .../delivery/tilt/ensure_saved_file.rb | 22 +++++++++++++++++++ lib/flowerbox/delivery/tilt/js_template.rb | 4 ---- .../delivery/tilt/template_that_saves.rb | 10 ++++++++- lib/flowerbox/delivery/unique_asset_list.rb | 4 ++++ 7 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 lib/flowerbox/delivery/tilt/ensure_saved_file.rb diff --git a/lib/flowerbox/delivery/sprockets_handler.rb b/lib/flowerbox/delivery/sprockets_handler.rb index 9701134..4dab5be 100644 --- a/lib/flowerbox/delivery/sprockets_handler.rb +++ b/lib/flowerbox/delivery/sprockets_handler.rb @@ -17,7 +17,7 @@ module Flowerbox::Delivery end def add(asset) - paths_for(asset).each { |path| @files.add(path_for_compiled_asset(path)) } + paths_for(asset).each { |path| add_paths_for_compiled_asset(path) } end def paths_for(asset) @@ -29,6 +29,7 @@ module Flowerbox::Delivery @environment = Sprockets::Environment.new @environment.unregister_postprocessor('application/javascript', Sprockets::SafetyColons) + @environment.register_postprocessor('application/javascript', Flowerbox::Delivery::Tilt::EnsureSavedFile) @environment.unregister_bundle_processor('text/css', Sprockets::CharsetNormalizer) @environment.register_engine('.js', Flowerbox::Delivery::Tilt::JSTemplate) @environment.register_engine('.css', Flowerbox::Delivery::Tilt::CSSTemplate) @@ -43,8 +44,8 @@ module Flowerbox::Delivery environment.find_asset(*args) end - def path_for_compiled_asset(path) - Pathname(asset_for(path, :bundle => false).to_s) + def add_paths_for_compiled_asset(path) + asset_for(path, :bundle => false).to_a.each { |file_path| @files.add(file_path) } end end end diff --git a/lib/flowerbox/delivery/tilt.rb b/lib/flowerbox/delivery/tilt.rb index 97d3fed..bc74ef6 100644 --- a/lib/flowerbox/delivery/tilt.rb +++ b/lib/flowerbox/delivery/tilt.rb @@ -6,5 +6,7 @@ module Flowerbox::Delivery::Tilt autoload :CSSTemplate, 'flowerbox/delivery/tilt/css_template' autoload :TemplateThatSaves, 'flowerbox/delivery/tilt/template_that_saves' + + autoload :EnsureSavedFile, 'flowerbox/delivery/tilt/ensure_saved_file' end diff --git a/lib/flowerbox/delivery/tilt/css_template.rb b/lib/flowerbox/delivery/tilt/css_template.rb index 7036447..dbea79a 100644 --- a/lib/flowerbox/delivery/tilt/css_template.rb +++ b/lib/flowerbox/delivery/tilt/css_template.rb @@ -6,9 +6,5 @@ class Flowerbox::Delivery::Tilt::CSSTemplate < Tilt::Template EXTENSION = "css" include Flowerbox::Delivery::Tilt::TemplateThatSaves - - def evaluate(scope, locals, &block) - handle_evaluate - end end diff --git a/lib/flowerbox/delivery/tilt/ensure_saved_file.rb b/lib/flowerbox/delivery/tilt/ensure_saved_file.rb new file mode 100644 index 0000000..b4de1b8 --- /dev/null +++ b/lib/flowerbox/delivery/tilt/ensure_saved_file.rb @@ -0,0 +1,22 @@ +require 'tilt' + +module Flowerbox::Delivery::Tilt + class EnsureSavedFile < Tilt::Template + include TemplateThatSaves + + def handle_evaluate + output = if File.file?(data) + data + else + save + end + + output + end + + def extension + "js" + end + end +end + diff --git a/lib/flowerbox/delivery/tilt/js_template.rb b/lib/flowerbox/delivery/tilt/js_template.rb index 6d26129..24b7666 100644 --- a/lib/flowerbox/delivery/tilt/js_template.rb +++ b/lib/flowerbox/delivery/tilt/js_template.rb @@ -6,9 +6,5 @@ class Flowerbox::Delivery::Tilt::JSTemplate < Tilt::Template EXTENSION = "js" include Flowerbox::Delivery::Tilt::TemplateThatSaves - - def evaluate(scope, locals, &block) - handle_evaluate - end end diff --git a/lib/flowerbox/delivery/tilt/template_that_saves.rb b/lib/flowerbox/delivery/tilt/template_that_saves.rb index 2df84be..331326c 100644 --- a/lib/flowerbox/delivery/tilt/template_that_saves.rb +++ b/lib/flowerbox/delivery/tilt/template_that_saves.rb @@ -18,7 +18,15 @@ module Flowerbox::Delivery::Tilt::TemplateThatSaves end def temp_file - File.join(Dir.pwd, ".tmp/sprockets", file.gsub(%r{(\.#{self.class::EXTENSION})(.*)$}, '\1')) + File.join(Dir.pwd, ".tmp/sprockets", file.gsub(%r{(\.#{extension})(.*)$}, '\1')) + end + + def evaluate(scope, locals, &block) + handle_evaluate + end + + def extension + self.class::EXTENSION end end diff --git a/lib/flowerbox/delivery/unique_asset_list.rb b/lib/flowerbox/delivery/unique_asset_list.rb index c22f1b4..549c6b3 100644 --- a/lib/flowerbox/delivery/unique_asset_list.rb +++ b/lib/flowerbox/delivery/unique_asset_list.rb @@ -4,6 +4,10 @@ module Flowerbox::Delivery [ files ].flatten.each { |file| self << file if !include?(file) } end + def to_json + collect(&:body) + end + private def include?(file) any? { |other_file| other_file == file }