lib/middleman/guard.rb in middleman-3.0.0.alpha.2 vs lib/middleman/guard.rb in middleman-3.0.0.alpha.3
- old
+ new
@@ -1,37 +1,41 @@
require "guard"
require "guard/guard"
require "rbconfig"
+require "net/http"
-if Config::CONFIG['host_os'].downcase =~ %r{mingw}
+if RbConfig::CONFIG['host_os'].downcase =~ %r{mingw}
require "win32/process"
end
module Middleman
module Guard
- def self.add_guard(&block)
- # Deprecation Warning
- puts "== Middleman::Guard.add_guard has been removed. Update your extensions to versions which support this change."
- end
-
- def self.start(options={})
- options_hash = ""
- options.each do |k,v|
- options_hash << ", :#{k} => '#{v}'"
+ class << self
+ def add_guard(&block)
+ # Deprecation Warning
+ puts "== Middleman::Guard.add_guard has been removed. Update your extensions to versions which support this change."
end
-
- guardfile_contents = %Q{
- guard 'middleman'#{options_hash} do
- watch(%r{(.*)})
+
+ def start(options={})
+ options_hash = ""
+ options.each do |k,v|
+ options_hash << ", :#{k} => '#{v}'"
end
- }
+
+ guardfile_contents = %Q{
+ guard 'middleman'#{options_hash} do
+ watch(%r{(.*)})
+ end
+ }
- ::Guard.start({ :guardfile_contents => guardfile_contents })
+ ::Guard.start({ :guardfile_contents => guardfile_contents })
+ end
end
end
end
+# @private
module Guard
class Middleman < Guard
def initialize(watchers = [], options = {})
super
@options = options
@@ -56,53 +60,62 @@
end
end
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
+ paths.each do |path|
+ file_did_delete(path)
end
end
private
def server_start
# Quiet down Guard
# ENV['GUARD_ENV'] = 'test' if @options[:debug] == "true"
- env = (@options[:environment] || "development").to_sym
- is_logging = @options.has_key?(:debug) && (@options[:debug] == "true")
- @app = ::Middleman.server.inst do
- set :environment, env
- set :logging, is_logging
- end
-
- app_rack = @app.class.to_rack_app
-
@server_job = fork do
+ env = (@options[:environment] || "development").to_sym
+ is_logging = @options.has_key?(:debug) && (@options[:debug] == "true")
+ app = ::Middleman.server.inst do
+ set :environment, env
+ set :logging, is_logging
+ end
+
+ app_rack = app.class.to_rack_app
+
opts = @options.dup
opts[:app] = app_rack
- puts "== The Middleman is standing watch on port #{opts[:Port]||4567}"
+ puts "== The Middleman is standing watch on port #{opts[:port]||4567}"
::Middleman.start_server(opts)
end
end
def server_stop
puts "== The Middleman is shutting down"
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
\ No newline at end of file