Class: Guard::Interactor

Inherits:
Object
  • Object
show all
Defined in:
lib/guard/interactor.rb,
lib/guard/commands/all.rb,
lib/guard/commands/show.rb,
lib/guard/commands/scope.rb,
lib/guard/commands/pause.rb,
lib/guard/commands/reload.rb,
lib/guard/commands/change.rb,
lib/guard/commands/notification.rb

Overview

The Guard interactor is a Pry REPL with a Guard specific command set.

Constant Summary

GUARD_RC =

The default Ruby script to configure Guard Pry if the option `:guard_rc` is not defined.

'~/.guardrc'
HISTORY_FILE =

The default Guard Pry history file if the option `:history_file` is not defined.

'~/.guard_history'
SHORTCUTS =

List of shortcuts for each interactor command

{
  help:         'h',
  all:          'a',
  reload:       'r',
  change:       'c',
  show:         's',
  scope:        'o',
  notification: 'n',
  pause:        'p',
  exit:         'e',
  quit:         'q'
}
ALL =
Pry::CommandSet.new do
  create_command 'all' do

    group 'Guard'
    description 'Run all plugins.'

    banner "Usage: all <scope>\n\nRun the Guard plugin `run_all` action.\n\nYou may want to specify an optional scope to the action,\neither the name of a Guard plugin or a plugin group.\n"

    def process(*entries)
      scopes, rest = ::Guard::Interactor.convert_scope(entries)

      if rest.empty?
        ::Guard.run_all scopes
      else
        output.puts "Unknown scope #{ rest.join(', ') }"
      end
    end
  end
end
SHOW =
Pry::CommandSet.new do
  create_command 'show' do

    group 'Guard'
    description 'Show all Guard plugins.'

    banner "Usage: show\n\nShow all defined Guard plugins and their options.\n"

    def process
      ::Guard::DslDescriber.new(::Guard.options).show
    end
  end
end
SCOPE =
Pry::CommandSet.new do
  create_command 'scope' do

    group 'Guard'
    description 'Scope Guard actions to groups and plugins.'

    banner "Usage: scope <scope>\n\nSet the global Guard scope.\n"

    def process(*entries)
      scope, rest = ::Guard::Interactor.convert_scope(entries)

      if scope[:plugins].empty? && scope[:groups].empty?
        output.puts 'Usage: scope <scope>'
      else
        ::Guard.scope = scope
      end
    end

  end
end
PAUSE =
Pry::CommandSet.new do
  create_command 'pause' do

    group 'Guard'
    description 'Toggles the file listener.'

    banner "Usage: pause\n\nToggles the file listener on and off.\n\nWhen the file listener is paused, the default Guard Pry\nprompt will show the pause sign `[p]`.\n"

    def process
      ::Guard.pause
    end
  end
end
RELOAD =
Pry::CommandSet.new do
  create_command 'reload' do

    group 'Guard'
    description 'Reload all plugins.'

    banner "Usage: reload <scope>\n\nRun the Guard plugin `reload` action.\n\nYou may want to specify an optional scope to the action,\neither the name of a Guard plugin or a plugin group.\n"

    def process(*entries)
      scopes, rest = ::Guard::Interactor.convert_scope(entries)

      if rest.empty?
        ::Guard.reload scopes
      else
        output.puts "Unknown scope #{ rest.join(', ') }"
      end
    end

  end
end
CHANGE =
Pry::CommandSet.new do
  create_command 'change' do

    group 'Guard'
    description 'Trigger a file change.'

    banner "Usage: change <file> <other_file>\n\nPass the given files to the Guard plugin `run_on_changes` action.\n"

    def process(*entries)
      scopes, files = ::Guard::Interactor.convert_scope(entries)

      if files.empty?
        output.puts 'Please specify a file.'
      else
        ::Guard.within_preserved_state do
          ::Guard.runner.run_on_changes(files, [], [])
        end
      end
    end

  end
end
NOTIFICATION =
Pry::CommandSet.new do
  create_command 'notification' do

    group 'Guard'
    description 'Toggles the notifications.'

    banner "Usage: notification\n\nToggles the notifications on and off.\n"

    def process
      ::Guard::Notifier.toggle
    end
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInteractor

Initializes the interactor. This configures Pry and creates some custom commands and aliases for Guard.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/guard/interactor.rb', line 102

def initialize
  return if ENV['GUARD_ENV'] == 'test'

  Pry.config.should_load_rc       = false
  Pry.config.should_load_local_rc = false
  Pry.config.history.file         = File.expand_path(self.class.options[:history_file] || HISTORY_FILE)

  @stty_exists = nil
  _add_hooks

  _replace_reset_command
  _create_run_all_command
  _create_command_aliases
  _create_guard_commands
  _create_group_commands

  _configure_prompt
end

Instance Attribute Details

#threadObject

Returns the value of attribute thread



39
40
41
# File 'lib/guard/interactor.rb', line 39

def thread
  @thread
end

Class Method Details

.convert_scope(entries) ⇒ Hash, Array<String>

Converts and validates a plain text scope to a valid plugin or group scope.

Parameters:

  • entries (Array<String>)

    the text scope

Returns:

  • (Hash, Array<String>)

    the plugin or group scope, the unknown entries



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/guard/interactor.rb', line 81

def self.convert_scope(entries)
  scopes  = { plugins: [], groups: [] }
  unknown = []

  entries.each do |entry|
    if plugin = ::Guard.plugin(entry)
      scopes[:plugins] << plugin
    elsif group = ::Guard.group(entry)
      scopes[:groups] << group
    else
      unknown << entry
    end
  end

  [scopes, unknown]
end

.enabledBoolean

Is the interactor enabled?

Returns:

  • (Boolean)

    true if enabled



63
64
65
# File 'lib/guard/interactor.rb', line 63

def self.enabled
  @enabled || @enabled.nil?
end

.enabled=(status) ⇒ Object

Set the enabled status for the interactor

Parameters:

  • status (Boolean)

    true if enabled



71
72
73
# File 'lib/guard/interactor.rb', line 71

def self.enabled=(status)
  @enabled = status
end

.optionsHash

Get the interactor options

Returns:

  • (Hash)

    the options



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

def self.options
  @options ||= {}
end

.options=(options) ⇒ Object

Set the interactor options

Parameters:

  • options (Hash)

    the interactor options

Options Hash (options):

  • :guard_rc (String)

    the Ruby script to configure Guard Pry

  • :history_file (String)

    the file to write the Pry history to



55
56
57
# File 'lib/guard/interactor.rb', line 55

def self.options=(options)
  @options = options
end

Instance Method Details

#startObject

Start the line reader in its own thread and stop Guard on Ctrl-D.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/guard/interactor.rb', line 124

def start
  return if ENV['GUARD_ENV'] == 'test'

  _store_terminal_settings if _stty_exists?

  unless @thread
    ::Guard::UI.debug 'Start interactor'

    @thread = Thread.new do
      Pry.start
      ::Guard.stop
    end
  end
end

#stopObject

Kill interactor thread if not current



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/guard/interactor.rb', line 141

def stop
  return if !@thread || ENV['GUARD_ENV'] == 'test'

  unless Thread.current == @thread
    ::Guard::UI.reset_line
    ::Guard::UI.debug 'Stop interactor'
    @thread.kill
    @thread = nil
  end

  _restore_terminal_settings if _stty_exists?
end