Class: Guard::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/guard/runner.rb

Overview

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

Constant Summary

MODIFICATION_TASKS =
[:run_on_modifications, :run_on_changes, :run_on_change]
ADDITION_TASKS =
[:run_on_additions, :run_on_changes, :run_on_change]
REMOVAL_TASKS =
[:run_on_removals, :run_on_changes, :run_on_deletion]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.stopping_symbol_for(guard) ⇒ Symbol

Note:

If a Guard group is being run and it has the `:halt_on_fail` option set, this method returns :no_catch as it will be caught at the group level.

Returns the symbol that has to be caught when running a supervised task.

Parameters:

Returns:

  • (Symbol)

    the symbol to catch

See Also:

  • #_scoped_plugins


93
94
95
# File 'lib/guard/runner.rb', line 93

def self.stopping_symbol_for(guard)
  guard.group.options[:halt_on_fail] ? :no_catch : :task_has_failed
end

Instance Method Details

#run(task, scope = {}) ⇒ Object

Runs a Guard-task on all registered plugins.

Parameters:

  • task (Symbol)

    the task to run

  • scopes (Hash)

    either the Guard plugin or the group to run the task on

See Also:

  • Guard::Runner.selfself.run_supervised_task


19
20
21
22
23
24
25
# File 'lib/guard/runner.rb', line 19

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

#run_on_changes(modified, added, removed) ⇒ Object

Runs the appropriate tasks on all registered plugins based on the passed changes.

Parameters:

  • modified (Array<String>)

    the modified paths.

  • added (Array<String>)

    the added paths.

  • removed (Array<String>)

    the removed paths.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/guard/runner.rb', line 38

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

#run_supervised_task(guard, task, *args) ⇒ Object

Run a Guard plugin task, but remove the Guard plugin when his work leads to a system failure.

When the Group has `:halt_on_fail` disabled, we've to catch `:task_has_failed` here in order to avoid an uncaught throw error.

Parameters:

  • guard (Guard::Plugin)

    the Guard to execute

  • task (Symbol)

    the task to run

  • args (Array)

    the arguments for the task

Raises:

  • (:task_has_failed)

    when task has failed



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/guard/runner.rb', line 63

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