Permalink
Browse files

Add support to scope plugins and groups from the interactor and cli.

  • Loading branch information...
1 parent 116e5d3 commit 864e13c287f7e22b8375a77b1031fc3fd0dbce8e @netzpirat netzpirat committed Dec 11, 2012
View
@@ -374,7 +374,7 @@ Notifications can also be disabled globally by setting a `GUARD_NOTIFY` environm
#### `-g`/`--group` option
-Only certain plugin groups can be run:
+Scope Guard to certain plugin groups on start:
```bash
$ guard --group group_name another_group_name
@@ -383,6 +383,15 @@ $ guard -g group_name another_group_name # shortcut
See the Guardfile DSL below for creating groups.
+#### `-P`/`--plugins` option
+
+Scope Guard to certain plugins on start:
+
+```bash
+$ guard --plugins plugin_name another_plugin_name
+$ guard -p plugin_name another_plugin_name # shortcut
+```
+
#### `-d`/`--debug` option
Guard can display debug information which can be very usefull for plugins
@@ -499,6 +508,7 @@ commands:
* `n`, `notification`: Toggles the notifications.
* `p`, `pause`: Toggles the file listener.
* `r`, `reload`: Reload all plugins.
+ * `o`, `scope`: Scope Guard actions to plugins or groups.
* `s`, `show`: Show all Guard plugins.
* `e`, `exit`: Stop all plugins and quit Guard
View
@@ -22,7 +22,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'bundler'
s.add_development_dependency 'rspec', '~> 2.12.0'
- s.add_development_dependency 'guard-rspec', '~> 2.1.0'
+ s.add_development_dependency 'guard-rspec', '~> 2.3.0'
s.files = Dir.glob('{bin,images,lib}/**/*') + %w[CHANGELOG.md LICENSE man/guard.1 man/guard.1.html README.md]
s.executable = 'guard'
View
@@ -26,7 +26,7 @@ module Guard
DEV_NULL = WINDOWS ? "NUL" : "/dev/null"
class << self
- attr_accessor :options, :interactor, :runner, :listener, :lock
+ attr_accessor :options, :interactor, :runner, :listener, :lock, :scope
# Initialize the Guard singleton:
#
@@ -48,8 +48,9 @@ def setup(options = {})
@options = options
@watchdir = (options[:watchdir] && File.expand_path(options[:watchdir])) || Dir.pwd
@runner = ::Guard::Runner.new
+ @scope = { :plugins => [], :groups => []}
- if @options[:debug]
+ if options[:debug]
Thread.abort_on_exception = true
::Guard::UI.options[:level] = :debug
debug_command_execution
@@ -66,6 +67,14 @@ def setup(options = {})
::Guard::Dsl.evaluate_guardfile(options)
::Guard::UI.error 'No guards found in Guardfile, please add at least one.' if @guards.empty?
+ if options[:group]
+ @scope[:groups] = options[:group].map { |g| ::Guard.groups(g) }
+ end
+
+ if options[:plugin]
+ @scope[:plugins] = options[:plugin].map { |p| ::Guard.guards(p) }
+ end
+
runner.deprecation_warning if @options[:show_deprecations]
setup_notifier
@@ -189,6 +198,8 @@ def stop
# @param [Hash] scopes hash with a Guard plugin or a group scope
#
def reload(scopes = {})
+ scopes = convert_scopes(scopes)
+
within_preserved_state do
::Guard::UI.clear(:force => true)
::Guard::UI.action_with_scopes('Reload', scopes)
@@ -206,6 +217,8 @@ def reload(scopes = {})
# @param [Hash] scopes hash with a Guard plugin or a group scope
#
def run_all(scopes = {})
+ scopes = convert_scopes(scopes)
+
within_preserved_state do
::Guard::UI.clear(:force => true)
::Guard::UI.action_with_scopes('Run', scopes)
@@ -448,5 +461,24 @@ def deprecated_options_warning
::Guard::UI.deprecation(NO_VENDOR_DEPRECATION) if options[:no_vendor]
end
+ # Convert the old scope format to the new scope format.
+ #
+ # @example Convert old scopes
+ # convert_scopes({ :guard => :rspec, :group => :backend })
+ # => { :plugins => [:rspec], :groups => [:backend] }
+ #
+ def convert_scopes(scopes)
+ if scopes[:guard]
+ scopes[:plugins] = [scopes[:guard]]
+ scopes.delete(:guard)
+ end
+
+ if scopes[:group]
+ scopes[:groups] = [scopes[:group]]
+ scopes.delete(:group)
+ end
+
+ scopes
+ end
end
end
View
@@ -41,6 +41,12 @@ class CLI < Thor
:aliases => '-g',
:banner => 'Run only the passed groups'
+ method_option :plugins,
+ :type => :array,
+ :default => [],
+ :aliases => '-P',
+ :banner => 'Run only the passed plugins'
+
method_option :watchdir,
:type => :string,
:aliases => '-w',
@@ -0,0 +1,32 @@
+module Guard
+ class Interactor
+
+ SCOPE = Pry::CommandSet.new do
+ create_command 'scope' do
+
+ group 'Guard'
+ description 'Scope Guard actions to groups and plugins.'
+
+ banner <<-BANNER
+ Usage: scope <scope>
+
+ Set the global Guard scope.
+ BANNER
+
+ def process(*entries)
+ scope, rest = ::Guard::Interactor.convert_scope(entries)
+
+ if rest.length == 0
+ ::Guard.scope = scope
+ else
+ output.puts "Unkown scope #{ rest.join(', ') }"
+ end
+ end
+
+ end
+ end
+
+ end
+end
+
+Pry.commands.import ::Guard::Interactor::SCOPE
View
@@ -361,10 +361,9 @@ def interactor(options)
# @see Guard::DslDescriber
#
def group(name, options = {})
- @groups = @@options[:group] || []
name = name.to_sym
- if block_given? && (@groups.empty? || @groups.map(&:to_sym).include?(name))
+ if block_given?
::Guard.add_group(name.to_s.downcase, options)
@current_group = name
View
@@ -37,7 +37,7 @@ def initialize(name, options = {})
# @return [String] the group name
#
def to_s
- "#{@name} group"
+ @name.to_s.capitalize
end
end
View
@@ -15,22 +15,36 @@ class Interactor
require 'guard/commands/notification'
require 'guard/commands/pause'
require 'guard/commands/reload'
+ require 'guard/commands/scope'
require 'guard/commands/show'
# The default Ruby script to configure Guard Pry if the option `:guard_rc` is not defined.
- GUARD_RC = '~/.guardrc'
+ GUARD_RC = '~/.guardrc'
# The default Guard Pry history file if the option `:history_file` is not defined.
HISTORY_FILE = '~/.guard_history'
+ # List of shortcuts for each interactor command
+ SHORTCUTS = {
+ :help => 'h',
+ :reload => 'r',
+ :change => 'c',
+ :show => 's',
+ :scope => 'o',
+ :notification => 'n',
+ :pause => 'p',
+ :exit => 'e',
+ :quit => 'q'
+ }
+
class << self
# Get the interactor options
#
# @return [Hash] the options
#
def options
- @options ||= {}
+ @options ||= { }
end
# Set the interactor options
@@ -68,9 +82,9 @@ def enabled=(status)
def initialize
return if ENV['GUARD_ENV'] == 'test'
- Pry.config.should_load_rc = false
+ Pry.config.should_load_rc = false
Pry.config.should_load_local_rc = false
- Pry.config.history.file = self.class.options[:history_file] || HISTORY_FILE
+ Pry.config.history.file = self.class.options[:history_file] || HISTORY_FILE
load_guard_rc
@@ -101,12 +115,12 @@ def create_run_all_command
end
# Creates command aliases for the commands
- # `help`, `reload`, `change`, `show`, `notification`, `pause`, `exit` and `quit`,
+ # `help`, `reload`, `change`, `scope`, `notification`, `pause`, `exit` and `quit`,
# which will be the first letter of the command.
#
def create_command_aliases
- %w(help reload change show notification pause exit quit).each do |command|
- Pry.commands.alias_command command[0].chr, command
+ SHORTCUTS.each do |command, shortcut|
+ Pry.commands.alias_command shortcut, command
end
end
@@ -155,10 +169,34 @@ def process
def configure_prompt
Pry.config.prompt = [
proc do |target_self, nest_level, pry|
- "[#{ pry.input_array.size }] #{ ::Guard.listener.paused? ? 'pause' : 'guard' }(#{ Pry.view_clip(target_self) })#{":#{ nest_level }" unless nest_level.zero? }> "
+ history = pry.input_array.size
+ process = ::Guard.listener.paused? ? 'pause' : 'guard'
+ clip = Pry.view_clip(target_self)
+ level = ":#{ nest_level }" unless nest_level.zero?
+ scope = if !::Guard.scope[:plugins].empty?
+ "{#{ ::Guard.scope[:plugins].join }} "
+ elsif !::Guard.scope[:groups].empty?
+ "{#{ ::Guard.scope[:groups].join }} "
+ else
+ ''
+ end
+
+ "[#{ history }] #{ scope }#{ process }(#{ clip })#{ level }> "
end,
proc do |target_self, nest_level, pry|
- "[#{ pry.input_array.size }] #{ ::Guard.listener.paused? ? 'pause' : 'guard' }(#{ Pry.view_clip(target_self) })#{":#{ nest_level }" unless nest_level.zero? }* "
+ history = pry.input_array.size
+ process = ::Guard.listener.paused? ? 'pause' : 'guard'
+ clip = Pry.view_clip(target_self)
+ level = ":#{ nest_level }" unless nest_level.zero?
+ scope = if !::Guard.scope[:plugins].empty?
+ "{#{ ::Guard.scope[:plugins].join }} "
+ elsif !::Guard.scope[:groups].empty?
+ "{#{ ::Guard.scope[:groups].join }} "
+ else
+ ''
+ end
+
+ "[#{ history }] #{ scope }#{ process }(#{ clip })#{ level }* "
end
]
end
@@ -208,13 +246,13 @@ def stty_exists?
# when stopping.
#
def store_terminal_settings
- @stty_save = `stty -g 2>#{DEV_NULL}`.chomp
+ @stty_save = `stty -g 2>#{ DEV_NULL }`.chomp
end
# Restore terminal settings
#
def restore_terminal_settings
- system("stty #{ @stty_save } 2>#{DEV_NULL}") if @stty_save
+ system("stty #{ @stty_save } 2>#{ DEV_NULL }") if @stty_save
end
# Converts and validates a plain text scope
@@ -224,14 +262,14 @@ def restore_terminal_settings
# @return [Hash, Array<String>] the plugin or group scope, the unknown entries
#
def self.convert_scope(entries)
- scopes = { }
+ scopes = { :plugins => [], :groups => [] }
unknown = []
entries.each do |entry|
- if guard = ::Guard.guards(entry)
- scopes[:guard] ||= guard
+ if plugin = ::Guard.guards(entry)
+ scopes[:plugins] << plugin
elsif group = ::Guard.groups(entry)
- scopes[:group] ||= group
+ scopes[:groups] << group
else
unknown << entry
end
Oops, something went wrong.

0 comments on commit 864e13c

Please sign in to comment.