file-monitor 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
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. 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.
4
+ Ruby File Monitor is a easy way to watch the directories and files, execute commands 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
  ------------
@@ -36,7 +36,7 @@ Install from source
36
36
  git clone https://github.com/jiangmiao/ruby-file-monitor
37
37
  cd ruby-file-monitor
38
38
  gem build file-monitor.gemspec
39
- gem install file-monitor-0.1.0.gem --user-install
39
+ gem install --local --user-install file-monitor-0.1.3.gem
40
40
 
41
41
  Install from gem server
42
42
 
@@ -45,13 +45,31 @@ Install from gem server
45
45
  Usage
46
46
  -----
47
47
 
48
+ #!/usr/bin/env ruby
49
+ # coding: utf-8
50
+ # File: examples/use-creator.rb
51
+
48
52
  require 'rubygems'
49
53
 
54
+ lib_dir = File.join File.dirname(__FILE__), '../lib'
55
+ $:.unshift lib_dir unless $:.include? lib_dir
56
+
57
+ require 'file-monitor'
58
+
59
+ dir = ARGV[0] || '.'
60
+
50
61
  # watch current working directory
51
- FileMonitor.watch '.' do
62
+ FileMonitor.watch dir do
52
63
 
53
- # do not watch directory end with git or svn
54
- # the last charactor '/' has been trimmed already
64
+ # set frequency 0.2 second (optional default is 0.2)
65
+ # frequency 0.2
66
+
67
+ # do not follow the symlink (optional default is false)
68
+ # follow_symlink false
69
+
70
+
71
+ # do not watch directory contains git and svn
72
+ # the last charactor '/' has been trimmed
55
73
  dirs {
56
74
  disallow /git$|svn$/
57
75
  }
@@ -59,7 +77,7 @@ Usage
59
77
  # record ruby files only
60
78
  # it equals files /\.rb$/
61
79
  files {
62
- disallow //
80
+ disallow /.*/
63
81
  allow /\.rb$/
64
82
  }
65
83
 
@@ -69,7 +87,6 @@ Usage
69
87
  puts events.size()
70
88
  puts "do something"
71
89
  }
72
-
73
90
  end
74
91
 
75
92
  Examples
@@ -0,0 +1,107 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require 'rubygems'
5
+
6
+ lib_dir = File.join File.dirname(__FILE__), '../lib'
7
+ $:.unshift lib_dir unless $:.include? lib_dir
8
+
9
+ require 'file-monitor'
10
+ require 'sinatra'
11
+ require 'getoptlong'
12
+
13
+ class AjaxF5 < FileMonitor
14
+ attr_reader :updated_at, :js
15
+ def initialize(dir)
16
+ super dir
17
+ @updated_at = Time.now.to_f
18
+ # ajax library from http://code.google.com/p/miniajax/
19
+ @js = <<EOT
20
+ (function() {
21
+ var ajax;
22
+ function $(e){if(typeof e=='string')e=document.getElementById(e);return e};
23
+ function collect(a,f){var n=[];for(var i=0;i<a.length;i++){var v=f(a[i]);if(v!=null)n.push(v)}return n};
24
+
25
+ ajax={};
26
+ ajax.x=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}};
27
+ ajax.serialize=function(f){var g=function(n){return f.getElementsByTagName(n)};var nv=function(e){if(e.name)return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);else return ''};var i=collect(g('input'),function(i){if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});var s=collect(g('select'),nv);var t=collect(g('textarea'),nv);return i.concat(s).concat(t).join('&');};
28
+ ajax.send=function(u,f,m,a){var x=ajax.x();x.open(m,u,true);x.onreadystatechange=function(){if(x.readyState==4)f(x.responseText)};if(m=='POST')x.setRequestHeader('Content-type','application/x-www-form-urlencoded');x.send(a)};
29
+ ajax.get=function(url,func){ajax.send(url,func,'GET')};
30
+ var now = Date.now();
31
+ function checkStatus() {
32
+ ajax.get("/f5_status", function(rt) {
33
+ if (now < parseFloat(rt)) {
34
+ location.reload(true);
35
+ }
36
+ setTimeout(checkStatus, 500);
37
+ });
38
+ }
39
+ checkStatus();
40
+ })();
41
+ EOT
42
+ end
43
+
44
+ # simple mark the last updated time
45
+ def check(events)
46
+ @updated_at = Time.now.to_f
47
+ puts "updated at #{@updated_at}"
48
+ end
49
+
50
+ def run
51
+ # use new thread to monitor the file
52
+ Thread.new do
53
+ begin
54
+ super
55
+ rescue
56
+ puts $!.message
57
+ end
58
+ end
59
+
60
+ f5 = self
61
+ set :logging, false
62
+ enable :run
63
+ get '/f5_status' do
64
+ (f5.updated_at*1000).to_s
65
+ end
66
+
67
+ get '/f5.js' do
68
+ f5.js
69
+ end
70
+
71
+ get '*' do
72
+ host = env['SERVER_NAME']
73
+ path = env['REQUEST_PATH']
74
+ begin
75
+ data = open('http://' + host + path) {|f|
76
+ content_type f.meta['content-type']
77
+ f.read
78
+ }
79
+ data.gsub Regexp.new('</body>', Regexp::IGNORECASE), '<script type="text/javascript" src="/f5.js"></script></body>'
80
+ rescue
81
+ $!.message.match /\d+/
82
+ [$&.to_i, {}, '']
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ dir = ARGV[0] || '.'
89
+
90
+ opts = GetoptLong.new(
91
+ [ '--help', '-h', GetoptLong::NO_ARGUMENT]
92
+ )
93
+
94
+ opts.each do|opt, arg|
95
+ case opt
96
+ when '--help'
97
+ puts 'ruby f5.rb [project-directory]'
98
+ exit
99
+ end
100
+ end
101
+
102
+ require 'sinatra'
103
+ require 'open-uri'
104
+ f5 = AjaxF5.new dir
105
+ f5.ignored_dirs = /\.git|\.svn/
106
+ f5.ignored_files = /\.sw.*|\~/
107
+ f5.run
@@ -59,6 +59,7 @@ EOT
59
59
 
60
60
  f5 = self
61
61
  set :logging, false
62
+ enable :run
62
63
  get '/f5_status' do
63
64
  (f5.updated_at*1000).to_s
64
65
  end
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+ # File: examples/use-creator.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'
11
+
12
+ dir = ARGV[0] || '.'
13
+
14
+ # watch current working directory
15
+ FileMonitor.watch dir do
16
+
17
+ # set frequency 0.2 second (optional default is 0.2)
18
+ # frequency 0.2
19
+
20
+ # do not follow the symlink (optional default is false)
21
+ # follow_symlink false
22
+
23
+
24
+ # do not watch directory contains git and svn
25
+ # the last charactor '/' has been trimmed
26
+ dirs {
27
+ disallow /git$|svn$/
28
+ }
29
+
30
+ # record ruby files only
31
+ # it equals files /\.rb$/
32
+ files {
33
+ disallow /.*/
34
+ allow /\.rb$/
35
+ }
36
+
37
+ # The commands will be runned when file changed
38
+ # the events contains all file modified infomation in last 0.2 second
39
+ exec {|events|
40
+ puts events.size()
41
+ puts "do something"
42
+ }
43
+ end
@@ -9,7 +9,9 @@ $:.unshift lib_dir unless $:.include? lib_dir
9
9
 
10
10
  require 'file-monitor.rb'
11
11
 
12
- m = FileMonitor.new('.')
12
+ dir = ARGV[0] || '.'
13
+
14
+ m = FileMonitor.new(dir)
13
15
  m.filter_dirs {
14
16
  disallow /\.git|\.svn/
15
17
  }
@@ -17,6 +17,7 @@ class MyFileMonitor < FileMonitor
17
17
  end
18
18
 
19
19
  dir = ARGV[0] || '.'
20
+
20
21
  m = MyFileMonitor.new(dir)
21
22
  # ignore any dirs end with .git on .svn
22
23
  # The alias of m.filter_dirs {
@@ -2,6 +2,7 @@
2
2
  # coding: utf-8
3
3
 
4
4
  require 'rb-inotify'
5
+ require 'stringio'
5
6
 
6
7
  class FileMonitorFilter
7
8
  def initialize
@@ -32,6 +33,14 @@ class FileMonitorFilter
32
33
  @patterns = []
33
34
  end
34
35
 
36
+ def to_s
37
+ str = StringIO.new
38
+ for pattern in @patterns
39
+ str.puts "#{pattern[0].to_s} #{pattern[1].inspect}"
40
+ end
41
+ str.string
42
+ end
43
+
35
44
  alias_method :d, :disallow
36
45
  alias_method :a, :allow
37
46
  end
@@ -49,17 +58,24 @@ class FileMonitor
49
58
  @frequency
50
59
  end
51
60
 
52
- def set_frequency(frequency)
53
- @frequency = frequency
61
+ # options
62
+ def follow_symlink=(follow_symlink)
63
+ @follow_symlink = follow_symlink
64
+ end
65
+
66
+ def follow_symlink(*args)
67
+ @follow_symlink = args[0] if args.size()
68
+ @follow_symlink
54
69
  end
55
70
 
56
71
  def initialize(project_dir = '.')
57
72
  @notifier = INotify::Notifier.new
58
73
  @project_dir = project_dir
59
74
 
60
- @events = []
61
- @ignores = []
62
- @frequency = 0.2
75
+ @events = []
76
+ @ignores = []
77
+ @frequency = 0.2
78
+ @follow_symlink = false
63
79
 
64
80
  @filters = {
65
81
  :files => FileMonitorFilter.new,
@@ -133,10 +149,15 @@ class FileMonitor
133
149
  end
134
150
  path
135
151
  end
152
+
136
153
 
137
154
  # Watch a directory
138
155
  def watch(dir)
139
156
  dir = trim_dir(dir)
157
+ if !@follow_symlink and File.symlink? dir
158
+ puts "ignore symlink directory #{dir}"
159
+ return false
160
+ end
140
161
  if ignored_dir?(dir)
141
162
  puts "ignore #{dir}"
142
163
  return false
@@ -170,7 +191,9 @@ class FileMonitor
170
191
  info += 'unknown ' + flags.to_s
171
192
  end
172
193
 
173
- if ignored_file?(event.absolute_name)
194
+ is_dir = flags.include?(:isdir)
195
+ if is_dir and ignored_dir?(event.absolute_name) or
196
+ !is_dir and ignored_file?(event.absolute_name)
174
197
  # the ignored info will not put currently
175
198
  info += "i #{event.absolute_name}"
176
199
  next
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file-monitor
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 2
9
- version: 0.1.2
4
+ prerelease:
5
+ version: 0.1.3
10
6
  platform: ruby
11
7
  authors:
12
8
  - JiangMiao
@@ -14,8 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2011-10-17 00:00:00 +08:00
18
- default_executable:
13
+ date: 2011-10-17 00:00:00 Z
19
14
  dependencies:
20
15
  - !ruby/object:Gem::Dependency
21
16
  name: rb-inotify
@@ -25,10 +20,6 @@ dependencies:
25
20
  requirements:
26
21
  - - ">="
27
22
  - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- - 8
31
- - 8
32
23
  version: 0.8.8
33
24
  type: :runtime
34
25
  version_requirements: *id001
@@ -40,17 +31,13 @@ dependencies:
40
31
  requirements:
41
32
  - - ">="
42
33
  - !ruby/object:Gem::Version
43
- segments:
44
- - 0
45
- - 8
46
- - 8
47
34
  version: 0.8.8
48
35
  type: :development
49
36
  version_requirements: *id002
50
37
  description: Ruby File Monitor is a easy way to watch the directories and files, execute commands when them changed.
51
38
  email: jiangfriend@gmail.com
52
- executables: []
53
-
39
+ executables:
40
+ - f5.rb
54
41
  extensions: []
55
42
 
56
43
  extra_rdoc_files: []
@@ -60,9 +47,10 @@ files:
60
47
  - examples/use-block.rb
61
48
  - examples/use-inherit.rb
62
49
  - examples/use-filter.rb
50
+ - examples/use-creator.rb
63
51
  - examples/f5.rb
52
+ - bin/f5.rb
64
53
  - README.md
65
- has_rdoc: true
66
54
  homepage: https://github.com/jiangmiao/ruby-file-monitor
67
55
  licenses: []
68
56
 
@@ -76,23 +64,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
64
  requirements:
77
65
  - - ">="
78
66
  - !ruby/object:Gem::Version
79
- segments:
80
- - 1
81
- - 8
82
- - 7
83
67
  version: 1.8.7
84
68
  required_rubygems_version: !ruby/object:Gem::Requirement
85
69
  none: false
86
70
  requirements:
87
71
  - - ">="
88
72
  - !ruby/object:Gem::Version
89
- segments:
90
- - 0
91
73
  version: "0"
92
74
  requirements: []
93
75
 
94
76
  rubyforge_project: file-monitor
95
- rubygems_version: 1.3.7
77
+ rubygems_version: 1.8.11
96
78
  signing_key:
97
79
  specification_version: 3
98
80
  summary: File Monitor Library