filewatcher 0.3.5 → 0.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 580b8d90d111cb7706b3c3a4f587bbb44ef3589c
4
- data.tar.gz: b91ab8f3512252cda8da5a6e137f6919857df4e8
3
+ metadata.gz: 822dcac9d604c0ba52d968e3bf92df44da1e1512
4
+ data.tar.gz: 6851bd0744d58eb1a9150c35fb27ace77f261793
5
5
  SHA512:
6
- metadata.gz: a76f9a6b2153aae280c48357a32df976123da3c8f5f8a9e726812ab4a4b9cfe2292b2789b337da7b759d36b8962c373b7e372c27ce5fd5e8bfc891af8ceda307
7
- data.tar.gz: 82e485c94aba35cae31403d8f9d043135bde656cd3a34b37b4d8abe31eed628bb74375b675ed027611b22b071aacb7f97cc73cd4aa513739b8c7d71e295a6364
6
+ metadata.gz: 173d5fc6f40ef6180110bf21169ab6b59f6f40a78cecad86bef0a21541e218febca5f13ed57247f4b0eb84ac08f1cbd2bbac89f6a4a25e61e9378e4f5ac70d21
7
+ data.tar.gz: 32a2ac8c557f68880771eb6423be097fca08b8eff914812361de881a3fe7f19342f2afc9e81f1051fe7d60c00825279405f81d9494ca2854c804654f860008f7
data/README.md CHANGED
@@ -1,7 +1,10 @@
1
1
  Filewatcher
2
2
  ===========
3
3
 
4
- [![Build Status](https://secure.travis-ci.org/judofyr/temple.png?branch=master)](http://travis-ci.org/judofyr/temple) [![Dependency Status](https://gemnasium.com/judofyr/temple.png?travis)](https://gemnasium.com/judofyr/temple) [![Code Climate](https://codeclimate.com/github/judofyr/temple.png)](https://codeclimate.com/github/judofyr/temple)
4
+ [![Gem Version](https://badge.fury.io/rb/filewatcher.svg)](http://badge.fury.io/rb/filewatcher)
5
+ [![Build Status](https://secure.travis-ci.org/thomasfl/filewatcher.png?branch=master)](http://travis-ci.org/thomasfl/filewatcher)
6
+ [![Dependency Status](https://gemnasium.com/thomasfl/filewatcher.png?travis)](https://gemnasium.com/thomasfl/filewatcher)
7
+ [![Code Climate](https://codeclimate.com/github/thomasfl/filewatcher.png)](https://codeclimate.com/github/thomasfl/filewatcher)
5
8
 
6
9
  Lightweight filewatcher weighing less than 200 LoC. No dependencies or platform specific code.
7
10
  Works everywhere. Monitors changes in the filesystem by polling. No config files needed to run.
@@ -84,7 +87,6 @@ Command line options
84
87
 
85
88
  --interval, -i <f>: Interval in seconds to scan filesystem. Defaults to 0.5 seconds.
86
89
  --exec, -e: Execute file as a script when file is updated.
87
- --recurse, -r <s>: Recurse into the directory, watching everything matching 'expression'
88
90
  --include, -n <s>: Include files (default: *)
89
91
  --exclude, -x <s>: Exclude file(s) matching (default: "")
90
92
  --list, -l: Print name of files being watched
@@ -102,6 +104,30 @@ Watch a list of files and directories:
102
104
  puts "Updated " + filename
103
105
  end
104
106
 
107
+ Watch a single directory, for changes in all files and subdirectories:
108
+
109
+ FileWatcher.new("lib/").watch do |filename|
110
+ ...
111
+ end
112
+
113
+ Notice that the previous is equivalent to the following:
114
+
115
+ FileWatcher.new("lib/**/*").watch do |filename|
116
+ ...
117
+ end
118
+
119
+ Watch files and dirs in the given directory - and not in subdirectories:
120
+
121
+ FileWatcher.new("lib/*").watch do |filename|
122
+ ...
123
+ end
124
+
125
+ Watch an absolute directory:
126
+
127
+ FileWatcher.new("/tmp/foo").watch do |filename|
128
+ ...
129
+ end
130
+
105
131
  To detect if a file is updated, added or deleted:
106
132
 
107
133
  FileWatcher.new(["README.rdoc"]).watch() do |filename, event|
@@ -143,16 +169,12 @@ for syntax.
143
169
  The filewatcher library is just a single file with 96 LOC (including comments)
144
170
  with no dependencies.
145
171
 
146
- TODO
147
- ----
148
-
149
- Use thor in the command line utility.
150
- The Ruby API is fairly well tested but the command line program has been
151
- buggy.
152
172
 
153
173
  Credits
154
174
  -------
155
175
 
176
+ Support for absolute and globbed paths by flbulgarelli: https://github.com/flbulgarelli
177
+
156
178
  Code inspired by Tom Lieber's blogg posting: http://alltom.com/pages/detecting-file-changes-with-ruby
157
179
 
158
180
  Find method by c00lrguy: http://snippets.dzone.com/posts/show/5457
data/Rakefile CHANGED
@@ -6,24 +6,3 @@ task :default => :test
6
6
  task :test do
7
7
  sh "bacon -Ilib -Itest --automatic --quiet"
8
8
  end
9
-
10
- # begin
11
- # require 'jeweler'
12
- # Jeweler::Tasks.new do |gem|
13
- # gem.name = "filewatcher"
14
- # gem.summary = %Q{Simple filewatcher.}
15
- # gem.description = %Q{Detect changes in filesystem.}
16
- # gem.email = "thomas.flemming@gmail.com"
17
- # gem.homepage = "http://github.com/thomasfl/filewatcher"
18
- # gem.executables = ["filewatcher"]
19
- # gem.authors = ["Thomas Flemming"]
20
- # gem.add_dependency 'trollop', '~> 2.0'
21
- # gem.licenses = ["MIT"]
22
- # end
23
- # rescue LoadError
24
- # puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
25
- # end
26
-
27
- # task :test => :check_dependencies
28
-
29
- # task :default => :test
data/bin/filewatcher CHANGED
@@ -7,7 +7,7 @@ require 'pathname'
7
7
  options = Trollop::options do
8
8
  version "filewatcher, version #{FileWatcher.VERSION} by Thomas Flemming 2014"
9
9
  banner <<-EOS
10
- Filewatcher scans filesystem and execute shell commands when files changes.
10
+ Filewatcher scans the filesystem and executes shell commands when files changes.
11
11
 
12
12
  Usage:
13
13
  filewatcher [-i interval] "<filenames>" "<shell commands>"
@@ -18,13 +18,12 @@ Where
18
18
  Examples:
19
19
  filewatcher "myfile" "echo 'myfile has changed'"
20
20
  filewatcher '*.rb' 'ruby $FILENAME'
21
-
21
+ filewatcher '**/*.rb' 'ruby $FILENAME' # Watch subdirectories
22
22
  Options:
23
23
  EOS
24
24
 
25
25
  opt :interval, "Interval to scan filesystem. Defaults to 0.5 seconds.", :short => 'i', :type => :float, :default => 0.5
26
26
  opt :exec, "Execute file as a script when file is updated.", :short => 'e', :type => :boolean, :default => false
27
- opt :recurse, "Recurse into the directory, watching everything matching 'expression'", :short => 'r', :type => :string
28
27
  opt :include, "Include files", :type => :string, :default => "*"
29
28
  opt :exclude, "Exclude file(s) matching", :type => :string, :default => ""
30
29
  opt :list, "Print name of files being watched"
@@ -41,13 +40,6 @@ if(ARGV.length == 1)
41
40
  files << ARGV[0]
42
41
  end
43
42
 
44
- if(options[:recurse])
45
- base = File.join(options[:recurse], "**")
46
- inc = Dir.glob(File.join(base, options[:include]))
47
- exc = Dir.glob(File.join(base, options[:exclude]))
48
- files = inc - exc
49
- end
50
-
51
43
  def split_files_void_escaped_whitespace(files)
52
44
  splitted_filenames = []
53
45
  files.each do |name|
@@ -106,7 +98,7 @@ FileWatcher.new(files, options[:list]).watch(options[:interval]) do |filename, e
106
98
  when :changed
107
99
  print "file updated"
108
100
  when :delete
109
- print "file eleted"
101
+ print "file deleted"
110
102
  when :new
111
103
  print "new file"
112
104
  else
data/lib/filewatcher.rb CHANGED
@@ -3,8 +3,10 @@
3
3
  # Issues: Currently doesn't monitor changes in directorynames
4
4
  class FileWatcher
5
5
 
6
+ attr_accessor :filenames
7
+
6
8
  def self.VERSION
7
- return '0.3.5'
9
+ return '0.3.6'
8
10
  end
9
11
 
10
12
  def initialize(unexpanded_filenames, print_filelist=false)
@@ -78,20 +80,19 @@ class FileWatcher
78
80
  if(!patterns.kind_of?Array)
79
81
  patterns = [patterns]
80
82
  end
81
- filenames = []
82
- patterns.each do |filename|
83
- if(File.directory?(filename))
84
- filenames = filenames + find(filename)
85
- else
86
- filenames = filenames + find('.', filename, true)
87
- end
88
- end
89
83
 
90
- return filenames.uniq
84
+ patterns.map { |it| Dir[fulldepth(it)] }.flatten.uniq
91
85
  end
92
86
 
93
- def find(dir, filename='*.*', subdirs=true)
94
- Dir[ subdirs ? File.join(dir.split(/\\/), '**', filename) : File.join(dir.split(/\\/), filename) ]
87
+ private
88
+
89
+ def fulldepth(pattern)
90
+ if File.directory? pattern
91
+ "#{pattern}/**/*"
92
+ else
93
+ pattern
94
+ end
95
95
  end
96
96
 
97
+
97
98
  end
@@ -3,8 +3,55 @@ require 'bacon'
3
3
  require 'fileutils'
4
4
  require File.expand_path("../lib/filewatcher.rb",File.dirname(__FILE__))
5
5
 
6
+
7
+
6
8
  describe FileWatcher do
7
9
 
10
+ fixtures =
11
+ %w(test/fixtures/file4.rb
12
+ test/fixtures/subdir/file6.rb
13
+ test/fixtures/subdir/file5.rb
14
+ test/fixtures/file2.txt
15
+ test/fixtures/file1.txt
16
+ test/fixtures/file3.rb)
17
+
18
+ explicit_relative_fixtures = fixtures.map { |it| "./#{it}" }
19
+
20
+ subfolder = 'test/fixtures/new_sub_folder'
21
+
22
+ after do
23
+ FileUtils.rm_rf subfolder
24
+ end
25
+
26
+ def includes_all(elements)
27
+ lambda { |it| elements.all? { |element| it.include? element }}
28
+ end
29
+
30
+ it "should handle absolute paths with globs" do
31
+ filewatcher = FileWatcher.new(File.expand_path('test/fixtures/**/*'))
32
+
33
+ filewatcher.filenames.should.satisfy &includes_all(fixtures.map { |it| File.expand_path(it) })
34
+ end
35
+
36
+ it "should handle globs" do
37
+ filewatcher = FileWatcher.new('test/fixtures/**/*')
38
+
39
+ filewatcher.filenames.should.satisfy &includes_all(fixtures)
40
+ end
41
+
42
+
43
+ it "should handle explicit relative paths with globs" do
44
+ filewatcher = FileWatcher.new('./test/fixtures/**/*')
45
+
46
+ filewatcher.filenames.should.satisfy &includes_all(explicit_relative_fixtures)
47
+ end
48
+
49
+ it "should handle explicit relative paths" do
50
+ filewatcher = FileWatcher.new('./test/fixtures')
51
+
52
+ filewatcher.filenames.should.satisfy &includes_all(explicit_relative_fixtures)
53
+ end
54
+
8
55
  it "should detect file deletions" do
9
56
  filename = "test/fixtures/file1.txt"
10
57
  open(filename,"w") { |f| f.puts "content1" }
@@ -34,14 +81,21 @@ describe FileWatcher do
34
81
  end
35
82
 
36
83
  it "should detect new files in subfolders" do
37
- subfolder = 'test/fixtures/new_sub_folder'
38
- filewatcher = FileWatcher.new(["test/fixtures"])
39
- filewatcher.filesystem_updated?.should.be.false
40
84
  FileUtils::mkdir_p subfolder
85
+
86
+ filewatcher = FileWatcher.new(["./test/fixtures"])
41
87
  filewatcher.filesystem_updated?.should.be.false
88
+
42
89
  open(subfolder + "/file.txt","w") { |f| f.puts "xyz" }
43
90
  filewatcher.filesystem_updated?.should.be.true
44
- FileUtils.rm_rf subfolder
91
+ end
92
+
93
+ it "should detect new subfolders" do
94
+ filewatcher = FileWatcher.new(["test/fixtures"])
95
+ filewatcher.filesystem_updated?.should.be.false
96
+
97
+ FileUtils::mkdir_p subfolder
98
+ filewatcher.filesystem_updated?.should.be.true
45
99
  end
46
100
 
47
101
  end
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.3.5
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Flemming
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-02 00:00:00.000000000 Z
11
+ date: 2014-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: trollop
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.2'
55
- description: Detect changes in filesystem.
55
+ description: Detect changes in filesystem. Works anywhere.
56
56
  email:
57
57
  - thomas.flemming@gmail.com
58
58
  executables: