Skip to content

Commit

Permalink
Merge pull request #60 from stereobooster/master
Browse files Browse the repository at this point in the history
Windows support!
  • Loading branch information
Rémy Coutable committed May 7, 2011
2 parents 5352528 + 29069cd commit d7b0876
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ if Config::CONFIG['target_os'] =~ /linux/i
gem 'libnotify', '~> 0.1.3', :require => false gem 'libnotify', '~> 0.1.3', :require => false
end end
if Config::CONFIG['target_os'] =~ /mswin|mingw/i if Config::CONFIG['target_os'] =~ /mswin|mingw/i
gem 'win32console' gem 'win32console', :require => false
gem 'rb-fchange', '>= 0.0.2', :require => false
end end
8 changes: 8 additions & 0 deletions README.markdown
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Features


* [FSEvent](http://en.wikipedia.org/wiki/FSEvents) support on Mac OS X 10.5+ (without RubyCocoa!, [rb-fsevent gem, >= 0.3.5](https://rubygems.org/gems/rb-fsevent) required). * [FSEvent](http://en.wikipedia.org/wiki/FSEvents) support on Mac OS X 10.5+ (without RubyCocoa!, [rb-fsevent gem, >= 0.3.5](https://rubygems.org/gems/rb-fsevent) required).
* [Inotify](http://en.wikipedia.org/wiki/Inotify) support on Linux ([rb-inotify gem, >= 0.5.1](https://rubygems.org/gems/rb-inotify) required). * [Inotify](http://en.wikipedia.org/wiki/Inotify) support on Linux ([rb-inotify gem, >= 0.5.1](https://rubygems.org/gems/rb-inotify) required).
* [Directory Change Notification](http://msdn.microsoft.com/en-us/library/aa365261\(VS.85\).aspx) suuport on Windows ([rb-fchange, >= 0.0.2](https://rubygems.org/gems/rb-fchange) required)
* Polling on the other operating systems (help us to support more OS). * Polling on the other operating systems (help us to support more OS).
* Automatic & Super fast (when polling is not used) files modifications detection (even new files are detected). * Automatic & Super fast (when polling is not used) files modifications detection (even new files are detected).
* Growl notifications ([growlnotify](http://growl.info/documentation/growlnotify.php) & [growl gem](https://rubygems.org/gems/growl) required). * Growl notifications ([growlnotify](http://growl.info/documentation/growlnotify.php) & [growl gem](https://rubygems.org/gems/growl) required).
Expand Down Expand Up @@ -67,6 +68,13 @@ And add it to you Gemfile:
gem 'libnotify' gem 'libnotify'
``` ```


### On Windows

Install the rb-fchange gem for [Directory Change Notification](http://msdn.microsoft.com/en-us/library/aa365261\(VS.85\).aspx) support:

$ gem install rb-fchange


Usage Usage
----- -----


Expand Down
7 changes: 7 additions & 0 deletions lib/guard/listener.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Guard


autoload :Darwin, 'guard/listeners/darwin' autoload :Darwin, 'guard/listeners/darwin'
autoload :Linux, 'guard/listeners/linux' autoload :Linux, 'guard/listeners/linux'
autoload :Windows, 'guard/listeners/windows'
autoload :Polling, 'guard/listeners/polling' autoload :Polling, 'guard/listeners/polling'


class Listener class Listener
Expand All @@ -14,6 +15,8 @@ def self.select_and_init
Darwin.new Darwin.new
elsif linux? && Linux.usable? elsif linux? && Linux.usable?
Linux.new Linux.new
elsif windows? && Windows.usable?
Windows.new
else else
UI.info "Using polling (Please help us to support your system better than that.)" UI.info "Using polling (Please help us to support your system better than that.)"
Polling.new Polling.new
Expand Down Expand Up @@ -54,6 +57,10 @@ def self.linux?
Config::CONFIG['target_os'] =~ /linux/i Config::CONFIG['target_os'] =~ /linux/i
end end


def self.windows?
Config::CONFIG['target_os'] =~ /mswin|mingw/i
end

end end
end end


Expand Down
36 changes: 36 additions & 0 deletions lib/guard/listeners/windows.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,36 @@
module Guard
class Windows < Listener
attr_reader :fchange

def initialize
super
@fchange = FChange::Notifier.new
end

def on_change(&callback)
@fchange.watch(Dir.pwd, :all_events, :recursive) do |event|
paths = [File.expand_path(event.watcher.path) + '/']
files = modified_files(paths, {:all => true})
update_last_event
callback.call(files)
end
end

def start
@fchange.run
end

def stop
@fchange.stop
end

def self.usable?
require 'rb-fchange'
true
rescue LoadError
UI.info "Please install rb-fchange gem for Windows file events support"
false
end

end
end
8 changes: 4 additions & 4 deletions spec/guard/listener_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
subject.select_and_init subject.select_and_init
end end


it "uses polling listener on Windows" do it "uses windows listener on Windows" do
Config::CONFIG['target_os'] = 'win32' Config::CONFIG['target_os'] = 'mingw'
Guard::Polling.stub(:usable?).and_return(true) Guard::Windows.stub(:usable?).and_return(true)
Guard::Polling.should_receive(:new) Guard::Windows.should_receive(:new)
subject.select_and_init subject.select_and_init
end end


Expand Down
88 changes: 88 additions & 0 deletions spec/guard/listeners/windows_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,88 @@
require 'spec_helper'
require 'guard/listeners/windows'

describe Guard::Windows do
subject { Guard::Windows }

if linux?
it "isn't usable on linux" do
subject.should_not be_usable
end
end

if mac?
it "isn't usable on Mac" do
subject.should_not be_usable
end
end

if windows?
it "is usable on Windows 2000 and later" do
subject.should be_usable
end

describe "#on_change" do
before(:each) do
@results = []
@listener = Guard::Windows.new
@listener.on_change do |files|
@results += files
end
end

it "catches new file" do
file = @fixture_path.join("newfile.rb")
if File.exists?(file)
begin
File.delete file
rescue
end
end
File.exists?(file).should be_false
start
FileUtils.touch file
stop
begin
File.delete file
rescue
end
@results.should == ['spec/fixtures/newfile.rb']
end

it "catches file update" do
file = @fixture_path.join("folder1/file1.txt")
File.exists?(file).should be_true
start
FileUtils.touch file
stop
@results.should == ['spec/fixtures/folder1/file1.txt']
end

it "catches files update" do
file1 = @fixture_path.join("folder1/file1.txt")
file2 = @fixture_path.join("folder1/folder2/file2.txt")
File.exists?(file1).should be_true
File.exists?(file2).should be_true
start
FileUtils.touch file1
FileUtils.touch file2
stop
@results.should == ['spec/fixtures/folder1/file1.txt', 'spec/fixtures/folder1/folder2/file2.txt']
end
end
end

private

def start
sleep 0.6
Thread.new { @listener.start }
sleep 0.6
end

def stop
sleep 0.6
@listener.stop
end

end
4 changes: 4 additions & 0 deletions spec/support/platform_helper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ def mac?


def linux? def linux?
Config::CONFIG['target_os'] =~ /linux/i Config::CONFIG['target_os'] =~ /linux/i
end

def windows?
Config::CONFIG['target_os'] =~ /mswin|mingw/i
end end

0 comments on commit d7b0876

Please sign in to comment.