em-dir-watcher 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.1.0
@@ -3,24 +3,22 @@ module EMDirWatcher
3
3
  module Platform
4
4
  module NIX
5
5
 
6
- if FSSM::Support.backend == 'FSEvents'
7
- # monkey-patch to set latency to 0
8
- module FssmFSEventsMonkeyPatch
6
+ # monkey-patch to set latency to 0
7
+ module FssmFSEventsMonkeyPatch
9
8
  def patched_add_handler(handler, preload=true)
10
- @handlers[handler.path.to_s] = handler
9
+ @handlers[handler.path.to_s] = handler
11
10
 
12
- fsevent = Rucola::FSEvents.new(handler.path.to_s, {:latency => 0.0}) do |events|
13
- events.each do |event|
14
- handler.refresh(event.path)
11
+ fsevent = Rucola::FSEvents.new(handler.path.to_s, {:latency => 0.0}) do |events|
12
+ events.each do |event|
13
+ handler.refresh(event.path)
14
+ end
15
15
  end
16
- end
17
16
 
18
- fsevent.create_stream
19
- handler.refresh(nil, true) if preload
20
- fsevent.start
21
- @fsevents << fsevent
17
+ fsevent.create_stream
18
+ handler.refresh(nil, true) if preload
19
+ fsevent.start
20
+ @fsevents << fsevent
22
21
  end
23
- end
24
22
  end
25
23
 
26
24
  class Watcher
@@ -30,8 +28,10 @@ class Watcher
30
28
  class << self
31
29
  def run_fssm_watcher path, globs
32
30
  require 'fssm'
33
- FSSM::Backends::FSEvents.send(:include, FssmFSEventsMonkeyPatch)
34
- FSSM::Backends::FSEvents.send(:alias_method, :add_handler, :patched_add_handler)
31
+ if FSSM::Support.backend == 'FSEvents'
32
+ FSSM::Backends::FSEvents.send(:include, FssmFSEventsMonkeyPatch)
33
+ FSSM::Backends::FSEvents.send(:alias_method, :add_handler, :patched_add_handler)
34
+ end
35
35
  $stdout.sync = true
36
36
  report = proc { |b,r|
37
37
  puts File.join(b, r)
@@ -0,0 +1,29 @@
1
+
2
+ # this is only used when debugging Windows code on a Mac
3
+
4
+ $stdout.sync = true
5
+
6
+ require 'rubygems'
7
+ require 'socket'
8
+ require 'fssm'
9
+
10
+ port = ARGV[0].to_i
11
+ dir = ARGV[1]
12
+
13
+ socket = TCPSocket.open('127.0.0.1', port)
14
+
15
+ report_change = proc do |a, b|
16
+ path = File.join(a, b)
17
+ socket.puts path
18
+ end
19
+
20
+ begin
21
+ FSSM.monitor dir do
22
+ update(&report_change)
23
+ create(&report_change)
24
+ delete(&report_change)
25
+ end
26
+ ensure
27
+ cn.close
28
+ socket.close
29
+ end
@@ -0,0 +1,24 @@
1
+ $stdout.sync = true
2
+
3
+ require 'rubygems'
4
+ require 'socket'
5
+ require 'win32/changenotify'
6
+
7
+ port = ARGV[0].to_i
8
+ dir = ARGV[1]
9
+ CN = Win32::ChangeNotify
10
+ cn = CN.new(dir, true, CN::FILE_NAME | CN::DIR_NAME | CN::LAST_WRITE)
11
+
12
+ socket = TCPSocket.open('127.0.0.1', port)
13
+
14
+ begin
15
+ cn.wait do |events|
16
+ events.each do |event|
17
+ # puts event.file_name
18
+ socket.puts event.file_name
19
+ end
20
+ end
21
+ ensure
22
+ cn.close
23
+ socket.close
24
+ end
@@ -0,0 +1,25 @@
1
+
2
+ require 'win32/api'
3
+
4
+ module EMDirWatcher
5
+ module Platform
6
+ module Windows
7
+
8
+ private
9
+
10
+ GetModuleFileName = Win32::API.new('GetModuleFileName', 'LPL', 'L', 'kernel32')
11
+
12
+ public
13
+
14
+ def self.path_to_ruby_exe
15
+ buf = 0.chr * 260
16
+ len = [buf.length].pack('L')
17
+
18
+ GetModuleFileName.call(0, buf, buf.length)
19
+
20
+ buf.strip
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,86 @@
1
+
2
+ require File.join(File.dirname(__FILE__), 'windows', 'path_to_ruby_exe')
3
+
4
+ module EMDirWatcher
5
+ module Platform
6
+ module Windows
7
+
8
+ module TcpHandler
9
+ attr_accessor :watcher
10
+
11
+ def post_init
12
+ @data_received = ""
13
+ end
14
+
15
+ def receive_data data
16
+ @data_received << data
17
+ while line = @data_received.slice!(/^[^\n]*[\n]/m)
18
+ @watcher.path_changed line.strip
19
+ end
20
+ end
21
+ end
22
+
23
+ class Watcher
24
+
25
+ def initialize path, globs, &handler
26
+ @path = path
27
+ @globs = globs
28
+ @handler = handler
29
+ @active = true
30
+
31
+ start_server
32
+ setup_listener
33
+ end
34
+
35
+ def start_server
36
+ @server = EM.start_server '127.0.0.1', 0, TcpHandler do |server|
37
+ server.watcher = self
38
+ end
39
+ @server_port, _ = Socket.unpack_sockaddr_in(EM::get_sockname @server)
40
+ puts "Server running on port #{@server_port}"
41
+ end
42
+
43
+ def stop_server
44
+ if @server
45
+ EM.stop_server @server
46
+ @server = nil
47
+ end
48
+ end
49
+
50
+ def stop
51
+ @active = false
52
+ kill
53
+ end
54
+
55
+ def kill
56
+ if @io
57
+ Process.kill 'TERM', @io.pid
58
+ Process.waitpid @io.pid
59
+ @io = nil
60
+ end
61
+ end
62
+
63
+ def setup_listener
64
+ path_to_script = File.join(File.dirname(__FILE__), 'windows', 'monitor.rb')
65
+ path_to_ruby_exe = 'ruby'
66
+ @io = IO.popen(path_to_ruby_exe + " " + path_to_script + " #{@server_port} " + File.expand_path(@path))
67
+ end
68
+
69
+ def path_changed path
70
+ @handler.call path
71
+ end
72
+
73
+ def listener_died
74
+ kill # waitpid to cleanup zombie
75
+ if @active
76
+ EM.next_tick do
77
+ setup_listener
78
+ end
79
+ end
80
+ end
81
+
82
+ end
83
+ end
84
+ end
85
+ end
86
+
@@ -3,7 +3,7 @@ require 'eventmachine'
3
3
  require 'rbconfig'
4
4
 
5
5
  module EMDirWatcher
6
- PLATFORM =
6
+ PLATFORM = ENV['EM_DIR_WATCHER_PLATFORM'] ||
7
7
  case Config::CONFIG['target_os']
8
8
  when /mswin|mingw/ then 'Windows'
9
9
  else 'NIX'
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 1
7
8
  - 0
8
- - 4
9
- version: 0.0.4
9
+ version: 0.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Andrey Tarantsov
@@ -37,6 +37,10 @@ files:
37
37
  - examples/monitor.rb
38
38
  - lib/em-dir-watcher.rb
39
39
  - lib/em-dir-watcher/platform/nix.rb
40
+ - lib/em-dir-watcher/platform/windows.rb
41
+ - lib/em-dir-watcher/platform/windows/monitor-nix.rb
42
+ - lib/em-dir-watcher/platform/windows/monitor.rb
43
+ - lib/em-dir-watcher/platform/windows/path_to_ruby_exe.rb
40
44
  - test/helper.rb
41
45
  - test/test_em-dir-watcher.rb
42
46
  has_rdoc: true