Skip to content
This repository
Browse code

Add `scope` DSL method.

Allow the Guard scope to be defined from the
`Guardfile` with the `scope` DSL method
  • Loading branch information...
commit aaf779d221daf77809a25c436b6c788d67882f5b 1 parent ad699c5
Michael Kessler netzpirat authored
3  CHANGELOG.md
Source Rendered
@@ -2,6 +2,7 @@
2 2
3 3 ### New features
4 4
  5 +- Allow the Guard scope to be defined from the `Guardfile` with the `scope` DSL method. ([@netzpirat][])
5 6 - [#378][] Scope plugins and groups from CLI and interactor. ([@netzpirat][])
6 7 - [#369][] Allow Guard plugins to specify their template location. ([@schmurfy][])
7 8 - [#364][] Add `ignore!` and `filter!` DSL methods. ([@tarsolya][])
@@ -775,4 +776,4 @@ The Listen integration has been supervised by [@thibaudgg][] and executed by [@M
775 776 [@waldo]: https://github.com/waldo
776 777 [@wereHamster]: https://github.com/wereHamster
777 778 [@yannlugrin]: https://github.com/yannlugrin
778   -[@zonque]: https://github.com/zonque
  779 +[@zonque]: https://github.com/zonque
12 Guardfile
... ... @@ -1,3 +1,5 @@
  1 +scope :group => :specs
  2 +
1 3 group :specs do
2 4 guard :rspec, :cli => '--fail-fast --format doc' do
3 5 watch(%r{^spec/.+_spec\.rb$})
@@ -6,8 +8,8 @@ group :specs do
6 8 end
7 9 end
8 10
9   -#group :docs do
10   -# guard :ronn do
11   -# watch(%r{^man/.+\.ronn?$})
12   -# end
13   -#end
  11 +group :docs do
  12 + guard :ronn do
  13 + watch(%r{^man/.+\.ronn?$})
  14 + end
  15 +end
23 README.md
Source Rendered
@@ -646,6 +646,29 @@ $ guard -g specs
646 646
647 647 Guard plugins that don't belong to a group are considered global and are always run.
648 648
  649 +### scope
  650 +
  651 +The `scope` method allows you to define the default plugin or group scope for Guard, if not
  652 +specified as command line option. Thus command line group and plugin scope takes precedence over
  653 +the DSL scope configuration.
  654 +
  655 +You can define either a single plugin or group:
  656 +
  657 +```ruby
  658 +scope :plugin => :rspec
  659 +scope :group => :docs
  660 +```
  661 +
  662 +or specify multiple plugins or groups.
  663 +
  664 +```ruby
  665 +scope :plugins => [:test, :jasmine]
  666 +scope :groups => [:docs, :frontend]
  667 +```
  668 +
  669 +If you define both the plugin and group scope, the plugin scope has precedence. If you use both the
  670 +plural and the singular option, the plural has precedence.
  671 +
649 672 ### notification
650 673
651 674 If you don't specify any notification configuration in your `Guardfile`, Guard goes through the list of available
10 lib/guard.rb
@@ -46,7 +46,7 @@ class << self
46 46 def setup(options = {})
47 47 @running = true
48 48 @lock = Mutex.new
49   - @options = options
  49 + @options = options.dup
50 50 @watchdir = (options[:watchdir] && File.expand_path(options[:watchdir])) || Dir.pwd
51 51 @runner = ::Guard::Runner.new
52 52 @scope = { :plugins => [], :groups => []}
@@ -68,12 +68,12 @@ def setup(options = {})
68 68 ::Guard::Dsl.evaluate_guardfile(options)
69 69 ::Guard::UI.error 'No guards found in Guardfile, please add at least one.' if @guards.empty?
70 70
71   - if options[:group]
72   - @scope[:groups] = options[:group].map { |g| ::Guard.groups(g) }
  71 + if @options[:group]
  72 + @scope[:groups] = @options[:group].map { |g| ::Guard.groups(g) }
73 73 end
74 74
75   - if options[:plugin]
76   - @scope[:plugins] = options[:plugin].map { |p| ::Guard.guards(p) }
  75 + if @options[:plugin]
  76 + @scope[:plugins] = @options[:plugin].map { |p| ::Guard.guards(p) }
77 77 end
78 78
79 79 runner.deprecation_warning if @options[:show_deprecations]
28 lib/guard/dsl.rb
@@ -553,5 +553,33 @@ def logger(options)
553 553 ::Guard::UI.options = ::Guard::UI.options.merge options
554 554 end
555 555
  556 + # Sets the default scope on startup
  557 + #
  558 + # @example Scope Guard to a single group
  559 + # scope :group => :frontend
  560 + #
  561 + # @example Scope Guard to multiple groups
  562 + # scope :groups => [:specs, :docs]
  563 + #
  564 + # @example Scope Guard to a single plugin
  565 + # scope :plugin => :test
  566 + #
  567 + # @example Scope Guard to multiple plugins
  568 + # scope :plugins => [:jasmine, :rspec]
  569 + #
  570 + # @param [Hash] scopes the scope for the groups and plugins
  571 + #
  572 + def scope(scopes = {})
  573 + if ::Guard.options[:plugin].empty?
  574 + ::Guard.options[:plugin] = [scopes[:plugin]] if scopes[:plugin]
  575 + ::Guard.options[:plugin] = scopes[:plugins] if scopes[:plugins]
  576 + end
  577 +
  578 + if ::Guard.options[:group].empty?
  579 + ::Guard.options[:group] = [scopes[:group]] if scopes[:group]
  580 + ::Guard.options[:group] = scopes[:groups] if scopes[:groups]
  581 + end
  582 + end
  583 +
556 584 end
557 585 end
70 spec/guard/dsl_spec.rb
@@ -593,6 +593,76 @@ def self.call(guard_class, event, args)
593 593 end
594 594 end
595 595
  596 + describe '#scope' do
  597 + context 'with an existing command line plugin scope' do
  598 + before do
  599 + ::Guard.options[:plugin] = ['rspec']
  600 + ::Guard.options[:group] = []
  601 + end
  602 +
  603 + it 'does not use the DSL scope plugin' do
  604 + described_class.evaluate_guardfile(:guardfile_contents => 'scope :plugin => :baz')
  605 + ::Guard.options[:plugin].should eql(['rspec'])
  606 + end
  607 +
  608 + it 'does not use the DSL scope plugins' do
  609 + described_class.evaluate_guardfile(:guardfile_contents => 'scope :plugins => [:foo, :bar]')
  610 + ::Guard.options[:plugin].should eql(['rspec'])
  611 + end
  612 + end
  613 +
  614 + context 'without an existing command line plugin scope' do
  615 + before do
  616 + ::Guard.options[:plugin] = []
  617 + ::Guard.options[:group] = []
  618 + end
  619 +
  620 + it 'does use the DSL scope plugin' do
  621 + described_class.evaluate_guardfile(:guardfile_contents => 'scope :plugin => :baz')
  622 + ::Guard.options[:plugin].should eql([:baz])
  623 + end
  624 +
  625 + it 'does use the DSL scope plugins' do
  626 + described_class.evaluate_guardfile(:guardfile_contents => 'scope :plugins => [:foo, :bar]')
  627 + ::Guard.options[:plugin].should eql([:foo, :bar])
  628 + end
  629 + end
  630 +
  631 + context 'with an existing command line group scope' do
  632 + before do
  633 + ::Guard.options[:plugin] = []
  634 + ::Guard.options[:group] = ['frontend']
  635 + end
  636 +
  637 + it 'does not use the DSL scope plugin' do
  638 + described_class.evaluate_guardfile(:guardfile_contents => 'scope :group => :baz')
  639 + ::Guard.options[:group].should eql(['frontend'])
  640 + end
  641 +
  642 + it 'does not use the DSL scope plugins' do
  643 + described_class.evaluate_guardfile(:guardfile_contents => 'scope :groups => [:foo, :bar]')
  644 + ::Guard.options[:group].should eql(['frontend'])
  645 + end
  646 + end
  647 +
  648 + context 'without an existing command line group scope' do
  649 + before do
  650 + ::Guard.options[:plugin] = []
  651 + ::Guard.options[:group] = []
  652 + end
  653 +
  654 + it 'does use the DSL scope group' do
  655 + described_class.evaluate_guardfile(:guardfile_contents => 'scope :group => :baz')
  656 + ::Guard.options[:group].should eql([:baz])
  657 + end
  658 +
  659 + it 'does use the DSL scope groups' do
  660 + described_class.evaluate_guardfile(:guardfile_contents => 'scope :groups => [:foo, :bar]')
  661 + ::Guard.options[:group].should eql([:foo, :bar])
  662 + end
  663 + end
  664 + end
  665 +
596 666 private
597 667
598 668 def fake_guardfile(name, contents)

0 comments on commit aaf779d

Please sign in to comment.
Something went wrong with that request. Please try again.