Skip to content

Commit

Permalink
Add scope DSL method.
Browse files Browse the repository at this point in the history
Allow the Guard scope to be defined from the
`Guardfile` with the `scope` DSL method
  • Loading branch information
netzpirat committed Dec 19, 2012
1 parent ad699c5 commit aaf779d
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

### New features

- Allow the Guard scope to be defined from the `Guardfile` with the `scope` DSL method. ([@netzpirat][])
- [#378][] Scope plugins and groups from CLI and interactor. ([@netzpirat][])
- [#369][] Allow Guard plugins to specify their template location. ([@schmurfy][])
- [#364][] Add `ignore!` and `filter!` DSL methods. ([@tarsolya][])
Expand Down Expand Up @@ -775,4 +776,4 @@ The Listen integration has been supervised by [@thibaudgg][] and executed by [@M
[@waldo]: https://github.com/waldo
[@wereHamster]: https://github.com/wereHamster
[@yannlugrin]: https://github.com/yannlugrin
[@zonque]: https://github.com/zonque
[@zonque]: https://github.com/zonque
12 changes: 7 additions & 5 deletions Guardfile
@@ -1,3 +1,5 @@
scope :group => :specs

group :specs do
guard :rspec, :cli => '--fail-fast --format doc' do
watch(%r{^spec/.+_spec\.rb$})
Expand All @@ -6,8 +8,8 @@ group :specs do
end
end

#group :docs do
# guard :ronn do
# watch(%r{^man/.+\.ronn?$})
# end
#end
group :docs do
guard :ronn do
watch(%r{^man/.+\.ronn?$})
end
end
23 changes: 23 additions & 0 deletions README.md
Expand Up @@ -646,6 +646,29 @@ $ guard -g specs

Guard plugins that don't belong to a group are considered global and are always run.

### scope

The `scope` method allows you to define the default plugin or group scope for Guard, if not
specified as command line option. Thus command line group and plugin scope takes precedence over
the DSL scope configuration.

You can define either a single plugin or group:

```ruby
scope :plugin => :rspec
scope :group => :docs
```

or specify multiple plugins or groups.

```ruby
scope :plugins => [:test, :jasmine]
scope :groups => [:docs, :frontend]
```

If you define both the plugin and group scope, the plugin scope has precedence. If you use both the
plural and the singular option, the plural has precedence.

### notification

If you don't specify any notification configuration in your `Guardfile`, Guard goes through the list of available
Expand Down
10 changes: 5 additions & 5 deletions lib/guard.rb
Expand Up @@ -46,7 +46,7 @@ class << self
def setup(options = {})
@running = true
@lock = Mutex.new
@options = options
@options = options.dup
@watchdir = (options[:watchdir] && File.expand_path(options[:watchdir])) || Dir.pwd
@runner = ::Guard::Runner.new
@scope = { :plugins => [], :groups => []}
Expand All @@ -68,12 +68,12 @@ 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) }
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) }
if @options[:plugin]
@scope[:plugins] = @options[:plugin].map { |p| ::Guard.guards(p) }
end

runner.deprecation_warning if @options[:show_deprecations]
Expand Down
28 changes: 28 additions & 0 deletions lib/guard/dsl.rb
Expand Up @@ -553,5 +553,33 @@ def logger(options)
::Guard::UI.options = ::Guard::UI.options.merge options
end

# Sets the default scope on startup
#
# @example Scope Guard to a single group
# scope :group => :frontend
#
# @example Scope Guard to multiple groups
# scope :groups => [:specs, :docs]
#
# @example Scope Guard to a single plugin
# scope :plugin => :test
#
# @example Scope Guard to multiple plugins
# scope :plugins => [:jasmine, :rspec]
#
# @param [Hash] scopes the scope for the groups and plugins
#
def scope(scopes = {})
if ::Guard.options[:plugin].empty?
::Guard.options[:plugin] = [scopes[:plugin]] if scopes[:plugin]
::Guard.options[:plugin] = scopes[:plugins] if scopes[:plugins]
end

if ::Guard.options[:group].empty?
::Guard.options[:group] = [scopes[:group]] if scopes[:group]
::Guard.options[:group] = scopes[:groups] if scopes[:groups]
end
end

end
end
70 changes: 70 additions & 0 deletions spec/guard/dsl_spec.rb
Expand Up @@ -593,6 +593,76 @@ def self.call(guard_class, event, args)
end
end

describe '#scope' do
context 'with an existing command line plugin scope' do
before do
::Guard.options[:plugin] = ['rspec']
::Guard.options[:group] = []
end

it 'does not use the DSL scope plugin' do
described_class.evaluate_guardfile(:guardfile_contents => 'scope :plugin => :baz')
::Guard.options[:plugin].should eql(['rspec'])
end

it 'does not use the DSL scope plugins' do
described_class.evaluate_guardfile(:guardfile_contents => 'scope :plugins => [:foo, :bar]')
::Guard.options[:plugin].should eql(['rspec'])
end
end

context 'without an existing command line plugin scope' do
before do
::Guard.options[:plugin] = []
::Guard.options[:group] = []
end

it 'does use the DSL scope plugin' do
described_class.evaluate_guardfile(:guardfile_contents => 'scope :plugin => :baz')
::Guard.options[:plugin].should eql([:baz])
end

it 'does use the DSL scope plugins' do
described_class.evaluate_guardfile(:guardfile_contents => 'scope :plugins => [:foo, :bar]')
::Guard.options[:plugin].should eql([:foo, :bar])
end
end

context 'with an existing command line group scope' do
before do
::Guard.options[:plugin] = []
::Guard.options[:group] = ['frontend']
end

it 'does not use the DSL scope plugin' do
described_class.evaluate_guardfile(:guardfile_contents => 'scope :group => :baz')
::Guard.options[:group].should eql(['frontend'])
end

it 'does not use the DSL scope plugins' do
described_class.evaluate_guardfile(:guardfile_contents => 'scope :groups => [:foo, :bar]')
::Guard.options[:group].should eql(['frontend'])
end
end

context 'without an existing command line group scope' do
before do
::Guard.options[:plugin] = []
::Guard.options[:group] = []
end

it 'does use the DSL scope group' do
described_class.evaluate_guardfile(:guardfile_contents => 'scope :group => :baz')
::Guard.options[:group].should eql([:baz])
end

it 'does use the DSL scope groups' do
described_class.evaluate_guardfile(:guardfile_contents => 'scope :groups => [:foo, :bar]')
::Guard.options[:group].should eql([:foo, :bar])
end
end
end

private

def fake_guardfile(name, contents)
Expand Down

0 comments on commit aaf779d

Please sign in to comment.