class Guard::Runner


The runner is responsible for running all methods defined on each plugin.

def self.stopping_symbol_for(guard)

Returns:
  • (Symbol) - the symbol to catch

Parameters:
  • guard (Guard::Plugin) -- the Guard plugin to execute

Other tags:
    See: ._scoped_plugins -

Other tags:
    Note: - If a Guard group is being run and it has the `:halt_on_fail`
def self.stopping_symbol_for(guard)
  guard.group.options[:halt_on_fail] ? :no_catch : :task_has_failed
end

def _clearable?(guard, modified_paths, added_paths, removed_paths)

Parameters:
  • removed_paths (Array) -- the removed paths.
  • added_paths (Array) -- the added paths.
  • modified_paths (Array) -- the modified paths.
  • guard (Guard::Plugin) -- the Guard plugin where run_on_changes is called
def _clearable?(guard, modified_paths, added_paths, removed_paths)
  (MODIFICATION_TASKS.any? { |task| guard.respond_to?(task) } && !modified_paths.empty?) ||
  (ADDITION_TASKS.any? { |task| guard.respond_to?(task) } && !added_paths.empty?) ||
  (REMOVAL_TASKS.any? { |task| guard.respond_to?(task) } && !removed_paths.empty?)
end

def _current_groups_scope(scope)

Returns:
  • (Array) - the groups to scope to

Parameters:
  • scopes (Hash) -- hash with a local plugins or a groups scope
def _current_groups_scope(scope)
  Array(_find_non_empty_groups_scope(scope)).map do |group|
    group.is_a?(Symbol) ? ::Guard.group(group) : group
  end
end

def _current_plugins_scope(scope)

Returns:
  • (Array) - the plugins to scope to

Parameters:
  • scopes (Hash) -- hash with a local plugins or a groups scope
def _current_plugins_scope(scope)
  if plugins = _find_non_empty_plugins_scope(scope)
    Array(plugins).map do |plugin|
      plugin.is_a?(Symbol) ? ::Guard.plugin(plugin) : plugin
    end
  else
    nil
  end
end

def _find_non_empty_groups_scope(scope)


Find the first non empty groups scope
def _find_non_empty_groups_scope(scope)
  _find_non_empty_scope(:group, scope, ::Guard.groups)
end

def _find_non_empty_plugins_scope(scope)


Find the first non empty plugins scope
def _find_non_empty_plugins_scope(scope)
  _find_non_empty_scope(:plugin, scope)
end

def _find_non_empty_scope(type, local_scope, *additional_possibilities)


Find the first non empty element in the given possibilities
def _find_non_empty_scope(type, local_scope, *additional_possibilities)
  [
    local_scope[:"#{type}s"],
    local_scope[type.to_sym],
    ::Guard.scope[:"#{type}s"],
    additional_possibilities.flatten
  ].compact.find { |a| !Array(a).empty? }
end

def _run_first_task_found(guard, tasks, task_param)

Parameters:
  • task_param (Object) -- the param to pass to each task
  • tasks (Array) -- the tasks to run the first among
  • guard (Guard::Plugin) -- the Guard plugin to run the first found task on
def _run_first_task_found(guard, tasks, task_param)
  tasks.each do |task|
    if guard.respond_to?(task)
      run_supervised_task(guard, task, task_param)
      break
    else
      ::Guard::UI.debug "Trying to run #{ guard.class.name }##{ task.to_s } with #{ task_param.inspect }"
    end
  end
end

def _scoped_plugins(scopes = {})

Other tags:
    Yield: - the task to run

Parameters:
  • scopes (Hash) -- hash with plugins or a groups scope
def _scoped_plugins(scopes = {})
  if plugins = _current_plugins_scope(scopes)
    plugins.each do |guard|
      yield(guard)
    end
  else
    _current_groups_scope(scopes).each do |group|
      current_plugin = nil
      block_return = catch :task_has_failed do
        ::Guard.plugins(group: group.name).each do |guard|
          current_plugin = guard
          yield(guard)
        end
      end
      if block_return.nil?
        ::Guard::UI.info "#{ current_plugin.class.name } has failed, other group's plugins execution has been halted."
      end
    end
  end
end

def run(task, scope = {})

Other tags:
    See: self.run_supervised_task -

Parameters:
  • scopes (Hash) -- either the Guard plugin or the group to run the task on
  • task (Symbol) -- the task to run
def run(task, scope = {})
  Lumberjack.unit_of_work do
    _scoped_plugins(scope) do |guard|
      run_supervised_task(guard, task) if guard.respond_to?(task)
    end
  end
end

def run_on_changes(modified, added, removed)

Parameters:
  • removed (Array) -- the removed paths.
  • added (Array) -- the added paths.
  • modified (Array) -- the modified paths.
def run_on_changes(modified, added, removed)
  ::Guard::UI.clearable
  _scoped_plugins do |guard|
    modified_paths = ::Guard::Watcher.match_files(guard, modified)
    added_paths    = ::Guard::Watcher.match_files(guard, added)
    removed_paths  = ::Guard::Watcher.match_files(guard, removed)
    ::Guard::UI.clear if _clearable?(guard, modified_paths, added_paths, removed_paths)
    _run_first_task_found(guard, MODIFICATION_TASKS, modified_paths) unless modified_paths.empty?
    _run_first_task_found(guard, ADDITION_TASKS, added_paths) unless added_paths.empty?
    _run_first_task_found(guard, REMOVAL_TASKS, removed_paths) unless removed_paths.empty?
  end
end

def run_supervised_task(guard, task, *args)

Raises:
  • (:task_has_failed) - when task has failed

Parameters:
  • args (Array) -- the arguments for the task
  • task (Symbol) -- the task to run
  • guard (Guard::Plugin) -- the Guard to execute
def run_supervised_task(guard, task, *args)
  begin
    catch self.class.stopping_symbol_for(guard) do
      guard.hook("#{ task }_begin", *args)
      result = guard.send(task, *args)
      guard.hook("#{ task }_end", result)
      result
    end
  rescue Exception => ex
    ::Guard::UI.error("#{ guard.class.name } failed to achieve its <#{ task.to_s }>, exception was:" +
             "\n#{ ex.class }: #{ ex.message }\n#{ ex.backtrace.join("\n") }")
    ::Guard.plugins.delete guard
    ::Guard::UI.info("\n#{ guard.class.name } has just been fired")
    ex
  end
end