class Guard::Foreman

Constants

DEFAULT_LOG_LOCATION

Default log location (Rails in mind)

TITLE

Public Class Methods

new(options = {}) click to toggle source

Initialize a Guard. @param [Array<Guard::Watcher>] watchers the Guard file watchers @param [Hash] options the custom Guard options

Calls superclass method
# File lib/guard/foreman.rb, line 15
def initialize(options = {})
  @log_file       = options.fetch(:log_file, DEFAULT_LOG_LOCATION)
  @concurrency    = options[:concurrency]
  @env            = options[:env]
  @procfile       = options[:procfile]
  @port           = options[:port]
  @root           = options[:root]

  super
end

Public Instance Methods

reload() click to toggle source

Called when ‘reload|r|z + enter` is pressed. This method should be mainly used for “reload” (really!) actions like reloading passenger/spork/bundler/… @raise [:task_has_failed] when reload has failed

# File lib/guard/foreman.rb, line 70
def reload
  info "Restarting Foreman..."
  stop
  start
end
run_all() click to toggle source

Called when just ‘enter` is pressed This method should be principally used for long action like running all specs/tests/… @raise [:task_has_failed] when run_all has failed

# File lib/guard/foreman.rb, line 79
def run_all
  start
end
run_on_additions(paths) click to toggle source
# File lib/guard/foreman.rb, line 90
def run_on_additions(paths)
  reload
end
run_on_changes(paths) click to toggle source

Called on file(s) modifications that the Guard watches. @param [Array<String>] paths the changes files or paths @raise [:task_has_failed] when run_on_change has failed

# File lib/guard/foreman.rb, line 86
def run_on_changes(paths)
  reload
end
run_on_modifications(paths) click to toggle source
# File lib/guard/foreman.rb, line 94
def run_on_modifications(paths)
  reload
end
run_on_removals(paths) click to toggle source
# File lib/guard/foreman.rb, line 98
def run_on_removals(paths)
  reload
end
start() click to toggle source

Call once when Guard starts. Please override initialize method to init stuff. @raise [:task_has_failed] when start has failed

# File lib/guard/foreman.rb, line 28
def start
  # Stop if running
  stop if @pid

  cmd = "foreman start"
  cmd += " -c #{@concurrency}" if @concurrency
  cmd += " -e #{@env}"         if @env
  cmd += " -f #{@procfile}"    if @procfile
  cmd += " -p #{@port}"        if @port
  cmd += " -d #{@root}"        if @root
  #cmd += " > #{@log_file}"     # Disabled for now

  debug "About to run #{cmd}"
  @pid = ::Spoon.spawnp(*cmd.split(" "))     # Spoon is a little weird

 info "Foreman started."
 debug "Foreman has pid #{@pid}"
 success "Foreman started"
end
stop() click to toggle source

Called when ‘stop|quit|exit|s|q|e + enter` is pressed (when Guard quits). @raise [:task_has_failed] when stop has failed

# File lib/guard/foreman.rb, line 50
def stop
  if @pid
    begin
      debug "Asking Foreman to terminate... (#{@pid})"
      ::Process.kill("TERM", @pid)
      debug "Waiting for Foreman to stop..."
      ::Process.waitpid(@pid)
      @pid = nil          # Unset @pid
    rescue Errno::ESRCH
      # Don't do anything, the process does not exist
      debug "Could not find Foreman process"
    end
    info "Foreman stopped."
  end
end

Private Instance Methods

debug(msg) click to toggle source
# File lib/guard/foreman.rb, line 108
def debug(msg)
  UI.debug(msg)
end
failure(msg) click to toggle source
# File lib/guard/foreman.rb, line 116
def failure(msg)
  ::Guard::Notifier.notify(msg, title: TITLE, image: :failure)
end
info(msg) click to toggle source
# File lib/guard/foreman.rb, line 104
def info(msg)
  UI.info(msg)
end
success(msg) click to toggle source
# File lib/guard/foreman.rb, line 112
def success(msg)
  ::Guard::Notifier.notify(msg, title: TITLE, image: :success)
end