diff --git a/fsevents_to_vm.gemspec b/fsevents_to_vm.gemspec index b55eaf9..410a6c7 100644 --- a/fsevents_to_vm.gemspec +++ b/fsevents_to_vm.gemspec @@ -19,6 +19,7 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] spec.add_dependency "rb-fsevent", "~> 0.9.5" + spec.add_dependency "rb-inotify", "~> 0.9.5" spec.add_dependency "thor", "~> 0.19.1" spec.add_dependency "net-ssh", "~> 2.9.2" diff --git a/lib/fsevents_to_vm.rb b/lib/fsevents_to_vm.rb index 1ce4482..9835572 100644 --- a/lib/fsevents_to_vm.rb +++ b/lib/fsevents_to_vm.rb @@ -1,6 +1,7 @@ require "fsevents_to_vm/version" require 'fsevents_to_vm/event' +require 'fsevents_to_vm/fs_event_proxy' require 'fsevents_to_vm/watch' require 'fsevents_to_vm/path_filter' require 'fsevents_to_vm/recursion_filter' diff --git a/lib/fsevents_to_vm/fs_event_proxy.rb b/lib/fsevents_to_vm/fs_event_proxy.rb new file mode 100644 index 0000000..99fed0f --- /dev/null +++ b/lib/fsevents_to_vm/fs_event_proxy.rb @@ -0,0 +1,52 @@ +def is_mac? + RUBY_PLATFORM.downcase.include?("darwin") +end + +def is_linux? + RUBY_PLATFORM.downcase.include?("linux") +end + +require 'rb-inotify' if is_linux? +require 'rb-fsevent' if is_mac? + +module FseventsToVm + class FsEventProxy + + attr_reader :notifier + + # + # Accepts the ablosute_path which the notifier + # will monitor + # + def initialize(absolute_path) + @path = absolute_path + @notifier = INotify::Notifier.new if is_linux? + @notifier = FSEvent.new if is_mac? + end + + # + # Accepts a Proc object containing the code + # to be executed when a notifier event occurs + # + def watch(&block) + if is_mac? + @notifier.watch([@path], file_events: true) do |event| + block.call(event) + end + elsif is_linux? + @notifier.watch(@path, :recursive, :attrib, :modify, :move, :create, :delete) do |event| + block.call([event.absolute_name]) + end + end + puts "Watching #{@path}" + end + + def run + @notifier.run + end + + def stop + @notifier.stop + end + end +end diff --git a/lib/fsevents_to_vm/watch.rb b/lib/fsevents_to_vm/watch.rb index 5f81f05..b5c3639 100644 --- a/lib/fsevents_to_vm/watch.rb +++ b/lib/fsevents_to_vm/watch.rb @@ -1,14 +1,12 @@ -require 'rb-fsevent' - module FseventsToVm class Watch - def initialize(*listen_dirs) - @listen_dirs = listen_dirs - @fs = FSEvent.new + def initialize(listen_dir) + @listen_dir = listen_dir + @fs = FsEventProxy.new(@listen_dir) end def run - @fs.watch(@listen_dirs, file_events: true) do |files| + @fs.watch do |files| files.each do |file| event = build_event(file) yield event if event