filewatcher 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +44 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/lib/filewatcher.rb +66 -0
- data/test/helper.rb +10 -0
- data/test/test_filewatcher.rb +39 -0
- metadata +74 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Thomas Flemming
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
= Filewatcher
|
2
|
+
|
3
|
+
Simple file watcher. Detect detect changes in files.
|
4
|
+
|
5
|
+
Examples:
|
6
|
+
|
7
|
+
require 'filewatcher'
|
8
|
+
|
9
|
+
FileWatcher.new(["file1.txt", "file2.txt"]).watch do |filename|
|
10
|
+
puts "Updated " + filename
|
11
|
+
end
|
12
|
+
|
13
|
+
Watch directories and files:
|
14
|
+
|
15
|
+
FileWatcher.new(["./lib/", "Rakefile", "test"]).watch do |filename|
|
16
|
+
puts "Updated " + filename
|
17
|
+
end
|
18
|
+
|
19
|
+
Print a list of watched files to stdout out before watching files:
|
20
|
+
|
21
|
+
FileWatcher.new(["./lib/"],"Watching files.").watch do |filename|
|
22
|
+
puts "Updated " + filename
|
23
|
+
end
|
24
|
+
|
25
|
+
To check for changes more often than once every second second:
|
26
|
+
|
27
|
+
FileWatcher.new(["./lib/"],"Watching files.").watch(0.5) do |filename|
|
28
|
+
puts "Updated " + filename
|
29
|
+
end
|
30
|
+
|
31
|
+
== Note on Patches/Pull Requests
|
32
|
+
|
33
|
+
* Fork the project.
|
34
|
+
* Make your feature addition or bug fix.
|
35
|
+
* Add tests for it. This is important so I don't break it in a
|
36
|
+
future version unintentionally.
|
37
|
+
* Commit, do not mess with rakefile, version, or history.
|
38
|
+
(if you want to have your own version, that is fine but
|
39
|
+
bump version in a commit by itself I can ignore when I pull)
|
40
|
+
* Send me a pull request. Bonus points for topic branches.
|
41
|
+
|
42
|
+
== Copyright
|
43
|
+
|
44
|
+
Copyright (c) 2010 Thomas Flemming. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "filewatcher"
|
8
|
+
gem.summary = %Q{Simple filewatcher.}
|
9
|
+
gem.description = %Q{Detect detect changes in files.}
|
10
|
+
gem.email = "thomas.flemming@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/thomasfl/filewatcher"
|
12
|
+
gem.authors = ["Thomas Flemming"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/test_*.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
task :test => :check_dependencies
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "filewatcher #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/filewatcher.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
class FileWatcher
|
2
|
+
|
3
|
+
def initialize(filenames,print_filelist=false)
|
4
|
+
if(filenames.kind_of?String)
|
5
|
+
filenames = [filenames]
|
6
|
+
end
|
7
|
+
|
8
|
+
filenames = expand_directories(filenames)
|
9
|
+
|
10
|
+
if(print_filelist)
|
11
|
+
if(print_filelist.kind_of?String)
|
12
|
+
puts print_filelist
|
13
|
+
else
|
14
|
+
puts "Watching:"
|
15
|
+
end
|
16
|
+
filenames.each do |filename|
|
17
|
+
puts filename
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
@last_mtimes = { }
|
22
|
+
filenames.each do |filename|
|
23
|
+
raise "File does not exist" unless File.exist?(filename)
|
24
|
+
@last_mtimes[filename] = File.stat(filename).mtime
|
25
|
+
end
|
26
|
+
@filenames = filenames
|
27
|
+
end
|
28
|
+
|
29
|
+
def watch(sleep=1, &on_update)
|
30
|
+
loop do
|
31
|
+
Kernel.sleep sleep until file_updated?
|
32
|
+
yield @updated_file
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def file_updated?
|
37
|
+
@filenames.each do |filename|
|
38
|
+
mtime = File.stat(filename).mtime
|
39
|
+
updated = @last_mtimes[filename] < mtime
|
40
|
+
@last_mtimes[filename] = mtime
|
41
|
+
if(updated)
|
42
|
+
@updated_file = filename
|
43
|
+
return true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
return false
|
47
|
+
end
|
48
|
+
|
49
|
+
def expand_directories(filenames)
|
50
|
+
files = []
|
51
|
+
filenames.each do |filename|
|
52
|
+
if(File.directory?(filename))
|
53
|
+
files = files + find(filename)
|
54
|
+
end
|
55
|
+
if(File.file?(filename))
|
56
|
+
files << filename
|
57
|
+
end
|
58
|
+
end
|
59
|
+
filenames = files
|
60
|
+
end
|
61
|
+
|
62
|
+
def find(dir, filename="*.*", subdirs=true)
|
63
|
+
Dir[ subdirs ? File.join(dir.split(/\\/), "**", filename) : File.join(dir.split(/\\/), filename) ]
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestFilewatcher < Test::Unit::TestCase
|
4
|
+
|
5
|
+
should "should detect directories and extract filenames" do
|
6
|
+
|
7
|
+
FileWatcher.new(["./test/"],"Watching files. Ctrl-C to abort.").watch(0.1) do |filename|
|
8
|
+
puts "updated: " + filename
|
9
|
+
end
|
10
|
+
|
11
|
+
FileWatcher.new(["./test/", "Rakefile", "lib"]).watch(0.1) do |filename|
|
12
|
+
puts "updated: " + filename
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
# should "should detect changes in files" do
|
19
|
+
|
20
|
+
# @pid = fork do
|
21
|
+
|
22
|
+
# trap("SIGINT") do
|
23
|
+
# exit
|
24
|
+
# end
|
25
|
+
|
26
|
+
# FileWatcher.new(["test/helper.rb"]).watch(0.1) do |filename|
|
27
|
+
# puts "updated: " + filename
|
28
|
+
# end
|
29
|
+
|
30
|
+
# end
|
31
|
+
# sleep(1)
|
32
|
+
# FileUtils.touch("test/helper.rb")
|
33
|
+
# sleep(1)
|
34
|
+
# Process.kill('SIGINT', @pid) rescue nil
|
35
|
+
|
36
|
+
# end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: filewatcher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Thomas Flemming
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-25 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: thoughtbot-shoulda
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: Detect detect changes in files.
|
26
|
+
email: thomas.flemming@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- lib/filewatcher.rb
|
42
|
+
- test/helper.rb
|
43
|
+
- test/test_filewatcher.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://github.com/thomasfl/filewatcher
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options:
|
50
|
+
- --charset=UTF-8
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.5
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Simple filewatcher.
|
72
|
+
test_files:
|
73
|
+
- test/helper.rb
|
74
|
+
- test/test_filewatcher.rb
|