Skip to content
Browse files

show the guards defined in the Guardfile with 'guard show' or 'guard -T'

  • Loading branch information...
1 parent a6f3ebe commit aa3010af915e71b091239be6ca761fe5659ce408 @johnbintz johnbintz committed Jun 21, 2011
Showing with 103 additions and 6 deletions.
  1. +7 −6 lib/guard.rb
  2. +26 −0 lib/guard/cli.rb
  3. +4 −0 lib/guard/dsl.rb
  4. +29 −0 lib/guard/dsl_describer.rb
  5. +34 −0 spec/guard/dsl_describer_spec.rb
  6. +3 −0 spec/guard/dsl_spec.rb
View
13 lib/guard.rb
@@ -1,11 +1,12 @@
module Guard
- autoload :UI, 'guard/ui'
- autoload :Dsl, 'guard/dsl'
- autoload :Interactor, 'guard/interactor'
- autoload :Listener, 'guard/listener'
- autoload :Watcher, 'guard/watcher'
- autoload :Notifier, 'guard/notifier'
+ autoload :UI, 'guard/ui'
+ autoload :Dsl, 'guard/dsl'
+ autoload :DslDescriber, 'guard/dsl_describer'
+ autoload :Interactor, 'guard/interactor'
+ autoload :Listener, 'guard/listener'
+ autoload :Watcher, 'guard/watcher'
+ autoload :Notifier, 'guard/notifier'
class << self
attr_accessor :options, :guards, :listener
View
26 lib/guard/cli.rb
@@ -37,5 +37,31 @@ def init(guard_name = nil)
end
end
+ desc "show", "Show all defined Guards and their options"
+ def show
+ ::Guard::DslDescriber.evaluate_guardfile(options)
+
+ ::Guard::DslDescriber.guardfile_structure.each do |group|
+ if !group[:guards].empty?
+ if group[:group]
+ puts "Group #{group[:group]}:"
+ else
+ puts "(Global):"
+ end
+
+ group[:guards].each do |guard|
+ print " #{guard[:name]}"
+
+ if !guard[:options].empty?
+ print ": #{guard[:options].collect { |k, v| "#{k} => #{v}" }.join(", ")}"
+ end
+ puts
+ end
+ end
+ end
+
+ puts
+ end
+ map %w(-T) => :show
end
end
View
4 lib/guard/dsl.rb
@@ -77,6 +77,10 @@ def guardfile_default_path
File.exist?(local_guardfile_path) ? local_guardfile_path : home_guardfile_path
end
+ def guardfile_structure
+
+ end
+
private
def local_guardfile_path
View
29 lib/guard/dsl_describer.rb
@@ -0,0 +1,29 @@
+require 'guard/dsl'
+
+module Guard
+ class DslDescriber < Dsl
+ @@guardfile_structure = [ { :guards => [] } ]
+
+ class << self
+ def guardfile_structure
+ @@guardfile_structure
+ end
+ end
+
+ private
+ def group(name, &guard_definition)
+ @@guardfile_structure << { :group => name.to_sym, :guards => [] }
+
+ @group = true
+ guard_definition.call
+ @group = false
+ end
+
+ def guard(name, options = {}, &watch_definition)
+ node = (@group ? @@guardfile_structure.last : @@guardfile_structure.first)
+
+ node[:guards] << { :name => name, :options => options }
+ end
+ end
+end
+
View
34 spec/guard/dsl_describer_spec.rb
@@ -0,0 +1,34 @@
+require 'spec_helper'
+
+describe Guard::DslDescriber do
+ subject { described_class }
+
+ it 'should evaluate a Guardfile and create the right structure' do
+ mixed_guardfile_string = <<-GUARD
+guard 'test', :a => :b do
+ watch('c')
+end
+
+group :a do
+ guard 'test' do
+ watch('c')
+ end
+end
+
+group "b" do
+ guard 'another' do
+ watch('c')
+ end
+end
+GUARD
+
+ subject.evaluate_guardfile(:guardfile_contents => mixed_guardfile_string)
+
+ subject.guardfile_structure.should == [
+ { :guards => [ { :name => 'test', :options => { :a => :b } } ] },
+ { :group => :a, :guards => [ { :name => 'test', :options => {} } ] },
+ { :group => :b, :guards => [ { :name => 'another', :options => {} } ] }
+ ]
+
+ end
+end
View
3 spec/guard/dsl_spec.rb
@@ -127,6 +127,9 @@
lambda { subject.evaluate_guardfile(:guardfile_contents => invalid_guardfile_string ) }.should raise_error
end
+ it "should generate a displayable structure from the Guardfile data" do
+ end
+
describe ".guardfile_default_path" do
let(:local_path) { File.join(Dir.pwd, 'Guardfile') }
let(:user_path) { File.expand_path(File.join("~", 'Guardfile')) }

0 comments on commit aa3010a

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