filemonitor 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/Manifest.txt +12 -0
- data/README.markdown +45 -0
- data/Rakefile +25 -0
- data/lib/FileMonitor.rb +111 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/FileMonitor_spec.rb +11 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +10 -0
- data/tasks/rspec.rake +21 -0
- metadata +78 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.markdown
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# FileMonitor
|
2
|
+
* http://github.com/joshaven/FileMonitor
|
3
|
+
* Joshaven Potter: <yourtech@gmail.com>
|
4
|
+
|
5
|
+
|
6
|
+
## DESCRIPTION
|
7
|
+
|
8
|
+
Watches the file system for changes.
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
require 'filemonitor'
|
12
|
+
file_spy = FileMonitor.new
|
13
|
+
# Add one or more files or directories with a callback block
|
14
|
+
file_spy.add(Dir.pwd) do |i|
|
15
|
+
# This callback will be executed in the context of a single file, even if you added a directory
|
16
|
+
puts "you probably should handle the change in #{i.file}"
|
17
|
+
end
|
18
|
+
# run a background process that watches for changed files... which is cloesed along with the parent process.
|
19
|
+
file_spy.spawn
|
20
|
+
|
21
|
+
|
22
|
+
## License
|
23
|
+
|
24
|
+
(The MIT License)
|
25
|
+
|
26
|
+
Copyright (c) 2009 Joshaven Potter
|
27
|
+
|
28
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
29
|
+
a copy of this software and associated documentation files (the
|
30
|
+
'Software'), to deal in the Software without restriction, including
|
31
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
32
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
33
|
+
permit persons to whom the Software is furnished to do so, subject to
|
34
|
+
the following conditions:
|
35
|
+
|
36
|
+
The above copyright notice and this permission notice shall be
|
37
|
+
included in all copies or substantial portions of the Software.
|
38
|
+
|
39
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
40
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
41
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
42
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
43
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
44
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
45
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'hoe', '>= 2.1.0'
|
3
|
+
require 'hoe'
|
4
|
+
require 'fileutils'
|
5
|
+
require './lib/FileMonitor'
|
6
|
+
|
7
|
+
Hoe.plugin :newgem
|
8
|
+
# Hoe.plugin :website
|
9
|
+
# Hoe.plugin :cucumberfeatures
|
10
|
+
|
11
|
+
# Generate all the Rake tasks
|
12
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
13
|
+
$hoe = Hoe.spec 'filemonitor' do
|
14
|
+
self.developer 'Joshaven Potter', 'yourtech@gmail.com'
|
15
|
+
self.rubyforge_name = self.name # TODO this is default value
|
16
|
+
# self.extra_deps = [['activesupport','>= 2.0.2']]
|
17
|
+
self.readme_file = "README.markdown"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'newgem/tasks'
|
21
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
22
|
+
|
23
|
+
# TODO - want other tests/tasks run by default? Add them to the list
|
24
|
+
# remove_task :default
|
25
|
+
# task :default => [:spec, :features]
|
data/lib/FileMonitor.rb
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
# Purpose:
|
2
|
+
# Watches the file system for changes
|
3
|
+
#
|
4
|
+
# Usage:
|
5
|
+
# > require 'file_monitor'
|
6
|
+
# > file_spy = FileMonitor.new
|
7
|
+
# > file_spy.add(Dir.pwd) do |i|
|
8
|
+
# > puts "you probably should handle the change in #{i.file}"
|
9
|
+
# > end
|
10
|
+
# > file_spy.spawn
|
11
|
+
|
12
|
+
require 'digest/md5'
|
13
|
+
|
14
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
15
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
16
|
+
|
17
|
+
module HashAccess
|
18
|
+
def method_missing(mth, *args)
|
19
|
+
return args.empty? ? self[mth] : self[mth.to_s.chomp('=').to_sym]=args.first
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class FileMonitor
|
24
|
+
VERSION = '0.0.1'
|
25
|
+
attr_accessor :spawns
|
26
|
+
def initialize()
|
27
|
+
@watched = []
|
28
|
+
end
|
29
|
+
|
30
|
+
def add(item, &callback)
|
31
|
+
if File.file? item
|
32
|
+
h = {:file=>File.expand_path(item), :callback=>callback}
|
33
|
+
h.extend(HashAccess)
|
34
|
+
@watched << h
|
35
|
+
elsif File.directory? item
|
36
|
+
files_recursive(item).each {|f| add(f, &callback) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def changes?
|
41
|
+
return first_changed
|
42
|
+
end
|
43
|
+
|
44
|
+
def process
|
45
|
+
# itterates watched files and runs callbacks when changes are detected.
|
46
|
+
@watched.each do |i|
|
47
|
+
key = digest(i.file)
|
48
|
+
i.digest = key if i.digest.nil? # skip first change detection, its always unknown on first run
|
49
|
+
update(i, key) unless i.digest == key
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def watching
|
54
|
+
# Returns an Array of files being watched
|
55
|
+
@watched
|
56
|
+
end
|
57
|
+
|
58
|
+
def monitor(interval = 1)
|
59
|
+
trap("INT") do
|
60
|
+
STDERR.puts " Interrupted by Control-C"
|
61
|
+
exit 2
|
62
|
+
end
|
63
|
+
|
64
|
+
while true
|
65
|
+
process
|
66
|
+
sleep interval
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def spawn(interval = 1)
|
71
|
+
if @spawns.nil?
|
72
|
+
@spawns = fork {monitor interval}
|
73
|
+
Process.detach(@spawns)
|
74
|
+
Kernel.at_exit do
|
75
|
+
Process.kill('HUP', @spawns)
|
76
|
+
@spawns = nil
|
77
|
+
end
|
78
|
+
true
|
79
|
+
else
|
80
|
+
@spawns
|
81
|
+
end
|
82
|
+
@spawns ||= []
|
83
|
+
end
|
84
|
+
private
|
85
|
+
def update(item, key)
|
86
|
+
item.callback.call(item) unless item.callback.nil?
|
87
|
+
item.digest= key
|
88
|
+
end
|
89
|
+
|
90
|
+
def first_changed
|
91
|
+
# returns first changed item or false
|
92
|
+
@watched.each {|i| return i if changed? i}
|
93
|
+
false
|
94
|
+
end
|
95
|
+
|
96
|
+
def changed?(item)
|
97
|
+
# returns md5 if changed, returns false when not changed
|
98
|
+
md5 = digest(item.file)
|
99
|
+
md5 == item.digest ? false : md5
|
100
|
+
end
|
101
|
+
|
102
|
+
def digest(file)
|
103
|
+
# returns the md5 of a file
|
104
|
+
Digest::MD5.hexdigest( File.read(file) )
|
105
|
+
end
|
106
|
+
|
107
|
+
def files_recursive(dirname)
|
108
|
+
# return an array of files from this (dirname) point forth.
|
109
|
+
Dir["#{dirname}/**/**"].collect {|f| f if File.file? f }.compact
|
110
|
+
end
|
111
|
+
end
|
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/FileMonitor.rb'}"
|
9
|
+
puts "Loading FileMonitor gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/spec/spec_helper.rb
ADDED
data/tasks/rspec.rake
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
begin
|
2
|
+
require 'spec'
|
3
|
+
rescue LoadError
|
4
|
+
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
5
|
+
require 'spec'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'spec/rake/spectask'
|
9
|
+
rescue LoadError
|
10
|
+
puts <<-EOS
|
11
|
+
To use rspec for testing you must install rspec gem:
|
12
|
+
gem install rspec
|
13
|
+
EOS
|
14
|
+
exit(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "Run the specs under spec/models"
|
18
|
+
Spec::Rake::SpecTask.new do |t|
|
19
|
+
t.spec_opts = ['--options', "spec/spec.opts"]
|
20
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: filemonitor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joshaven Potter
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-15 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.3.3
|
24
|
+
version:
|
25
|
+
description: Watches the file system for changes.
|
26
|
+
email:
|
27
|
+
- yourtech@gmail.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- History.txt
|
34
|
+
- Manifest.txt
|
35
|
+
files:
|
36
|
+
- History.txt
|
37
|
+
- Manifest.txt
|
38
|
+
- README.markdown
|
39
|
+
- Rakefile
|
40
|
+
- lib/FileMonitor.rb
|
41
|
+
- script/console
|
42
|
+
- script/destroy
|
43
|
+
- script/generate
|
44
|
+
- spec/FileMonitor_spec.rb
|
45
|
+
- spec/spec.opts
|
46
|
+
- spec/spec_helper.rb
|
47
|
+
- tasks/rspec.rake
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://github.com/joshaven/FileMonitor
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options:
|
54
|
+
- --main
|
55
|
+
- README.markdown
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project: filemonitor
|
73
|
+
rubygems_version: 1.3.5
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Watches the file system for changes.
|
77
|
+
test_files: []
|
78
|
+
|