rerun 0.7.0.pre2 → 0.7.0.pre3

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -10,7 +10,7 @@ desc "Run all specs"
10
10
  RSpec::Core::RakeTask.new('spec') do |t|
11
11
  ENV['ENV'] = "test"
12
12
  t.pattern = 'spec/**/*_spec.rb'
13
- t.ruby_opts = ['-rubygems'] if defined? Gem
13
+ t.rspec_opts = ['--color']
14
14
  end
15
15
 
16
16
  $rubyforge_project = 'pivotalrb'
data/lib/rerun/glob.rb CHANGED
@@ -12,6 +12,9 @@ module Rerun
12
12
 
13
13
  def to_regexp_string
14
14
  chars = @glob_string.split('')
15
+
16
+ chars = smoosh(chars)
17
+
15
18
  curlies = 0;
16
19
  escaping = false;
17
20
  chars.map do |char|
@@ -20,6 +23,8 @@ module Rerun
20
23
  char
21
24
  else
22
25
  case char
26
+ when '**'
27
+ "([^/]+/)*"
23
28
  when '*'
24
29
  ".*"
25
30
  when "?"
@@ -58,5 +63,20 @@ module Rerun
58
63
  def to_regexp
59
64
  Regexp.new(to_regexp_string)
60
65
  end
66
+
67
+ def smoosh chars
68
+ out = []
69
+ until chars.empty?
70
+ char = chars.shift
71
+ if char == "*" and chars.first == "*"
72
+ chars.shift
73
+ chars.shift if chars.first == "/"
74
+ out.push("**")
75
+ else
76
+ out.push(char)
77
+ end
78
+ end
79
+ out
80
+ end
61
81
  end
62
82
  end
data/lib/rerun/runner.rb CHANGED
@@ -144,14 +144,12 @@ module Rerun
144
144
 
145
145
  unless @watcher
146
146
 
147
- watcher = Watcher.new do
147
+ watcher = Watcher.new(:directory => dir, :pattern => pattern) do
148
148
  restart unless @restarting
149
149
  end
150
- say "Watching #{dir}/#{pattern}"
151
- watcher.add_directory(dir, pattern)
152
- watcher.sleep_time = 1
153
150
  watcher.start
154
151
  @watcher = watcher
152
+ say "Watching #{dir}/#{pattern} using #{watcher.adapter.class.name.split('::').last} adapter"
155
153
  end
156
154
  end
157
155
 
@@ -169,13 +167,14 @@ module Rerun
169
167
  end
170
168
 
171
169
  def signal(signal)
172
- say "Sending #{signal} to #{@pid}" unless signal == 0
170
+ say "Sending signal #{signal} to #{@pid}" unless signal == 0
173
171
  Process.kill(signal, @pid)
174
172
  true
175
173
  rescue
176
174
  false
177
175
  end
178
176
 
177
+ # todo: test escalation
179
178
  def stop
180
179
  if @pid && (@pid != 0)
181
180
  notify "stopping", "All good things must come to an end." unless @restarting
data/lib/rerun/watcher.rb CHANGED
@@ -2,7 +2,7 @@ require 'listen'
2
2
 
3
3
  Thread.abort_on_exception = true
4
4
 
5
- # This class will watch a directory or a set of directories and alert you of
5
+ # This class will watch a directory and alert you of
6
6
  # new files, modified files, deleted files.
7
7
  #
8
8
  # Author: Paul Horman, http://paulhorman.com/filesystemwatcher/
@@ -13,56 +13,40 @@ module Rerun
13
13
  MODIFIED = 1
14
14
  DELETED = 2
15
15
 
16
- attr_accessor :sleep_time, :priority
17
- attr_reader :directories
16
+ attr_reader :directory, :pattern, :priority
18
17
 
19
- def initialize(&client_callback)
18
+ # Create a file system watcher. Start it by calling #start.
19
+ #
20
+ # @param options[:directory] the directory to watch (default ".")
21
+ # @param options[:pattern] the glob pattern to search under the watched directory (default "**/*")
22
+ # @param options[:priority] the priority of the watcher thread (default 0)
23
+ #
24
+ def initialize(options = {}, &client_callback)
20
25
  @client_callback = client_callback
21
26
 
22
- @sleep_time = 1
23
- @priority = 0
27
+ options = {
28
+ :directory => ".",
29
+ :pattern => "**/*",
30
+ :priority => 0,
31
+ }.merge(options)
24
32
 
25
- @directories = []
26
- @files = []
33
+ @pattern = options[:pattern]
34
+ @directory = options[:directory]
35
+ if FileTest.exists?(directory) && FileTest.readable?(directory) then
36
+ @directory = Directory.new(directory, @pattern)
37
+ else
38
+ raise InvalidDirectoryError, "Dir '#{directory}' either doesnt exist or isnt readable"
39
+ end
40
+ @priority = options[:priority]
27
41
 
28
42
  @found = nil
29
43
  @first_time = true
30
44
  @thread = nil
31
-
32
- end
33
-
34
- # add a directory to be watched
35
- # @param dir the directory to watch
36
- # @param expression the glob pattern to search under the watched directory
37
- def add_directory(dir, expression="**/*")
38
- if FileTest.exists?(dir) && FileTest.readable?(dir) then
39
- @directories << Directory.new(dir, expression)
40
- else
41
- raise InvalidDirectoryError, "Dir '#{dir}' either doesnt exist or isnt readable"
42
- end
43
- end
44
-
45
- def remove_directory(dir)
46
- @directories.delete(dir)
47
- end
48
-
49
- # add a specific file to the watch list
50
- # @param file the file to watch
51
- def add_file(file)
52
- if FileTest.exists?(file) && FileTest.readable?(file) then
53
- @files << file
54
- else
55
- raise InvalidFileError, "File '#{file}' either doesnt exist or isnt readable"
56
- end
57
- end
58
-
59
- def remove_file(file)
60
- @files.delete(file)
61
45
  end
62
46
 
63
47
  def prime
64
48
  @first_time = true
65
- @found = Hash.new()
49
+ @found = {}
66
50
  examine
67
51
  @first_time = false
68
52
  end
@@ -76,24 +60,27 @@ module Rerun
76
60
 
77
61
  @thread = Thread.new do
78
62
  # todo: multiple dirs
79
- # todo: convert each dir's pattern to a regex and get Listen to do the file scan for us
80
- @listener = Listen::Listener.new(@directories.first.dir) do |modified, added, removed|
63
+
64
+ regexp = Glob.new(@pattern).to_regexp
65
+ @listener = Listen::Listener.new(@directory.dir, :filter => regexp) do |modified, added, removed|
81
66
  #d { modified }
82
67
  #d { added }
83
68
  #d { removed }
84
69
  examine
85
- sleep(@sleep_time)
86
70
  end
87
71
  @listener.start
88
72
  end
89
73
 
90
74
  @thread.priority = @priority
91
75
 
92
- at_exit { stop } #?
93
-
94
- sleep 1 until @listener.instance_variable_get(:@adapter)
95
- puts "Using adapter #{@listener.instance_variable_get(:@adapter)}"
76
+ at_exit { stop } # try really hard to clean up after ourselves
77
+ end
96
78
 
79
+ def adapter
80
+ timeout(4) do
81
+ sleep 1 until adapter = @listener.instance_variable_get(:@adapter)
82
+ adapter
83
+ end
97
84
  end
98
85
 
99
86
  # kill the filewatcher thread
@@ -123,11 +110,7 @@ module Rerun
123
110
 
124
111
  already_examined = Hash.new()
125
112
 
126
- @directories.each do |directory|
127
- examine_files(directory.files(), already_examined)
128
- end
129
-
130
- examine_files(@files, already_examined) if not @files.empty?
113
+ examine_files(@directory.files, already_examined)
131
114
 
132
115
  # now diff the found files and the examined files to see if
133
116
  # something has been deleted
data/rerun.gemspec CHANGED
@@ -3,7 +3,7 @@ $spec = Gem::Specification.new do |s|
3
3
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
4
4
 
5
5
  s.name = 'rerun'
6
- s.version = '0.7.0.pre2'
6
+ s.version = '0.7.0.pre3'
7
7
 
8
8
  s.description = "Restarts your app when a file changes"
9
9
  s.summary = "Launches an app, and restarts it whenever the filesystem changes."
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rerun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0.pre2
4
+ version: 0.7.0.pre3
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-30 00:00:00.000000000 Z
12
+ date: 2012-07-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: listen
@@ -61,7 +61,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
61
  version: '0'
62
62
  segments:
63
63
  - 0
64
- hash: 1600005325287533273
64
+ hash: 3232457078342945302
65
65
  required_rubygems_version: !ruby/object:Gem::Requirement
66
66
  none: false
67
67
  requirements: