Module: Guard::Setuper

Included in:
Guard
Defined in:
lib/guard/setuper.rb

Constant Summary

DEFAULT_OPTIONS =
{
  clear: false,
  notify: true,
  debug: false,
  group: [],
  plugin: [],
  watchdir: nil,
  guardfile: nil,
  no_interactions: false,
  no_bundler_warning: false,
  show_deprecations: false,
  latency: nil,
  force_polling: false,
  wait_for_delay: nil,
  listen_on: nil
}
DEFAULT_GROUPS =
[:default]

Instance Method Summary collapse

Instance Method Details

#clear_optionsObject

Clear Guard's options hash



95
96
97
# File 'lib/guard/setuper.rb', line 95

def clear_options
  @options = nil
end

#evaluate_guardfileObject

Evaluates the Guardfile content. It displays an error message if no Guard plugins are instantiated after the Guardfile evaluation.



145
146
147
148
# File 'lib/guard/setuper.rb', line 145

def evaluate_guardfile
  evaluator.evaluate_guardfile
  ::Guard::UI.error 'No plugins found in Guardfile, please add at least one.' if plugins.empty?
end

#evaluatorObject

Lazy initializer for Guardfile evaluator



81
82
83
# File 'lib/guard/setuper.rb', line 81

def evaluator
  @evaluator ||= ::Guard::Guardfile::Evaluator.new(@opts || {})
end

#interactorObject

Lazy initializer the interactor unless the user has specified not to.



87
88
89
90
91
# File 'lib/guard/setuper.rb', line 87

def interactor
  return if options[:no_interactions] || !::Guard::Interactor.enabled

  @interactor ||= ::Guard::Interactor.new
end

#optionsObject

Lazy initializer for Guard's options hash



75
76
77
# File 'lib/guard/setuper.rb', line 75

def options
  @options ||= ::Guard::Options.new(@opts, DEFAULT_OPTIONS)
end

#reset_groupsObject

Initializes the groups array with the default group(s).

See Also:



103
104
105
# File 'lib/guard/setuper.rb', line 103

def reset_groups
  @groups = DEFAULT_GROUPS.map { |name| Group.new(name) }
end

#reset_pluginsObject

Initializes the plugins array to an empty array.

See Also:



111
112
113
# File 'lib/guard/setuper.rb', line 111

def reset_plugins
  @plugins = []
end

#reset_scopeObject

Initializes the scope hash to `{ groups: [], plugins: [] }`.

See Also:

  • Guard.setup_scope


119
120
121
# File 'lib/guard/setuper.rb', line 119

def reset_scope
  @scope = { groups: [], plugins: [] }
end

#setup(opts = {}) ⇒ Guard

Initializes the Guard singleton:

  • Initialize the internal Guard state;

  • Create the interactor when necessary for user interaction;

  • Select and initialize the file change listener.

Parameters:

  • options (Hash)

    a customizable set of options

Returns:

  • (Guard)

    the Guard singleton



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/guard/setuper.rb', line 42

def setup(opts = {})
  _reset_lazy_accessors
  @running   = true
  @lock      = Mutex.new
  @opts      = opts
  @watchdirs = [Dir.pwd]
  @runner    = ::Guard::Runner.new

  if options[:watchdir]
    # Ensure we have an array
    @watchdirs = Array(options[:watchdir]).map { |dir| File.expand_path dir }
  end

  ::Guard::UI.clear(force: true)
  _setup_debug if options[:debug]
  _setup_listener
  _setup_signal_traps

  reset_groups
  reset_plugins
  reset_scope

  evaluate_guardfile

  setup_scope(groups: options[:group], plugins: options[:plugin])

  _setup_notifier

  self
end

#setup_scope(new_scope) ⇒ Object

Stores the scopes defined by the user via the `–group` / `-g` option (to run only a specific group) or the `–plugin` / `-P` option (to run only a specific plugin).

See Also:



130
131
132
133
134
135
136
137
138
# File 'lib/guard/setuper.rb', line 130

def setup_scope(new_scope)
  if new_scope[:groups] && new_scope[:groups].any?
    scope[:groups]  = new_scope[:groups].map { |group| ::Guard.add_group(group) }
  end

  if new_scope[:plugins] && new_scope[:plugins].any?
    scope[:plugins] = new_scope[:plugins].map { |plugin| ::Guard.plugin(plugin) }
  end
end