class Guard::Middleman

Guards must be in the Guard module to be picked up

def bootup

def bootup
  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
  opts[:logging] = is_logging
  puts "== The Middleman is standing watch on port #{opts[:port]||4567}"
  ::Middleman.start_server(opts)
end

def initialize(watchers = [], options = {})

Save the options for later
def initialize(watchers = [], options = {})
  super
  @options = options
  # Trap the interupt signal and shut down Guard (and thus the server) smoothly
  trap(kill_command) do 
    ::Guard.stop
    exit!(0)
  end
end

def kill_command

def kill_command
  ::Middleman::WINDOWS ? 1 : :INT
end

def needs_to_reload?(paths)

Returns:
  • (Boolean) - Whether the server needs to reload

Parameters:
  • paths (Array) -- Array of paths to check
def needs_to_reload?(paths)
  paths.any? do |path|
    path.match(%{^config\.rb}) || path.match(%r{^lib/^[^\.](.*)\.rb$})
  end
end

def reload

Simply stop, then start
def reload
  stop
  start
end

def run_on_change(paths)

Parameters:
  • paths (Array) -- Array of paths that changed
def run_on_change(paths)
  # See if the changed file is config.rb or lib/*.rb
  return reload if needs_to_reload?(paths)
  
  # Otherwise forward to Middleman
  paths.each { |path| tell_server(:change => path) }
end

def run_on_deletion(paths)

Parameters:
  • paths (Array) -- Array of paths that were removed
def run_on_deletion(paths)
  # See if the changed file is config.rb or lib/*.rb
  return reload if needs_to_reload?(paths)
  
  # Otherwise forward to Middleman
  paths.each { |path| tell_server(:delete => path) }
end

def start

Start Middleman in a fork
def start
  if ::Middleman::JRUBY
    thread = Thread.new { bootup }
    thread.join
  else
    @server_job = fork { bootup }
  end
end

def stop

Stop the forked Middleman
def stop
  puts "== The Middleman is shutting down"
  if ::Middleman::JRUBY
  else
    Process.kill(kill_command, @server_job)
    Process.wait @server_job
    @server_job = nil
  end
end

def tell_server(params={})

Parameters:
  • params (Hash) -- Keys to be hashed and sent to server
def tell_server(params={})
  uri = URI.parse("http://#{@options[:host]}:#{@options[:port]}/__middleman__")
  Net::HTTP.post_form(uri, {}.merge(params))
end