middleman/lib/middleman/guard.rb

73 lines
1.5 KiB
Ruby
Raw Normal View History

2011-07-14 03:29:59 +02:00
require "guard"
require "guard/guard"
2011-07-14 03:45:15 +02:00
require "guard/livereload"
2011-08-09 23:46:50 +02:00
require "rbconfig"
2011-07-14 03:29:59 +02:00
2011-08-09 23:46:50 +02:00
if Config::CONFIG['host_os'].downcase =~ %r{mswin|mingw}
require "win32/process"
require 'win32console'
2011-08-09 23:46:50 +02:00
end
2011-07-14 03:29:59 +02:00
module Middleman::Guard
2011-07-14 03:45:15 +02:00
def self.start(options={}, livereload={})
2011-07-14 03:29:59 +02:00
options_hash = ""
options.each do |k,v|
options_hash << ", :#{k} => '#{v}'"
end
2011-07-14 03:58:22 +02:00
guardfile_contents = %Q{
guard 'middleman'#{options_hash} do
watch("config.rb")
2011-07-21 22:50:33 +02:00
watch(%r{^lib/^[^\.](.*)\.rb$})
2011-07-14 03:58:22 +02:00
end
}
if livereload
livereload_options_hash = ""
livereload.each do |k,v|
livereload_options_hash << ", :#{k} => '#{v}'"
end
guardfile_contents << %Q{
2011-07-14 03:45:15 +02:00
guard 'livereload'#{livereload_options_hash} do
2011-08-09 23:38:08 +02:00
watch(%r{^source/([^\.].*)$})
2011-07-14 03:45:15 +02:00
end
2011-07-14 03:29:59 +02:00
}
2011-07-14 03:58:22 +02:00
end
::Guard.start({ :guardfile_contents => guardfile_contents })
2011-07-14 03:29:59 +02:00
end
end
module Guard
2011-07-14 03:45:15 +02:00
class Middleman < Guard
2011-07-14 03:29:59 +02:00
def initialize(watchers = [], options = {})
super
2011-08-09 23:37:55 +02:00
@options = options
2011-07-14 03:29:59 +02:00
end
def start
server_start
end
def run_on_change(paths)
server_stop
server_start
end
private
def server_start
@server_job = fork do
2011-08-09 23:37:55 +02:00
::Middleman.start_server(@options)
2011-07-14 03:29:59 +02:00
end
end
def server_stop
puts "== The Middleman is shutting down"
Process.kill("KILL", @server_job)
Process.wait @server_job
@server_job = nil
2011-08-09 23:55:48 +02:00
# @server_options[:app] = nil
2011-07-14 03:29:59 +02:00
end
end
end