2011-11-17 20:44:48 +00:00
|
|
|
require 'erb'
|
2013-04-23 20:28:23 +00:00
|
|
|
require 'rack/livereload/processing_skip_analyzer'
|
|
|
|
require 'rack/livereload/body_processor'
|
2011-11-17 20:44:48 +00:00
|
|
|
|
2011-11-04 15:51:22 +00:00
|
|
|
module Rack
|
|
|
|
class LiveReload
|
|
|
|
attr_reader :app
|
|
|
|
|
2011-11-07 14:06:40 +00:00
|
|
|
def initialize(app, options = {})
|
2012-01-03 13:58:51 +00:00
|
|
|
@app, @options = app, options
|
2011-11-08 14:48:34 +00:00
|
|
|
end
|
|
|
|
|
2011-11-04 15:51:22 +00:00
|
|
|
def call(env)
|
2012-03-04 18:25:45 +00:00
|
|
|
dup._call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
def _call(env)
|
2011-11-17 20:44:48 +00:00
|
|
|
_, path, file = (env['PATH_INFO'] || '').split('/')
|
|
|
|
|
|
|
|
if path == '__rack' && ::File.file?(target = ::File.expand_path("../../../js/#{file}", __FILE__))
|
|
|
|
deliver_file(target)
|
2011-11-04 15:51:22 +00:00
|
|
|
else
|
2013-01-24 15:22:54 +00:00
|
|
|
status, headers, body = result = @app.call(env)
|
|
|
|
|
2013-04-23 20:28:23 +00:00
|
|
|
return result if ProcessingSkipAnalyzer.skip_processing?(result, env, @options)
|
2012-03-02 14:25:48 +00:00
|
|
|
|
2013-04-23 20:28:23 +00:00
|
|
|
processor = BodyProcessor.new(body, @options)
|
|
|
|
processor.process!(env)
|
2012-09-18 21:22:19 +00:00
|
|
|
|
2013-04-23 20:28:23 +00:00
|
|
|
headers['Content-Length'] = processor.content_length.to_s
|
2011-12-01 12:25:02 +00:00
|
|
|
|
2013-04-23 20:28:23 +00:00
|
|
|
if processor.livereload_added
|
|
|
|
headers['X-Rack-LiveReload'] = '1'
|
2011-11-04 15:51:22 +00:00
|
|
|
end
|
|
|
|
|
2013-04-23 20:28:23 +00:00
|
|
|
[ status, headers, processor.new_body ]
|
2011-11-04 15:51:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def deliver_file(file)
|
2011-11-17 20:44:48 +00:00
|
|
|
type = case ::File.extname(file)
|
|
|
|
when '.js'
|
|
|
|
'text/javascript'
|
|
|
|
when '.swf'
|
|
|
|
'application/swf'
|
|
|
|
end
|
|
|
|
|
|
|
|
[ 200, { 'Content-Type' => type, 'Content-Length' => ::File.size(file).to_s }, [ ::File.read(file) ] ]
|
|
|
|
end
|
2011-11-04 15:51:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|