filewatcher 0.3.6 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +33 -11
- data/Rakefile +2 -0
- data/bin/filewatcher +62 -6
- data/lib/filewatcher.rb +6 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2fb39cdddc7a64525396dac78226807bb53e450a
|
4
|
+
data.tar.gz: 641cea3ede6d8d1eb20fe7a0b8aa5ad75e86d75e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 728a408d87cfb6be8eb5556f31bc74972645275d0bb0c3c9a1045a9d4287e1aa3ff4f5a15fb46dfa14953e837ed2b38cdcfc8ad8e34e380b6f2980a9cebbc932
|
7
|
+
data.tar.gz: bad167aae4d3e3e7fe9848ea7295f1a0fd71e4984480f35765ee869814eeee8555f0fe705b5b61a33c7dc4cc8fd95d3e6bbce6410935e3c0c22b3233effcc950
|
data/README.md
CHANGED
@@ -8,6 +8,19 @@ Filewatcher
|
|
8
8
|
|
9
9
|
Lightweight filewatcher weighing less than 200 LoC. No dependencies or platform specific code.
|
10
10
|
Works everywhere. Monitors changes in the filesystem by polling. No config files needed to run.
|
11
|
+
When running filewatcher from the command line, you specify which files to monitor and what action
|
12
|
+
to perform on updates.
|
13
|
+
|
14
|
+
Search recursively for javascript files and run jshint when a file is updated, added,
|
15
|
+
renamed or deleted:
|
16
|
+
|
17
|
+
Linux/OSX:
|
18
|
+
|
19
|
+
$ filewatcher '**/*.js' 'jshint $FILENAME'
|
20
|
+
|
21
|
+
In Windows:
|
22
|
+
|
23
|
+
> filewatcher "**/*.js" "jshint %FILENAME%"
|
11
24
|
|
12
25
|
Install
|
13
26
|
-------
|
@@ -20,10 +33,10 @@ Command line utility
|
|
20
33
|
--------------------
|
21
34
|
|
22
35
|
Filewatcher scans the filesystem and execute shell commands when files are
|
23
|
-
|
36
|
+
updated, added, renamed or deleted.
|
24
37
|
|
25
38
|
Usage:
|
26
|
-
filewatcher [-i interval] "<filename>" "<shell command>"
|
39
|
+
filewatcher [-i interval][-l] "<filename>" "<shell command>"
|
27
40
|
|
28
41
|
Where
|
29
42
|
filename: filename(s) to scan.
|
@@ -47,13 +60,13 @@ In Linux/OSX:
|
|
47
60
|
|
48
61
|
Place filenames or filenames in quotes to use ruby filename globbing instead
|
49
62
|
of shell filename globbing. This will make filewatcher look for files in
|
50
|
-
subdirectories too.
|
63
|
+
subdirectories too. To watch all javascript files in subdirectories:
|
51
64
|
|
52
|
-
> filewatcher "
|
65
|
+
> filewatcher "**/*.js" "node %FILENAME%"
|
53
66
|
|
54
67
|
In Linux/OSX:
|
55
68
|
|
56
|
-
> filewatcher '
|
69
|
+
> filewatcher '**/*.js' 'node $FILENAME'
|
57
70
|
|
58
71
|
Try to run the updated file as a script when it is updated by using the
|
59
72
|
--exec/-e option. Works with files with file extensions that looks like a
|
@@ -71,6 +84,13 @@ filesystem gets updated:
|
|
71
84
|
|
72
85
|
$ filewatcher "src test" "ruby test/test_suite.rb"
|
73
86
|
|
87
|
+
Automatic restart of processes
|
88
|
+
------------------------------
|
89
|
+
|
90
|
+
The `--restart` option restarts the command when changes happens on the file system. The `--dontwait` starts the command when filewatcher is started. To start a webserver and have it automatically restart when html files are updated:
|
91
|
+
|
92
|
+
$ filewatcher --dontwait --restart "*.html" "python -m SimpleHTTPServer"
|
93
|
+
|
74
94
|
Available enviroment variables
|
75
95
|
------------------------------
|
76
96
|
|
@@ -101,7 +121,7 @@ Watch a list of files and directories:
|
|
101
121
|
require 'filewatcher'
|
102
122
|
|
103
123
|
FileWatcher.new(["lib/", "Rakefile"]).watch do |filename|
|
104
|
-
puts "
|
124
|
+
puts "Changed " + filename
|
105
125
|
end
|
106
126
|
|
107
127
|
Watch a single directory, for changes in all files and subdirectories:
|
@@ -142,6 +162,8 @@ To detect if a file is updated, added or deleted:
|
|
142
162
|
end
|
143
163
|
end
|
144
164
|
|
165
|
+
When a file is renamed it is detected as a deletion and a file addition.
|
166
|
+
|
145
167
|
To check for changes more often than the default once every second:
|
146
168
|
|
147
169
|
FileWatcher.new(["README.rdoc"]).watch(0.5) do |filename|
|
@@ -172,14 +194,14 @@ with no dependencies.
|
|
172
194
|
|
173
195
|
Credits
|
174
196
|
-------
|
197
|
+
This project would not be where it is today without the generous help provided by people reporting issues and these contributors:
|
175
198
|
|
176
|
-
Support for absolute and globbed paths by flbulgarelli: https://github.com/flbulgarelli
|
177
199
|
|
178
|
-
|
200
|
+
* Support for absolute and globbed paths by Franco Leonardo Bulgarelli: https://github.com/flbulgarelli
|
179
201
|
|
180
|
-
|
202
|
+
* Command line globbing by Kristoffer Roupé https://github.com/kitofr
|
181
203
|
|
182
|
-
|
204
|
+
This gem was initially inspired by Tom Lieber's blogg posting: http://alltom.com/pages/detecting-file-changes-with-ruby
|
183
205
|
|
184
206
|
Note on Patches/Pull Requests
|
185
207
|
-----------------------------
|
@@ -194,4 +216,4 @@ Note on Patches/Pull Requests
|
|
194
216
|
Copyright
|
195
217
|
---------
|
196
218
|
|
197
|
-
Copyright (c) 2011 -
|
219
|
+
Copyright (c) 2011 - 2015 Thomas Flemming. See LICENSE for details.
|
data/Rakefile
CHANGED
data/bin/filewatcher
CHANGED
@@ -3,9 +3,10 @@ require 'rubygems'
|
|
3
3
|
require 'filewatcher'
|
4
4
|
require 'trollop'
|
5
5
|
require 'pathname'
|
6
|
+
require 'thread'
|
6
7
|
|
7
8
|
options = Trollop::options do
|
8
|
-
version "filewatcher, version #{FileWatcher.VERSION} by Thomas Flemming
|
9
|
+
version "filewatcher, version #{FileWatcher.VERSION} by Thomas Flemming 2015"
|
9
10
|
banner <<-EOS
|
10
11
|
Filewatcher scans the filesystem and executes shell commands when files changes.
|
11
12
|
|
@@ -22,11 +23,13 @@ Examples:
|
|
22
23
|
Options:
|
23
24
|
EOS
|
24
25
|
|
25
|
-
opt :
|
26
|
+
opt :dontwait, "Do not wait for filesystem updates before running", :short => 'd', :type => :boolean, :default => false
|
27
|
+
opt :restart, "Restart process when filesystem is updated", :short => 'r', :type => :boolean, :default => false
|
28
|
+
opt :list, "Print name of files being watched"
|
26
29
|
opt :exec, "Execute file as a script when file is updated.", :short => 'e', :type => :boolean, :default => false
|
27
30
|
opt :include, "Include files", :type => :string, :default => "*"
|
28
31
|
opt :exclude, "Exclude file(s) matching", :type => :string, :default => ""
|
29
|
-
opt :
|
32
|
+
opt :interval, "Interval to scan filesystem. Defaults to 0.5 seconds.", :short => 'i', :type => :float, :default => 0.5
|
30
33
|
end
|
31
34
|
|
32
35
|
Trollop::die "must have at least one argument" if(ARGV.size == 0)
|
@@ -56,7 +59,45 @@ end
|
|
56
59
|
|
57
60
|
files = split_files_void_escaped_whitespace(files)
|
58
61
|
|
59
|
-
|
62
|
+
def fork_process(env, cmd)
|
63
|
+
pipe_read, pipe_write = IO.pipe
|
64
|
+
fork do
|
65
|
+
pipe_read.close
|
66
|
+
pipe_write.write Process.spawn(env,cmd).to_s
|
67
|
+
exit
|
68
|
+
end
|
69
|
+
|
70
|
+
Kernel.sleep 1
|
71
|
+
pipe_write.close
|
72
|
+
child_pid = pipe_read.read.to_i
|
73
|
+
pipe_read.close
|
74
|
+
return child_pid
|
75
|
+
end
|
76
|
+
|
77
|
+
def kill_process(child_pid)
|
78
|
+
still_running = true
|
79
|
+
begin
|
80
|
+
Process.kill(1, child_pid)
|
81
|
+
rescue Errno::ESRCH
|
82
|
+
still_running = false
|
83
|
+
end
|
84
|
+
while still_running do
|
85
|
+
begin
|
86
|
+
Process.getpgid( child_pid)
|
87
|
+
still_running = true
|
88
|
+
rescue Errno::ESRCH
|
89
|
+
still_running = false
|
90
|
+
end
|
91
|
+
Kernel.sleep 0.1
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
if(options[:restart])
|
96
|
+
rd, wr = IO.pipe
|
97
|
+
child_pid = nil
|
98
|
+
end
|
99
|
+
|
100
|
+
FileWatcher.new(files, options[:list], options[:dontwait]).watch(options[:interval]) do |filename, event|
|
60
101
|
cmd = nil
|
61
102
|
if(options[:exec] and File.exist?(filename))
|
62
103
|
extension = filename[/(\.[^\.]*)$/,0]
|
@@ -91,8 +132,23 @@ FileWatcher.new(files, options[:list]).watch(options[:interval]) do |filename, e
|
|
91
132
|
if(event != :delete)
|
92
133
|
ENV['FILEPATH'] = path.realpath.to_s
|
93
134
|
end
|
94
|
-
|
95
|
-
|
135
|
+
|
136
|
+
if(options[:restart])
|
137
|
+
if(child_pid == nil)
|
138
|
+
child_pid = fork_process(env, cmd)
|
139
|
+
else
|
140
|
+
kill_process(child_pid)
|
141
|
+
child_pid = fork_process(env, cmd)
|
142
|
+
end
|
143
|
+
else
|
144
|
+
begin
|
145
|
+
pid = Process.spawn(env, cmd)
|
146
|
+
Process.wait()
|
147
|
+
rescue SystemExit, Interrupt
|
148
|
+
exit(0)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
96
152
|
else
|
97
153
|
case(event)
|
98
154
|
when :changed
|
data/lib/filewatcher.rb
CHANGED
@@ -6,14 +6,14 @@ class FileWatcher
|
|
6
6
|
attr_accessor :filenames
|
7
7
|
|
8
8
|
def self.VERSION
|
9
|
-
return '0.
|
9
|
+
return '0.4.0'
|
10
10
|
end
|
11
11
|
|
12
|
-
def initialize(unexpanded_filenames, print_filelist=false)
|
12
|
+
def initialize(unexpanded_filenames, print_filelist=false, dontwait=false)
|
13
13
|
@unexpanded_filenames = unexpanded_filenames
|
14
14
|
@last_mtimes = { }
|
15
15
|
@filenames = expand_directories(@unexpanded_filenames)
|
16
|
-
|
16
|
+
@dontwait = dontwait
|
17
17
|
puts 'Watching:' if print_filelist
|
18
18
|
@filenames.each do |filename|
|
19
19
|
raise 'File does not exist' unless File.exist?(filename)
|
@@ -23,6 +23,9 @@ class FileWatcher
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def watch(sleep=1, &on_update)
|
26
|
+
if(@dontwait)
|
27
|
+
yield '',''
|
28
|
+
end
|
26
29
|
loop do
|
27
30
|
begin
|
28
31
|
Kernel.sleep sleep until filesystem_updated?
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filewatcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thomas Flemming
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-02-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: trollop
|