From 95fb0e9702fe7e5d4cab6d10c7f665015f4cad30 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 14 Mar 2012 13:07:28 -0400 Subject: [PATCH] more hacking --- flowerbox-delivery.gemspec | 1 + lib/flowerbox/delivery/server.rb | 27 ++++++++++++++----- lib/flowerbox/delivery/sprockets_handler.rb | 14 ++++++---- lib/flowerbox/delivery/tilt/css_template.rb | 1 + lib/flowerbox/delivery/tilt/js_template.rb | 4 +++ lib/flowerbox/delivery/tilt/jst_template.rb | 8 +++++- .../delivery/tilt/template_that_saves.rb | 14 ++++++---- lib/flowerbox/delivery/unique_asset_list.rb | 2 +- 8 files changed, 53 insertions(+), 18 deletions(-) diff --git a/flowerbox-delivery.gemspec b/flowerbox-delivery.gemspec index ee89fa6..314a363 100644 --- a/flowerbox-delivery.gemspec +++ b/flowerbox-delivery.gemspec @@ -26,6 +26,7 @@ Gem::Specification.new do |gem| gem.add_development_dependency 'ejs' gem.add_runtime_dependency 'rack' + gem.add_runtime_dependency 'thin' gem.add_runtime_dependency 'sprockets' gem.add_runtime_dependency 'coffee-script' gem.add_runtime_dependency 'sprockets-vendor_gems' diff --git a/lib/flowerbox/delivery/server.rb b/lib/flowerbox/delivery/server.rb index 8da608a..7e921a1 100644 --- a/lib/flowerbox/delivery/server.rb +++ b/lib/flowerbox/delivery/server.rb @@ -1,6 +1,7 @@ require 'rack' require 'net/http' require 'socket' +require 'rack/builder' module Flowerbox module Delivery @@ -14,12 +15,18 @@ module Flowerbox def start @server_thread = Thread.new do server_options = { :Port => port, :Host => interface } - if !options[:logging] - server_options[:AccessLog] = [ nil, nil ] - server_options[:Logger] = Logger.new('/dev/null') + + app = options[:app] + + if options[:logging] + real_app = app + app = ::Rack::Builder.new do + use ::Rack::CommonLogger, STDOUT + run real_app + end end - ::Rack::Handler::WEBrick.run(options[:app], server_options) do |server| + ::Rack::Handler::Thin.run(app, server_options) do |server| trap('QUIT') { server.stop } Thread.current[:server] = server @@ -35,7 +42,7 @@ module Flowerbox def stop if @server_thread - @server_thread[:server].shutdown + @server_thread[:server].stop wait_for_server_to_stop end @@ -68,6 +75,14 @@ module Flowerbox @port end + def address + "http://#{interface}:#{port}/" + end + + def alive? + @server_thread.alive? + end + private def wait_for_server_to_start while true do @@ -84,7 +99,7 @@ module Flowerbox end def wait_for_server_to_stop - while true do + while alive? do begin connect_interface = '127.0.0.1' if interface == '0.0.0.0' diff --git a/lib/flowerbox/delivery/sprockets_handler.rb b/lib/flowerbox/delivery/sprockets_handler.rb index 495a23d..5dcd53f 100644 --- a/lib/flowerbox/delivery/sprockets_handler.rb +++ b/lib/flowerbox/delivery/sprockets_handler.rb @@ -9,7 +9,7 @@ module Flowerbox::Delivery attr_reader :files, :options - def_delegators :environment, :append_path, :register_engine + def_delegators :environment, :append_path, :register_engine, :[] def initialize(options) @options = options @@ -25,16 +25,20 @@ module Flowerbox::Delivery environment.find_asset(asset).to_a.collect(&:pathname) end + def expire_index! + @environment.send(:expire_index!) + end + def environment return @environment if @environment @environment = Sprockets::EnvironmentWithVendoredGems.new @environment.unregister_postprocessor('application/javascript', Sprockets::SafetyColons) - @environment.register_postprocessor('application/javascript', Flowerbox::Delivery::Tilt::EnsureSavedFile) + #@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) - @environment.register_engine('.jst', Flowerbox::Delivery::Tilt::JSTTemplate) + #@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| append_path(path) } diff --git a/lib/flowerbox/delivery/tilt/css_template.rb b/lib/flowerbox/delivery/tilt/css_template.rb index dbea79a..85e1b63 100644 --- a/lib/flowerbox/delivery/tilt/css_template.rb +++ b/lib/flowerbox/delivery/tilt/css_template.rb @@ -6,5 +6,6 @@ class Flowerbox::Delivery::Tilt::CSSTemplate < Tilt::Template EXTENSION = "css" include Flowerbox::Delivery::Tilt::TemplateThatSaves + end diff --git a/lib/flowerbox/delivery/tilt/js_template.rb b/lib/flowerbox/delivery/tilt/js_template.rb index 24b7666..6d26129 100644 --- a/lib/flowerbox/delivery/tilt/js_template.rb +++ b/lib/flowerbox/delivery/tilt/js_template.rb @@ -6,5 +6,9 @@ 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/jst_template.rb b/lib/flowerbox/delivery/tilt/jst_template.rb index 850f087..25d96da 100644 --- a/lib/flowerbox/delivery/tilt/jst_template.rb +++ b/lib/flowerbox/delivery/tilt/jst_template.rb @@ -6,9 +6,15 @@ class Flowerbox::Delivery::Tilt::JSTTemplate < Sprockets::JstProcessor include Flowerbox::Delivery::Tilt::TemplateThatSaves def evaluate(scope, locals, &block) - super + @data = super + + p @data handle_evaluate end + + def data_to_save + @data || data + end end diff --git a/lib/flowerbox/delivery/tilt/template_that_saves.rb b/lib/flowerbox/delivery/tilt/template_that_saves.rb index 331326c..4efc845 100644 --- a/lib/flowerbox/delivery/tilt/template_that_saves.rb +++ b/lib/flowerbox/delivery/tilt/template_that_saves.rb @@ -12,21 +12,25 @@ module Flowerbox::Delivery::Tilt::TemplateThatSaves def save FileUtils.mkdir_p File.dirname(temp_file) - File.open(temp_file, 'wb') { |fh| fh.print data } + File.open(temp_file, 'wb') { |fh| fh.print data_to_save } temp_file end + def data_to_save + data + end + def temp_file 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 + + def evaluate(scope, locals, &block) + handle_evaluate + end end diff --git a/lib/flowerbox/delivery/unique_asset_list.rb b/lib/flowerbox/delivery/unique_asset_list.rb index 549c6b3..b16049f 100644 --- a/lib/flowerbox/delivery/unique_asset_list.rb +++ b/lib/flowerbox/delivery/unique_asset_list.rb @@ -5,7 +5,7 @@ module Flowerbox::Delivery end def to_json - collect(&:body) + collect(&:logical_path) end private