-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstalker
executable file
·67 lines (58 loc) · 1.26 KB
/
stalker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env ruby
require "pathname"
class Stalker
attr_reader :paths
def initialize(script)
@handlers = {}
@paths = []
instance_eval(script.read)
end
def stalk(path, &block)
Dir[path].each do |p|
path = Pathname.new(p)
add_handler(path, block) if path.file?
end
end
def add_handler(path, block)
if @handlers.key? path
@handlers[path] << block
else
@handlers[path] = [ block ]
@paths << path
end
end
def handle(path)
@handlers[path].each { |handler| handler.call(path) }
end
end
class Engine
def initialize(script_path)
script = Pathname.new(script_path)
if script.file?
@stalker = Stalker.new(Pathname.new(script_path))
init_file_sizes
else
puts "Attempt to load the script file from #{script_path}. File not found."
exit
end
end
def run
loop do
@sizes.each do |file, size|
if file.size != size
@sizes[file] = file.size
@stalker.handle(file)
end
end
sleep 1
end
end
def init_file_sizes
@sizes = Hash.new(0)
@stalker.paths.each do |path|
@sizes[path] = path.size
end
end
end
puts "Start watching..."
Engine.new(ARGV.first || "watchlist").run