Permalink
Browse files

using the Listen gem

  • Loading branch information...
alexch committed Jun 30, 2012
1 parent 6ad7f4c commit 535819fc19d732d301c110d3c35db538c46ed3bc
View
@@ -1,10 +1,12 @@
source :rubygems
+gemspec
+
group :development do
gem "rake"
end
group :test do
gem 'rspec'
- gem 'wrong'
+ gem 'wrong', ">=0.6.2"
end
View
@@ -198,6 +198,11 @@ Based upon and/or inspired by:
* Andrés Botero <https://github.com/anbotero>
* Dreamcat4
+# Version History
+
+* v0.7.0
+ * uses Listen gem
+
# License
Open Source MIT License. See "LICENSE" file.
View
@@ -20,7 +20,8 @@ $spec =
require 'rubygems/specification'
data = File.read('rerun.gemspec')
spec = nil
- Thread.new { spec = eval("$SAFE = 3\n#{data}") }.join
+ #Thread.new { spec = eval("$SAFE = 3\n#{data}") }.join
+ spec = eval data
spec
end
View
@@ -1,16 +1,15 @@
here = File.expand_path(File.dirname(__FILE__))
$: << here unless $:.include?(here)
+require "listen" # pull in the Listen gem
require "rerun/system"
require "rerun/runner"
require "rerun/watcher"
-require "rerun/osxwatcher"
-require "rerun/fswatcher"
+require "rerun/glob"
-# todo: make sure this works in non-Mac environments (also Macs without growlnotify)
module Rerun
-
+
DEFAULT_PATTERN = "**/*.{rb,js,css,scss,sass,erb,html,haml,ru}"
-end
+end
View
@@ -1,6 +0,0 @@
-require 'rerun/watcher'
-
-module Rerun
- class FSWatcher < Watcher
- end
-end
View
@@ -0,0 +1,62 @@
+# based on http://cpan.uwinnipeg.ca/htdocs/Text-Glob/Text/Glob.pm.html#glob_to_regex_string-
+
+# todo: release as separate gem
+#
+module Rerun
+ class Glob
+ NO_LEADING_DOT = '(?=[^\.])' # todo
+
+ def initialize glob_string
+ @glob_string = glob_string
+ end
+
+ def to_regexp_string
+ chars = @glob_string.split('')
+ curlies = 0;
+ escaping = false;
+ chars.map do |char|
+ if escaping
+ escaping = false
+ char
+ else
+ case char
+ when '*'
+ ".*"
+ when "?"
+ "."
+ when "."
+ "\\."
+
+ when "{"
+ curlies += 1
+ "("
+ when "}"
+ if curlies > 0
+ curlies -= 1
+ ")"
+ else
+ char
+ end
+ when ","
+ if curlies > 0
+ "|"
+ else
+ char
+ end
+ when "\\"
+ escaping = true
+ "\\"
+
+ else
+ char
+
+ end
+ end
+ end.join
+ end
+
+ def to_regexp
+ Regexp.new(to_regexp_string)
+ end
+ end
+end
View
@@ -1,95 +0,0 @@
-require "rerun/system"
-require "rerun/watcher"
-
-#TODO: make it notice deleted files natively, rather than passing to 'examine'
-#TODO: use http://github.com/spicycode/fsevent
-module Rerun
- class OSXWatcher < Rerun::Watcher
- attr_reader :last_check, :valid_extensions
- attr_reader :stream
-
- def start
- prime
- timestamp_checked
-
- dirs = Array(directories.map{|d| d.dir})
-
- mac_callback = lambda do |stream, ctx, num_events, paths, marks, event_ids|
- examine
- # changed_files = extract_changed_files_from_paths(split_paths(paths, num_events))
- # timestamp_checked
- # puts "changed files:"
- # p changed_files
- # yield changed_files unless changed_files.empty?
- end
-
- @stream = OSX::FSEventStreamCreate(OSX::KCFAllocatorDefault, mac_callback, nil, dirs, OSX::KFSEventStreamEventIdSinceNow, @sleep_time, 0)
- raise "Failed to create stream" unless stream
-
- OSX::FSEventStreamScheduleWithRunLoop(stream, OSX::CFRunLoopGetCurrent(), OSX::KCFRunLoopDefaultMode)
- unless OSX::FSEventStreamStart(stream)
- raise "Failed to start stream"
- end
-
- @thread = Thread.new do
- begin
- OSX::CFRunLoopRun()
- rescue Interrupt
- OSX::FSEventStreamStop(stream)
- OSX::FSEventStreamInvalidate(stream)
- OSX::FSEventStreamRelease(stream)
- @stream = nil
- end
- end
-
- @thread.priority = @priority
- end
-
- def stop
- @thread.kill
- end
-
- def timestamp_checked
- @last_check = Time.now
- end
-
- def split_paths(paths, num_events)
- paths.regard_as('*')
- rpaths = []
- num_events.times { |i| rpaths << paths[i] }
- rpaths
- end
-
- def extract_changed_files_from_paths(paths)
- changed_files = []
- paths.each do |path|
- next if ignore_path?(path)
- Dir.glob(path + "*").each do |file|
- next if ignore_file?(file)
- changed_files << file if file_changed?(file)
- end
- end
- changed_files
- end
-
- def file_changed?(file)
- File.stat(file).mtime > last_check
- end
-
- def ignore_path?(path)
- path =~ /(?:^|\/)\.(git|svn)/
- end
-
- def ignore_file?(file)
- File.basename(file).index('.') == 0 or not valid_extension?(file)
- end
-
- def file_extension(file)
- file =~ /\.(\w+)$/ and $1
- end
-
- def valid_extension?(file)
- valid_extensions.nil? or valid_extensions.include?(file_extension(file))
- end
- end
-end
View
@@ -144,8 +144,8 @@ def start
end
unless @watcher
- watcher_class = osx_foundation? ? OSXWatcher : FSWatcher
- # watcher_class = FSWatcher
+ #watcher_class = osx_foundation? ? OSXWatcher : FSWatcher
+ watcher_class = Watcher
watcher = watcher_class.new do
restart unless @restarting
View
@@ -12,6 +12,7 @@ def osx_foundation?
require 'osx/foundation'
OSX.require_framework '/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework'
$osx_foundation = true
+ puts "Using OSX Watcher"
end
$osx_foundation
rescue LoadError
View
@@ -1,3 +1,8 @@
+require 'listen'
+
+require "wrong"
+include Wrong::D
+
Thread.abort_on_exception = true
@@ -74,16 +79,25 @@ def start
prime
@thread = Thread.new do
- while true do
+ # todo: multiple dirs
+ # todo: convert each dir's pattern to a regex and get Listen to do the file scan for us
+ @listener = Listen::Listener.new(@directories.first.dir) do |modified, added, removed|
+ #d { modified }
+ #d { added }
+ #d { removed }
examine
sleep(@sleep_time)
end
+ @listener.start
end
@thread.priority = @priority
at_exit { stop } #?
+ sleep 1 until @listener.instance_variable_get(:@adapter)
+ puts "Using adapter #{@listener.instance_variable_get(:@adapter)}"
+
end
# kill the filewatcher thread
@@ -110,6 +124,7 @@ def join
private
def examine
+
already_examined = Hash.new()
@directories.each do |directory|
@@ -123,6 +138,7 @@ def examine
all_found_files = @found.keys()
all_examined_files = already_examined.keys()
intersection = all_found_files - all_examined_files
+
intersection.each do |file_name|
@client_callback.call(DELETED, file_name)
@found.delete(file_name)
@@ -171,7 +187,7 @@ def initialize(dir, expression)
@dir.chop! if @dir =~ %r{/$}
end
- def files()
+ def files
return Dir["#{@dir}/#{@expression}"]
end
end
View
@@ -3,7 +3,7 @@ $spec = Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.name = 'rerun'
- s.version = '0.6.6'
+ s.version = '0.7.0.pre1'
s.description = "Restarts your app when a file changes"
s.summary = "Launches an app, and restarts it whenever the filesystem changes."
@@ -18,18 +18,14 @@ $spec = Gem::Specification.new do |s|
rerun.gemspec
bin/rerun
icons/rails_grn_sml.png
- icons/rails_red_sml.png
- lib/rerun.rb
- lib/rerun/fswatcher.rb
- lib/rerun/osxwatcher.rb
- lib/rerun/runner.rb
- lib/rerun/system.rb
- lib/rerun/watcher.rb
- ]
+ icons/rails_red_sml.png] +
+ Dir['lib/**/*.rb']
s.executables = ['rerun']
s.test_files = s.files.select {|path| path =~ /^spec\/.*_spec.rb/}
s.extra_rdoc_files = %w[README.md]
+
+ s.add_dependency 'listen'
#s.add_dependency 'rack', '>= 0.9.1'
#s.add_dependency 'launchy', '>= 0.3.3', '< 1.0'
View
@@ -1,13 +0,0 @@
-here = File.expand_path(File.dirname(__FILE__))
-require "#{here}/spec_helper.rb"
-require "#{here}/watcher_examples"
-require 'rerun/fswatcher'
-
-module Rerun
- describe FSWatcher do
- it_should_behave_like "all watchers"
- def create_watcher(&block)
- FSWatcher.new(&block)
- end
- end
-end
View
@@ -0,0 +1,47 @@
+here = File.expand_path(File.dirname(__FILE__))
+require "#{here}/spec_helper.rb"
+
+require "rerun/glob"
+
+module Rerun
+ describe Glob do
+ {
+ "x" => "x",
+
+ "*" => ".*",
+ "foo*" => "foo.*",
+ "*foo" => ".*foo",
+ "*foo*" => ".*foo.*",
+
+ "?" => ".",
+
+ "." => "\\.",
+
+ "{foo,bar,baz}" => "(foo|bar|baz)",
+ "{.txt,.md}" => '(\.txt|\.md)',
+
+ # pass through slash-escapes verbatim
+ "\\x" => "\\x",
+ "\\." => "\\.",
+ "\\*" => "\\*",
+ "\\\\" => "\\\\",
+
+ #"**/*.txt" => "([^/]*/)*.*\\.txt"
+
+ }.each_pair do |glob_string, regexp_string|
+ specify glob_string do
+ Glob.new(glob_string).to_regexp_string.should == regexp_string
+ end
+ end
+
+ it "excludes files beginning with dots"
+
+ describe "#to_regexp" do
+ it "makes a regexp" do
+ Glob.new("foo*").to_regexp.should == /foo.*/
+ end
+ end
+
+ end
+end
+
Oops, something went wrong.

0 comments on commit 535819f

Please sign in to comment.