diff --git a/Gemfile b/Gemfile index a7126b6..32f6d47 100644 --- a/Gemfile +++ b/Gemfile @@ -4,11 +4,10 @@ gemspec require 'rbconfig' -if Config::CONFIG['target_os'] =~ /darwin/i +if Config::CONFIG['host_os'] =~ /darwin/i gem 'rb-fsevent', '>= 0.3.9' - gem 'growl', '~> 1.0.3' -end -if Config::CONFIG['target_os'] =~ /linux/i + gem 'growl', '~> 1.0' +elsif Config::CONFIG['host_os'] =~ /linux/i gem 'rb-inotify', '>= 0.5.1' - gem 'libnotify', '~> 0.1.3' + gem 'libnotify', '~> 0.1' end diff --git a/Guardfile b/Guardfile index c7303d5..117d61c 100644 --- a/Guardfile +++ b/Guardfile @@ -1,5 +1,7 @@ +ENV["GUARD_ENV"] = 'development' + guard('rspec', :cli => '-f doc', :version => 2) do watch(%r{^spec/(.*)_spec\.rb}) watch(%r{^lib/(.*)\.rb}) { |m| "spec/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { "spec" } -end \ No newline at end of file +end diff --git a/Rakefile b/Rakefile index 4541498..04717e7 100644 --- a/Rakefile +++ b/Rakefile @@ -8,14 +8,14 @@ task :default => :spec namespace(:spec) do desc "Run all specs on multiple ruby versions (requires rvm)" task(:portability) do - %w[1.8.6 1.8.7 1.9.2].each do |version| + %w[1.8.6 1.8.7 1.9.2 ree].each do |version| system <<-BASH bash -c 'source ~/.rvm/scripts/rvm; rvm #{version}; echo "--------- version #{version} ----------\n"; bundle install; - rake spec' + rake spec;' BASH end end -end \ No newline at end of file +end diff --git a/lib/guard/cli.rb b/lib/guard/cli.rb index c3e4526..a80211d 100644 --- a/lib/guard/cli.rb +++ b/lib/guard/cli.rb @@ -38,4 +38,4 @@ module Guard end end -end \ No newline at end of file +end diff --git a/lib/guard/dsl.rb b/lib/guard/dsl.rb index aee322d..dd1251d 100644 --- a/lib/guard/dsl.rb +++ b/lib/guard/dsl.rb @@ -43,11 +43,7 @@ module Guard end def callback(*args, &listener) - listener, events = if args.size > 1 - args - else - [listener, args[0]] - end + listener, events = args.size > 1 ? args : [listener, args[0]] @callbacks << { :events => events, :listener => listener } end diff --git a/lib/guard/guard.rb b/lib/guard/guard.rb index e952fe3..0b52f62 100644 --- a/lib/guard/guard.rb +++ b/lib/guard/guard.rb @@ -54,4 +54,4 @@ module Guard end end -end \ No newline at end of file +end diff --git a/lib/guard/hook.rb b/lib/guard/hook.rb index 950e7f5..88f01ba 100644 --- a/lib/guard/hook.rb +++ b/lib/guard/hook.rb @@ -1,14 +1,32 @@ module Guard module Hook + def self.included(base) base.send :include, InstanceMethods end module InstanceMethods - # When passed a sybmol, #hook will generate a hook name - # from the symbol and calling method name. When passed - # a string, #hook will turn the string into a symbol - # directly. + # When +event+ is a Symbol, #hook will generate a hook name + # by concatenating the method name from where #hook is called + # with the given Symbol. + # Example: + # def run_all + # hook :foo + # end + # Here, when #run_all is called, #hook will notify callbacks + # registered for the "run_all_foo" event. + # + # When +event+ is a String, #hook will directly turn the String + # into a Symbol. + # Example: + # def run_all + # hook "foo_bar" + # end + # Here, when #run_all is called, #hook will notify callbacks + # registered for the "foo_bar" event. + # + # +args+ parameter is passed as is to the callbacks registered + # for the given event. def hook(event, *args) hook_name = if event.is_a? Symbol calling_method = caller[0][/`([^']*)'/, 1] @@ -51,5 +69,6 @@ module Guard @callbacks = nil end end + end end diff --git a/lib/guard/interactor.rb b/lib/guard/interactor.rb index f5247eb..e65346c 100644 --- a/lib/guard/interactor.rb +++ b/lib/guard/interactor.rb @@ -1,6 +1,6 @@ module Guard module Interactor - + def self.init_signal_traps # Run all (Ctrl-\) Signal.trap('QUIT') do @@ -8,7 +8,7 @@ module Guard ::Guard.guards.each { |guard| ::Guard.supervised_task(guard, :run_all) } end end - + # Stop (Ctrl-C) Signal.trap('INT') do UI.info "Bye bye...", :reset => true @@ -16,7 +16,7 @@ module Guard ::Guard.guards.each { |guard| ::Guard.supervised_task(guard, :stop) } abort("\n") end - + # Reload (Ctrl-Z) Signal.trap('TSTP') do ::Guard.run do @@ -24,6 +24,6 @@ module Guard end end end - + end -end \ No newline at end of file +end diff --git a/lib/guard/listener.rb b/lib/guard/listener.rb index 3675489..5289be9 100644 --- a/lib/guard/listener.rb +++ b/lib/guard/listener.rb @@ -121,4 +121,4 @@ end # end # # end -# end \ No newline at end of file +# end diff --git a/lib/guard/notifier.rb b/lib/guard/notifier.rb index d250a22..cabb857 100644 --- a/lib/guard/notifier.rb +++ b/lib/guard/notifier.rb @@ -63,4 +63,4 @@ module Guard end end -end \ No newline at end of file +end diff --git a/lib/guard/ui.rb b/lib/guard/ui.rb index 94953db..d71ee8d 100644 --- a/lib/guard/ui.rb +++ b/lib/guard/ui.rb @@ -2,21 +2,21 @@ module Guard module UI class << self - def info(message, options = {}) + def info(message, options={}) unless ENV["GUARD_ENV"] == "test" reset_line if options[:reset] puts reset_color(message) if message != '' end end - def error(message, options = {}) + def error(message, options={}) unless ENV["GUARD_ENV"] == "test" reset_line if options[:reset] puts "ERROR: #{message}" end end - def debug(message, options = {}) + def debug(message, options={}) unless ENV["GUARD_ENV"] == "test" reset_line if options[:reset] puts "DEBUG: #{message}" if ::Guard.options && ::Guard.options[:debug] diff --git a/lib/guard/watcher.rb b/lib/guard/watcher.rb index fc16b23..a4292a0 100644 --- a/lib/guard/watcher.rb +++ b/lib/guard/watcher.rb @@ -1,11 +1,11 @@ module Guard class Watcher attr_accessor :pattern, :action - + def initialize(pattern, action = nil) @pattern, @action = pattern, action @@warning_printed ||= false - + # deprecation warning if @pattern.is_a?(String) && @pattern =~ /(^(\^))|(>?(\\\.)|(\.\*))|(\(.*\))|(\[.*\])|(\$$)/ unless @@warning_printed @@ -17,7 +17,7 @@ module Guard @pattern = Regexp.new(@pattern) end end - + def self.match_files(guard, files) guard.watchers.inject([]) do |paths, watcher| files.each do |file| @@ -33,7 +33,7 @@ module Guard paths.flatten.map { |p| p.to_s } end end - + def self.match_files?(guards, files) guards.any? do |guard| guard.watchers.any? do |watcher| @@ -41,7 +41,7 @@ module Guard end end end - + def match_file?(file) if @pattern.is_a?(Regexp) file.match(@pattern) @@ -49,7 +49,7 @@ module Guard file == @pattern ? [file] : nil end end - + def call_action(matches) begin @action.arity > 0 ? @action.call(matches) : @action.call @@ -57,6 +57,6 @@ module Guard UI.error "Problem with watch action!" end end - + end -end \ No newline at end of file +end diff --git a/spec/guard/dsl_spec.rb b/spec/guard/dsl_spec.rb index 625b037..ba626f4 100644 --- a/spec/guard/dsl_spec.rb +++ b/spec/guard/dsl_spec.rb @@ -1,8 +1,11 @@ require 'spec_helper' +require 'guard/guard' describe Guard::Dsl do subject { Guard::Dsl } + class Guard::Dummy < Guard::Guard; end + before(:each) do ::Guard.stub!(:add_guard) end @@ -114,20 +117,23 @@ describe Guard::Dsl do describe "#callback" do it "creates callbacks for the guard" do class MyCustomCallback + def self.call(guard_class, event, args) + # do nothing + end end - mock_guardfile_content(" - guard 'test' do - callback(:start_end) { 'Guard::Test started!' } + mock_guardfile_content(' + guard :dummy do + callback(:start_end) { |guard_class, event, args| "#{guard_class} executed \'#{event}\' hook with #{args}!" } callback(MyCustomCallback, [:start_begin, :run_all_begin]) - end") + end') - ::Guard.should_receive(:add_guard).with('test', anything, anything, {}) do |name, watchers, callbacks, options| + ::Guard.should_receive(:add_guard).with(:dummy, anything, anything, {}) do |name, watchers, callbacks, options| callbacks.should have(2).items - callbacks[0][:events].should == :start_end - callbacks[0][:listener].call.should == proc { 'Guard::Test started!' }.call - callbacks[1][:events].should == [:start_begin, :run_all_begin] - callbacks[1][:listener].should == MyCustomCallback + callbacks[0][:events].should == :start_end + callbacks[0][:listener].call(Guard::Dummy, :start_end, 'foo').should == "Guard::Dummy executed 'start_end' hook with foo!" + callbacks[1][:events].should == [:start_begin, :run_all_begin] + callbacks[1][:listener].should == MyCustomCallback end subject.evaluate_guardfile end diff --git a/spec/guard/notifier_spec.rb b/spec/guard/notifier_spec.rb index b12781b..73aa761 100644 --- a/spec/guard/notifier_spec.rb +++ b/spec/guard/notifier_spec.rb @@ -19,9 +19,8 @@ describe Guard::Notifier do ) subject.notify 'great', :title => 'Guard' end - end - if linux? + elsif linux? require 'libnotify' it "uses Libnotify on Linux" do Libnotify.should_receive(:show).with( @@ -42,9 +41,8 @@ describe Guard::Notifier do Growl.should_not_receive(:notify) subject.notify 'great', :title => 'Guard' end - end - if linux? + elsif linux? require 'libnotify' it "does nothing" do Libnotify.should_not_receive(:show)