implement new http-based messaging between guard and instance

This commit is contained in:
Thomas Reynolds 2011-11-26 20:09:14 -08:00
parent e2fdd9a4d3
commit abbd489c91
6 changed files with 69 additions and 45 deletions

View file

@ -283,6 +283,17 @@ class Middleman::Base
@req = Rack::Request.new(env)
@res = Rack::Response.new
if env["PATH_INFO"] == "/__middleman__" && env["REQUEST_METHOD"] == "POST"
if req.params.has_key?("change")
file_did_change(req.params["change"])
elsif req.params.has_key?("delete")
file_did_delete(req.params["delete"])
end
res.status = 200
return res.finish
end
# Catch :halt exceptions and use that response if given
catch(:halt) do
process_request
@ -373,13 +384,13 @@ class Middleman::Base
# @param [String] Request path
# @return [String] Path with index file if necessary
def full_path(path)
# cache.fetch(:full_path, path) do
cache.fetch(:full_path, path) do
parts = path ? path.split('/') : []
if parts.last.nil? || parts.last.split('.').length == 1
path = File.join(path, index_file)
end
"/" + path.sub(%r{^/}, '')
# end
end
end
# Add a new mime-type for a specific extension

View file

@ -31,6 +31,10 @@ module Middleman
@cache[key]
end
def keys
@cache.keys
end
# Clear the entire cache
def clear
@cache = {}

View file

@ -4,11 +4,6 @@ module Middleman::CoreExtensions::Rendering
# Autoload
require "coffee_script"
begin
require "slim"
rescue LoadError
end
app.send :include, InstanceMethods
# Activate custom renderers
@ -17,6 +12,11 @@ module Middleman::CoreExtensions::Rendering
app.register Middleman::Renderers::Markdown
app.register Middleman::Renderers::ERb
app.register Middleman::Renderers::Liquid
begin
require "slim"
rescue LoadError
end
end
alias :included :registered
end
@ -28,7 +28,7 @@ module Middleman::CoreExtensions::Rendering
def initialize
file_changed %r{^source/} do |file|
path = File.expand_path(file, root)
# cache.remove(:raw_template, path.to_sym)
cache.remove(:raw_template, path)
end
super
@ -97,25 +97,25 @@ module Middleman::CoreExtensions::Rendering
# @private
def render_individual_file(path, locs = {}, opts = {}, &block)
path = path.to_s
body = #cache.fetch(:raw_template, path) do
# $stderr.puts "reading: #{path}"
body = cache.fetch(:raw_template, path) do
File.read(path)
# end
end
extension = File.extname(path)
options = opts.merge(options_for_ext(extension))
options[:outvar] ||= '@_out_buf'
template = #cache.fetch(:compiled_template, options, body) do
template = cache.fetch(:compiled_template, options, body) do
::Tilt.new(path, 1, options) { body }
# end
end
template.render(self, locs, &block)
end
# @private
def options_for_ext(ext)
# cache.fetch(:options_for_ext, ext) do
cache.fetch(:options_for_ext, ext) do
options = {}
extension_class = ::Tilt[ext]
@ -126,7 +126,7 @@ module Middleman::CoreExtensions::Rendering
end
options
# end
end
end
# @private
@ -194,7 +194,7 @@ module Middleman::CoreExtensions::Rendering
# @private
def resolve_template(request_path, options={})
request_path = request_path.to_s
# cache.fetch(:resolve_template, request_path, options) do
cache.fetch(:resolve_template, request_path, options) do
relative_path = request_path.sub(%r{^/}, "")
on_disk_path = File.expand_path(relative_path, self.source_dir)
@ -228,7 +228,7 @@ module Middleman::CoreExtensions::Rendering
else
false
end
# end
end
end
end
end

View file

@ -1,6 +1,7 @@
require "guard"
require "guard/guard"
require "rbconfig"
require "net/http"
if Config::CONFIG['host_os'].downcase =~ %r{mingw}
require "win32/process"
@ -60,20 +61,16 @@ module Guard
if needs_to_restart
reload
elsif !@app.nil?
else
paths.each do |path|
@app.logger.debug :file_change, Time.now, path if @app.settings.logging?
@app.file_did_change(path)
file_did_change(path)
end
end
end
def run_on_deletion(paths)
if !@app.nil?
paths.each do |path|
@app.logger.debug :file_remove, Time.now, path if @app.settings.logging?
@app.file_did_delete(path)
end
file_did_delete(path)
end
end
@ -82,16 +79,16 @@ module Guard
# Quiet down Guard
# ENV['GUARD_ENV'] = 'test' if @options[:debug] == "true"
@server_job = fork do
env = (@options[:environment] || "development").to_sym
is_logging = @options.has_key?(:debug) && (@options[:debug] == "true")
@app = ::Middleman.server.inst do
app = ::Middleman.server.inst do
set :environment, env
set :logging, is_logging
end
app_rack = @app.class.to_rack_app
app_rack = app.class.to_rack_app
@server_job = fork do
opts = @options.dup
opts[:app] = app_rack
puts "== The Middleman is standing watch on port #{opts[:port]||4567}"
@ -104,7 +101,20 @@ module Guard
Process.kill("KILL", @server_job)
Process.wait @server_job
@server_job = nil
@app = nil
# @app = nil
end
def talk_to_server(params={})
uri = URI.parse("http://#{@options[:host]}:#{@options[:port]}/__middleman__")
Net::HTTP.post_form(uri, {}.merge(params))
end
def file_did_change(path)
talk_to_server :change => path
end
def file_did_delete(path)
talk_to_server :delete => path
end
end
end

View file

@ -4,7 +4,6 @@ module Middleman::Sitemap
def initialize(app)
@app = app
@cache = ::Middleman::Cache.new
@source = File.expand_path(@app.source, @app.root)
@pages = {}
end
@ -23,12 +22,12 @@ module Middleman::Sitemap
def ignore(path)
page(path) { ignore }
@cache.remove(:ignored_paths)
app.cache.remove(:ignored_paths)
end
def proxy(path, target)
page(path) { proxy_to(target.sub(%r{^/}, "")) }
@cache.remove(:proxied_paths)
app.cache.remove(:proxied_paths)
end
def page(path, &block)
@ -53,7 +52,7 @@ module Middleman::Sitemap
end
def ignored_paths
@cache.fetch :ignored_paths do
app.cache.fetch :ignored_paths do
@pages.values.select(&:ignored?).map(&:path)
end
end
@ -63,7 +62,7 @@ module Middleman::Sitemap
end
def generic_paths
@cache.fetch :generic_paths do
app.cache.fetch :generic_paths do
@pages.values.select(&:generic?).map(&:path)
end
end
@ -73,7 +72,7 @@ module Middleman::Sitemap
end
def proxied_paths
@cache.fetch :proxied_paths do
app.cache.fetch :proxied_paths do
@pages.values.select(&:proxy?).map(&:path)
end
end
@ -123,7 +122,7 @@ module Middleman::Sitemap
protected
def extensionless_path(file)
@cache.fetch(:extensionless_path, file) do
app.cache.fetch(:extensionless_path, file) do
path = file.dup
end_of_the_line = false

View file

@ -31,7 +31,7 @@ module Middleman::Sitemap
end
def metadata
# store.cache.fetch(:metadata, source_file) do
app.cache.fetch(:metadata, source_file) do
metadata = { :options => {}, :locals => {} }
app.provides_metadata.each do |callback, matcher|
next if !matcher.nil? && !source_file.match(matcher)
@ -39,7 +39,7 @@ module Middleman::Sitemap
metadata = metadata.deep_merge(result)
end
metadata
# end
end
end
def render(opts={}, locs={}, &block)