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 collapse

PLUGIN_FAILED =
"%s has failed, other group's plugins will be skipped."
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



110
111
112
# File 'lib/guard/runner.rb', line 110

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

Instance Method Details

#_supervise(plugin, 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:

  • plugin (Guard::Plugin)

    guard 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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/guard/runner.rb', line 78

def _supervise(plugin, task, *args)
  catch self.class.stopping_symbol_for(plugin) do
    plugin.hook("#{ task }_begin", *args)
    result = UI.options.with_progname(plugin.class.name) do
      begin
        plugin.send(task, *args)
      rescue Interrupt
        throw(:task_has_failed)
      end
    end
    plugin.hook("#{ task }_end", result)
    result
  end
rescue ScriptError, StandardError, RuntimeError
  UI.error("#{ plugin.class.name } failed to achieve its"\
                    " <#{ task }>, exception was:" \
                    "\n#{ $!.class }: #{ $!.message }" \
                    "\n#{ $!.backtrace.join("\n") }")
  Guard.state.session.plugins.remove(plugin)
  UI.info("\n#{ plugin.class.name } has just been fired")
  $!
end

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

Runs a Guard-task on all registered plugins.

on

Parameters:

  • task (Symbol)

    the task to run

  • scope_hash (Hash) (defaults to: {})

    either the Guard plugin or the group to run the task



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

def run(task, scope_hash = {})
  Lumberjack.unit_of_work do
    items = Guard.state.scope.grouped_plugins(scope_hash || {})
    items.each do |_group, plugins|
      _run_group_plugins(plugins) do |plugin|
        _supervise(plugin, task) if plugin.respond_to?(task)
      end
    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.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/guard/runner.rb', line 44

def run_on_changes(modified, added, removed)
  types = {
    MODIFICATION_TASKS => modified,
    ADDITION_TASKS => added,
    REMOVAL_TASKS => removed
  }

  UI.clearable

  Guard.state.scope.grouped_plugins.each do |_group, plugins|
    _run_group_plugins(plugins) do |plugin|
      UI.clear
      types.each do |tasks, unmatched_paths|
        next if unmatched_paths.empty?
        match_result = Watcher.match_files(plugin, unmatched_paths)
        next if match_result.empty?
        task = tasks.detect { |meth| plugin.respond_to?(meth) }
        _supervise(plugin, task, match_result) if task
      end
    end
  end
end