more guard docs
This commit is contained in:
parent
6f7c2f881d
commit
bf025b85e9
2 changed files with 59 additions and 63 deletions
|
@ -2,4 +2,6 @@ lib/**/*.rb
|
||||||
--exclude lib/middleman/vendor
|
--exclude lib/middleman/vendor
|
||||||
--exclude lib/middleman/extensions/automatic_image_sizes/fastimage.rb
|
--exclude lib/middleman/extensions/automatic_image_sizes/fastimage.rb
|
||||||
--exclude lib/middleman/extensions/minify_css/cssmin.rb
|
--exclude lib/middleman/extensions/minify_css/cssmin.rb
|
||||||
|
--exclude lib/middleman/step_definitions
|
||||||
|
--exclude lib/middleman/step_definitions.rb
|
||||||
--no-private
|
--no-private
|
|
@ -1,57 +1,49 @@
|
||||||
|
# Guard watches the filesystem for changes
|
||||||
require "guard"
|
require "guard"
|
||||||
require "guard/guard"
|
require "guard/guard"
|
||||||
require "rbconfig"
|
|
||||||
|
# File changes are forwarded to the currently running app via HTTP
|
||||||
require "net/http"
|
require "net/http"
|
||||||
require "thin"
|
|
||||||
|
|
||||||
if RbConfig::CONFIG['host_os'].downcase =~ %r{mingw}
|
# Support forking on Windows
|
||||||
require "win32/process"
|
require "rbconfig"
|
||||||
end
|
require "win32/process" if RbConfig::CONFIG['host_os'].downcase =~ %r{mingw}
|
||||||
|
|
||||||
module Middleman
|
module Middleman::Guard
|
||||||
module Guard
|
def self.start(options={})
|
||||||
class << self
|
# Forward CLI options to Guard
|
||||||
def add_guard(&block)
|
options_hash = options.map { |k,v| ", :#{k} => '#{v}'" }.join
|
||||||
# Deprecation Warning
|
|
||||||
puts "== Middleman::Guard.add_guard has been removed. Update your extensions to versions which support this change."
|
|
||||||
end
|
|
||||||
|
|
||||||
def start(options={})
|
# Watch all files in project, even hidden ones.
|
||||||
options_hash = ""
|
::Guard.start({
|
||||||
options.each do |k,v|
|
:guardfile_contents => %Q{
|
||||||
options_hash << ", :#{k} => '#{v}'"
|
|
||||||
end
|
|
||||||
|
|
||||||
guardfile_contents = %Q{
|
|
||||||
guard 'middleman'#{options_hash} do
|
guard 'middleman'#{options_hash} do
|
||||||
watch(%r{(.*)})
|
watch(%r{(.*)})
|
||||||
end
|
end
|
||||||
}
|
},
|
||||||
|
|
||||||
::Guard.start({
|
|
||||||
:guardfile_contents => guardfile_contents,
|
|
||||||
:watch_all_modifications => true
|
:watch_all_modifications => true
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Shut up Guard
|
|
||||||
module Guard::UI
|
|
||||||
class << self
|
|
||||||
def info(message, options = { }); end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# @private
|
# @private
|
||||||
module Guard
|
module Guard
|
||||||
|
# Monkeypatch Guard into being quiet
|
||||||
|
module UI
|
||||||
|
class << self
|
||||||
|
def info(message, options = { }); end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Guards must be in the Guard module to be picked up
|
||||||
class Middleman < Guard
|
class Middleman < Guard
|
||||||
|
# Save the options for later
|
||||||
def initialize(watchers = [], options = {})
|
def initialize(watchers = [], options = {})
|
||||||
super
|
super
|
||||||
@options = options
|
@options = options
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Start Middleman in a fork
|
||||||
def start
|
def start
|
||||||
@server_job = fork do
|
@server_job = fork do
|
||||||
env = (@options[:environment] || "development").to_sym
|
env = (@options[:environment] || "development").to_sym
|
||||||
|
@ -61,6 +53,7 @@ module Guard
|
||||||
set :logging, is_logging
|
set :logging, is_logging
|
||||||
end
|
end
|
||||||
|
|
||||||
|
require "thin"
|
||||||
::Thin::Logging.silent = !is_logging
|
::Thin::Logging.silent = !is_logging
|
||||||
|
|
||||||
app_rack = app.class.to_rack_app
|
app_rack = app.class.to_rack_app
|
||||||
|
@ -72,6 +65,7 @@ module Guard
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Stop the forked Middleman
|
||||||
def stop
|
def stop
|
||||||
puts "== The Middleman is shutting down"
|
puts "== The Middleman is shutting down"
|
||||||
Process.kill("KILL", @server_job)
|
Process.kill("KILL", @server_job)
|
||||||
|
@ -79,52 +73,52 @@ module Guard
|
||||||
@server_job = nil
|
@server_job = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Simply stop, then start
|
||||||
def reload
|
def reload
|
||||||
stop
|
stop
|
||||||
start
|
start
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# What to do on file change
|
||||||
|
# @param [Array<String>] paths Array of paths that changed
|
||||||
def run_on_change(paths)
|
def run_on_change(paths)
|
||||||
needs_to_restart = false
|
# See if the changed file is config.rb or lib/*.rb
|
||||||
|
return reload if needs_to_reload?(paths)
|
||||||
|
|
||||||
paths.each do |path|
|
# Otherwise forward to Middleman
|
||||||
if path.match(%{^config\.rb}) || path.match(%r{^lib/^[^\.](.*)\.rb$})
|
paths.each { |path| tell_server(:change => path) }
|
||||||
needs_to_restart = true
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if needs_to_restart
|
|
||||||
reload
|
|
||||||
else
|
|
||||||
paths.each do |path|
|
|
||||||
file_did_change(path)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# What to do on file deletion
|
||||||
|
# @param [Array<String>] paths Array of paths that were removed
|
||||||
def run_on_deletion(paths)
|
def run_on_deletion(paths)
|
||||||
paths.each do |path|
|
# See if the changed file is config.rb or lib/*.rb
|
||||||
file_did_delete(path)
|
return reload if needs_to_reload?(paths)
|
||||||
end
|
|
||||||
|
# Otherwise forward to Middleman
|
||||||
|
paths.each { |path| tell_server(:delete => path) }
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def talk_to_server(params={})
|
# Whether the passed files are config.rb or lib/*.rb
|
||||||
|
# @param [Array<String>] paths Array of paths to check
|
||||||
|
# @return [Boolean] Whether the server needs to reload
|
||||||
|
def needs_to_reload?(paths)
|
||||||
|
paths.any? do |path|
|
||||||
|
path.match(%{^config\.rb}) || path.match(%r{^lib/^[^\.](.*)\.rb$})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Send a message to the running server
|
||||||
|
# @param [Hash] params Keys to be hashed and sent to server
|
||||||
|
def tell_server(params={})
|
||||||
uri = URI.parse("http://#{@options[:host]}:#{@options[:port]}/__middleman__")
|
uri = URI.parse("http://#{@options[:host]}:#{@options[:port]}/__middleman__")
|
||||||
Net::HTTP.post_form(uri, {}.merge(params))
|
Net::HTTP.post_form(uri, {}.merge(params))
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Trap the interupt signal and shut down Guard (and thus the server) smoothly
|
||||||
trap(:INT) do
|
trap(:INT) do
|
||||||
::Guard.stop
|
::Guard.stop
|
||||||
exit
|
exit
|
||||||
|
|
Loading…
Reference in a new issue