Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Restructure and add portable notifier

  • Loading branch information...
commit ca7f624f06f4e02d4a8023443325bef60ee64465 1 parent 0d432c6
@wycats authored
View
3  lib/vigilo.rb
@@ -1,7 +1,8 @@
module Vigilo
class Watcher
def self.watch(*directories, &block)
- watcher = new(Vigilo::Adapters::FSEvents.new, *directories)
+ listener = ENV["VIGILO_ADAPTER"] ? Vigilo::Adapters.const_get(ENV["VIGILO_ADAPTER"]) : Vigilo::Adapters::Portable
+ watcher = new(Vigilo::Adapter.new(listener), *directories)
watcher.start(&block)
end
View
81 lib/vigilo/adapters/base.rb
@@ -0,0 +1,81 @@
+module Vigilo
+ class Adapter
+ def initialize(listener)
+ @listener = listener.new
+ end
+
+ def watch(directories, &block)
+ @listener.callback(&block)
+ @listener.directories directories
+ @listener.start
+ end
+ end
+
+ module Adapters
+ module Listener
+ def callback(&block)
+ @block = block
+ end
+
+ def directories(directories)
+ @files = Hash.new {|h,k| h[k] = {} }
+ @directories = directories
+
+ directories.each do |directory|
+ Dir.glob("#{directory}/**/*").each do |file|
+ insert(directory, file)
+ end
+ end
+ end
+
+ CHANGES = {:changed => [], :added => [], :removed => []}
+
+ def on_change(directories)
+ changes = {:changed => [], :added => [], :removed => []}
+
+ directories.each do |dir|
+ base_dir = dir.sub(/\/$/, '')
+ diff(base_dir, changes)
+
+ Dir["#{base_dir}/*"].each do |file|
+ next if file =~ /\/\./
+ next unless File.file?(file)
+ next if @files[base_dir][File.basename(file)]
+
+ changes[:added].push file
+ insert(base_dir, file)
+ end
+ end
+
+ @block.call(changes) unless changes == CHANGES
+ end
+
+ def diff(base, changed_files)
+ @files[base].each do |file, stat|
+ fullname = File.join(base, file)
+
+ if File.exist?(fullname)
+ new_stat = File.stat(fullname)
+ if stat.mtime != new_stat.mtime
+ changed_files[:changed] << fullname
+ @files[base][file] = new_stat
+ end
+ else
+ @files[base].delete(file)
+ changed_files[:removed] << fullname
+ end
+ end
+
+ changed_files
+ end
+
+ def insert(directory, file)
+ if File.file?(file)
+ @files[File.dirname(file)][File.basename(file)] = File.stat(file)
+ end
+ end
+ end
+ end
+end
+
+
View
83 lib/vigilo/adapters/fsevents.rb
@@ -1,85 +1,14 @@
require "fsevent"
+require "vigilo/adapters/base"
module Vigilo
module Adapters
- class FSEvents
- class Listener < FSEvent
- def callback(&block)
- @block = block
- end
+ class FSEvents < ::FSEvent
+ include Listener
- def directories(directories)
- @files = Hash.new {|h,k| h[k] = {} }
-
- directories.each do |directory|
- Dir.glob("#{directory}/**/*", File::FNM_DOTMATCH).each do |file|
- insert(directory, file)
- end
- end
- end
-
- def on_change(directories)
- changes = {:changed => [], :added => [], :removed => []}
-
- directories.each do |dir|
- base_dir = dir.sub(/\/$/, '')
- diff(base_dir, changes)
-
- Dir["#{base_dir}/*"].each do |file|
- next unless File.file?(file)
- next if @files[base_dir][File.basename(file)]
-
- changes[:added].push file
- insert(base_dir, file)
- end
- end
-
- @block.call(changes)
- end
-
- def diff(base, changed_files)
- @files[base].each do |file, stat|
- fullname = File.join(base, file)
-
- if File.exist?(fullname)
- new_stat = File.stat(fullname)
- if stat.mtime != new_stat.mtime
- changed_files[:changed] << fullname
- @files[base][file] = new_stat
- end
- else
- @files[base].delete(file)
- changed_files[:removed] << fullname
- end
- end
-
- changed_files
- end
-
- def insert(directory, file)
- if File.file?(file)
- @files[File.dirname(file)][File.basename(file)] = File.stat(file)
- end
- end
- end
-
- def initialize
- @listener = Listener.new
- end
-
- def watch(directories, &block)
- @listener.directories directories
- @listener.callback(&block)
- @listener.watch_directories(directories)
- puts "READY"
- @listener.start
- end
-
- private
- def insert(directory, file)
- if File.file?(file)
- @files[File.dirname(file)][File.basename(file)] = File.stat(file)
- end
+ def directories(directories)
+ super
+ watch_directories(directories)
end
end
end
View
24 lib/vigilo/adapters/portable.rb
@@ -0,0 +1,24 @@
+require "vigilo/adapters/base"
+
+module Vigilo
+ module Adapters
+ class Portable
+ include Listener
+
+ def start
+ loop do
+ directories = []
+ @directories.each do |directory|
+ Dir.glob("#{directory}/**/*").each do |file|
+ directories << file if File.directory?(file)
+ end
+ end
+ directories.concat(@directories)
+ on_change(directories)
+ sleep 1
+ end
+ end
+ end
+ end
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.