file-monitor 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +28 -1
- data/examples/use-filter.rb +27 -0
- data/lib/file-monitor.rb +79 -30
- metadata +4 -3
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Ruby File Monitor
|
2
2
|
=================
|
3
3
|
|
4
|
-
Ruby File Monitor is a easy way to watch the directories and files, do anything when them changed. It's base on [rb-inotify](https://github.com/nex3/rb-inotify), So it only works in inotify supported system such as Linux.
|
4
|
+
Ruby File Monitor is a easy way to watch the directories and files, do anything when them changed. Use for situation like Auto refresh webpage, Auto execute unit tests, Auto compile CoffeeScript, Haml and so on. It's base on [rb-inotify](https://github.com/nex3/rb-inotify), So it only works in inotify supported system such as Linux.
|
5
5
|
|
6
6
|
Requirements
|
7
7
|
------------
|
@@ -29,6 +29,9 @@ Features
|
|
29
29
|
|
30
30
|
To avoid run the check methods too quickly, for example, when delete 20 files at the same time, if without Events Buffer will run the check methods 20 times. the frequency of file-monitor is 0.2 second
|
31
31
|
|
32
|
+
5. High Performance
|
33
|
+
|
34
|
+
For Ruby File Monitor use inotify library, so it's very fast, even watching thousands of directories.
|
32
35
|
|
33
36
|
Installation
|
34
37
|
------------
|
@@ -91,6 +94,30 @@ Usage
|
|
91
94
|
|
92
95
|
If block exists, the check method will be ignored.
|
93
96
|
|
97
|
+
### Filter mode
|
98
|
+
|
99
|
+
#!/usr/bin/env ruby
|
100
|
+
# coding: utf-8
|
101
|
+
# File: examples/use-filter.rb
|
102
|
+
|
103
|
+
require 'file-monitor.rb'
|
104
|
+
|
105
|
+
m = FileMonitor.new('.')
|
106
|
+
m.filter_dirs {
|
107
|
+
disallow /\.git|\.svn/
|
108
|
+
}
|
109
|
+
|
110
|
+
# record .rb files only
|
111
|
+
m.filter_files {
|
112
|
+
disallow /.*/
|
113
|
+
allow /\.rb$/
|
114
|
+
}
|
115
|
+
|
116
|
+
m.run do|events|
|
117
|
+
puts events.size()
|
118
|
+
puts "do something"
|
119
|
+
end
|
120
|
+
|
94
121
|
Examples
|
95
122
|
--------
|
96
123
|
### Auto F5
|
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
# File: examples/use-filter.rb
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
|
7
|
+
lib_dir = File.join File.dirname(__FILE__), '../lib'
|
8
|
+
$:.unshift lib_dir unless $:.include? lib_dir
|
9
|
+
|
10
|
+
require 'file-monitor.rb'
|
11
|
+
|
12
|
+
m = FileMonitor.new('.')
|
13
|
+
m.filter_dirs {
|
14
|
+
disallow /\.git|\.svn/
|
15
|
+
}
|
16
|
+
|
17
|
+
# record .rb files only
|
18
|
+
m.filter_files {
|
19
|
+
disallow /.*/
|
20
|
+
allow /\.rb$/
|
21
|
+
}
|
22
|
+
|
23
|
+
m.run do|events|
|
24
|
+
puts events.size()
|
25
|
+
puts "do something"
|
26
|
+
end
|
27
|
+
|
data/lib/file-monitor.rb
CHANGED
@@ -3,44 +3,92 @@
|
|
3
3
|
|
4
4
|
require 'rb-inotify'
|
5
5
|
|
6
|
-
class
|
7
|
-
|
8
|
-
|
6
|
+
class FileMonitorFilter
|
7
|
+
def initialize
|
8
|
+
@patterns = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def ignored?(path)
|
12
|
+
status = :allow
|
13
|
+
for mode, pattern in @patterns
|
14
|
+
if path =~ pattern
|
15
|
+
status = mode
|
16
|
+
end
|
17
|
+
end
|
18
|
+
return status == :disallow
|
19
|
+
end
|
20
|
+
|
21
|
+
alias_method :disallow?, :ignored?
|
22
|
+
|
23
|
+
def disallow(pattern)
|
24
|
+
@patterns << [:disallow, pattern]
|
25
|
+
end
|
26
|
+
|
27
|
+
def allow(pattern)
|
28
|
+
@patterns << [:allow, pattern]
|
29
|
+
end
|
30
|
+
|
31
|
+
def reset
|
32
|
+
@patterns = []
|
33
|
+
end
|
34
|
+
end
|
9
35
|
|
36
|
+
class FileMonitor
|
10
37
|
# do the action every @frequency second, to avoid check too frequently
|
11
38
|
attr_accessor :frequency
|
12
39
|
|
13
|
-
def initialize(project_dir)
|
40
|
+
def initialize(project_dir = '.')
|
14
41
|
@notifier = INotify::Notifier.new
|
15
42
|
@project_dir = project_dir
|
16
43
|
|
17
44
|
@events = []
|
18
45
|
@ignores = []
|
19
46
|
@frequency = 0.2
|
47
|
+
|
48
|
+
@filters = {
|
49
|
+
:files => FileMonitorFilter.new,
|
50
|
+
:dirs => FileMonitorFilter.new
|
51
|
+
}
|
20
52
|
end
|
21
53
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
54
|
+
# Compatible to old ignored_dirs and ignored_files mode
|
55
|
+
def ignore_dirs(path)
|
56
|
+
self.ignored_dirs= path
|
57
|
+
end
|
58
|
+
|
59
|
+
def ignore_files(pattern)
|
60
|
+
self.ignored_files = pattern
|
61
|
+
end
|
62
|
+
|
63
|
+
def ignored_dirs=(pattern)
|
64
|
+
@filters[:dirs].disallow pattern
|
65
|
+
end
|
66
|
+
|
67
|
+
def ignored_files=(pattern)
|
68
|
+
@filters[:files].disallow pattern
|
69
|
+
end
|
70
|
+
|
71
|
+
# New Filter mode
|
72
|
+
def filter(type, &block)
|
73
|
+
@filters[type].instance_eval &block
|
74
|
+
end
|
75
|
+
|
76
|
+
def filter_files(&block)
|
77
|
+
filter :files, &block
|
78
|
+
end
|
79
|
+
|
80
|
+
def filter_dirs(&block)
|
81
|
+
filter :dirs, &block
|
29
82
|
end
|
30
83
|
|
31
84
|
def ignored_dir?(path)
|
32
|
-
ignored? path
|
85
|
+
@filters[:dirs].ignored? path
|
33
86
|
end
|
34
87
|
|
35
88
|
def ignored_file?(path)
|
36
|
-
ignored? path
|
89
|
+
@filters[:files].ignored? path
|
37
90
|
end
|
38
91
|
|
39
|
-
# TODO combine events maybe it's not nesscary
|
40
|
-
# moved_from + moved_to = rename
|
41
|
-
# create + delete = delete
|
42
|
-
# delete + create = modify
|
43
|
-
# rename + delete = delete
|
44
92
|
def push_event event
|
45
93
|
@events << event
|
46
94
|
end
|
@@ -61,22 +109,23 @@ class FileMonitor
|
|
61
109
|
# + created or moved_to
|
62
110
|
# - deleted or moved_from
|
63
111
|
# # modified
|
112
|
+
# 'stop watching' ignored
|
64
113
|
info = ''
|
65
|
-
|
114
|
+
flag = flags[0]
|
115
|
+
flag = :moved_from if flags.include? :moved_from
|
116
|
+
flag = :moved_to if flags.include? :moved_to
|
117
|
+
|
118
|
+
case flag
|
119
|
+
when :moved_from, :delete
|
66
120
|
info += '-'
|
67
|
-
|
121
|
+
when :moved_to, :create
|
68
122
|
info += '+'
|
123
|
+
when :modify
|
124
|
+
info += '#'
|
125
|
+
when :ignored
|
126
|
+
info += 'stop watching'
|
69
127
|
else
|
70
|
-
|
71
|
-
when :create
|
72
|
-
info += '+'
|
73
|
-
when :modify
|
74
|
-
info += '#'
|
75
|
-
when :delete
|
76
|
-
info += '-'
|
77
|
-
when :ignored
|
78
|
-
info += 'stop watching'
|
79
|
-
end
|
128
|
+
info += 'unknown ' + flags.to_s
|
80
129
|
end
|
81
130
|
|
82
131
|
if ignored_file?(event.absolute_name)
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- JiangMiao
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-10-
|
17
|
+
date: 2011-10-16 00:00:00 +08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -59,6 +59,7 @@ files:
|
|
59
59
|
- lib/file-monitor.rb
|
60
60
|
- examples/use-block.rb
|
61
61
|
- examples/use-inherit.rb
|
62
|
+
- examples/use-filter.rb
|
62
63
|
- examples/f5.rb
|
63
64
|
- README.md
|
64
65
|
has_rdoc: true
|