Class: Guard::Engine

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Internals::Helpers
Defined in:
lib/guard/engine.rb

Overview

Engine is the main orchestrator class.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Guard::Engine

Initialize a new Guard::Engine object.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • clear (Boolean)

    if auto clear the UI should be done

  • notify (Boolean)

    if system notifications should be shown

  • debug (Boolean)

    if debug output should be shown

  • group (Array<String>)

    the list of groups to start

  • watchdirs (Array<String>)

    the directories to watch

  • guardfile (String)

    the path to the Guardfile

  • inline (String)

    the inline content of a Guardfile



40
41
42
43
# File 'lib/guard/engine.rb', line 40

def initialize(options = {})
  @options = options
  Thread.current[:engine] = self
end

Instance Method Details

#evaluatorObject



49
50
51
# File 'lib/guard/engine.rb', line 49

def evaluator
  @evaluator ||= Guardfile::Evaluator.new(options)
end

#pause(expected = nil) ⇒ Object

Pause Guard listening to file changes.

Raises:

  • (ArgumentError)


164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/guard/engine.rb', line 164

def pause(expected = nil)
  states = { paused: true, unpaused: false, toggle: !paused? }
  key = expected || :toggle

  raise ArgumentError, "invalid mode: #{expected.inspect}" unless states.key?(key)

  pause = states[key]
  return if pause == paused?

  _listener.public_send(pause ? :pause : :start)
  UI.info "File event handling has been #{pause ? 'paused' : 'resumed'}"
end

#reload(*entries) ⇒ Object

Reload Guardfile and all Guard plugins currently enabled. If no scope is given, then the Guardfile will be re-evaluated, which results in a stop/start, which makes the reload obsolete.

Parameters:

  • scopes (Hash)

    hash with a Guard plugin or a group scope



144
145
146
147
148
149
# File 'lib/guard/engine.rb', line 144

def reload(*entries)
  entries.flatten!
  UI.clear(force: true)
  UI.action_with_scopes("Reload", session.scope_titles(entries))
  _runner.run(:reload, entries)
end

#run_all(*entries) ⇒ Object

Trigger run_all on all Guard plugins currently enabled.

Parameters:

  • scopes (Hash)

    hash with a Guard plugin or a group scope



155
156
157
158
159
160
# File 'lib/guard/engine.rb', line 155

def run_all(*entries)
  entries.flatten!
  UI.clear(force: true)
  UI.action_with_scopes("Run", session.scope_titles(entries))
  _runner.run(:run_all, entries)
end

#sessionObject



45
46
47
# File 'lib/guard/engine.rb', line 45

def session
  @session ||= Guard::Internals::Session.new(options)
end

#setupObject

Evaluate the Guardfile and instantiate internals.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/guard/engine.rb', line 63

def setup
  _instantiate

  UI.reset_and_clear

  if evaluator.inline?
    UI.info("Using inline Guardfile.")
  elsif evaluator.custom?
    UI.info("Using Guardfile at #{evaluator.guardfile_path}.")
  end

  self
end

#showObject



177
178
179
# File 'lib/guard/engine.rb', line 177

def show
  DslDescriber.new(self).show
end

#startObject

Start Guard by evaluating the Guardfile, initializing declared Guard plugins and starting the available file change listener. Main method for Guard that is called from the CLI when Guard starts.

  • Setup Guard internals
  • Evaluate the Guardfile
  • Configure Notifiers
  • Initialize the declared Guard plugins
  • Start the available file change listener

Parameters:

  • options (Hash)

    a customizable set of options

See Also:

  • CLI#start


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/guard/engine.rb', line 95

def start
  setup

  _initialize_listener
  _initialize_signal_traps
  _initialize_notifier

  UI.debug "Guard starts all plugins"
  _runner.run(:start)

  UI.info "Guard is now watching at '#{session.watchdirs.join("', '")}'"
  _listener.start

  exitcode = 0
  begin
    loop do
      break if _interactor.foreground == :exit

      loop do
        break unless _queue.pending?

        _queue.process
      end
    end
  rescue Interrupt
  rescue SystemExit => e
    exitcode = e.status
  end

  exitcode
ensure
  stop
end

#stopObject



129
130
131
132
133
134
135
136
# File 'lib/guard/engine.rb', line 129

def stop
  _listener.stop
  _interactor.background
  UI.debug "Guard stops all plugins"
  _runner.run(:stop)
  Notifier.disconnect
  UI.info "Bye bye...", reset: true
end

#to_sObject Also known as: inspect



53
54
55
# File 'lib/guard/engine.rb', line 53

def to_s
  "#<#{self.class}:#{object_id} @options=#{options}>"
end