guard 0.8.8 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. data/CHANGELOG.md +16 -1
  2. data/README.md +665 -293
  3. data/bin/fsevent_watch_guard +0 -0
  4. data/lib/guard.rb +66 -31
  5. data/lib/guard/cli.rb +9 -3
  6. data/lib/guard/dsl.rb +32 -5
  7. data/lib/guard/dsl_describer.rb +3 -2
  8. data/lib/guard/guard.rb +2 -2
  9. data/lib/guard/interactor.rb +179 -48
  10. data/lib/guard/listener.rb +32 -17
  11. data/lib/guard/listeners/darwin.rb +5 -9
  12. data/lib/guard/listeners/linux.rb +6 -10
  13. data/lib/guard/listeners/windows.rb +4 -2
  14. data/lib/guard/notifier.rb +171 -258
  15. data/lib/guard/notifiers/gntp.rb +114 -0
  16. data/lib/guard/notifiers/growl.rb +98 -0
  17. data/lib/guard/notifiers/growl_notify.rb +91 -0
  18. data/lib/guard/notifiers/libnotify.rb +96 -0
  19. data/lib/guard/notifiers/rb_notifu.rb +101 -0
  20. data/lib/guard/ui.rb +2 -2
  21. data/lib/guard/version.rb +1 -1
  22. data/lib/guard/watcher.rb +1 -1
  23. data/lib/vendor/darwin/Gemfile +6 -0
  24. data/lib/vendor/darwin/Guardfile +8 -0
  25. data/lib/vendor/darwin/LICENSE +20 -0
  26. data/lib/vendor/darwin/README.rdoc +254 -0
  27. data/lib/vendor/darwin/Rakefile +21 -0
  28. data/lib/vendor/darwin/ext/extconf.rb +61 -0
  29. data/lib/vendor/darwin/ext/fsevent/fsevent_watch.c +226 -0
  30. data/lib/vendor/darwin/lib/rb-fsevent.rb +2 -0
  31. data/lib/vendor/darwin/lib/rb-fsevent/fsevent.rb +105 -0
  32. data/lib/vendor/darwin/lib/rb-fsevent/version.rb +3 -0
  33. data/lib/vendor/darwin/rb-fsevent.gemspec +24 -0
  34. data/lib/vendor/darwin/spec/fixtures/folder1/file1.txt +0 -0
  35. data/lib/vendor/darwin/spec/fixtures/folder1/folder2/file2.txt +0 -0
  36. data/lib/vendor/darwin/spec/rb-fsevent/fsevent_spec.rb +75 -0
  37. data/lib/vendor/darwin/spec/spec_helper.rb +24 -0
  38. data/lib/vendor/linux/MIT-LICENSE +20 -0
  39. data/lib/vendor/linux/README.md +66 -0
  40. data/lib/vendor/linux/Rakefile +54 -0
  41. data/lib/vendor/linux/VERSION +1 -0
  42. data/lib/vendor/linux/lib/rb-inotify.rb +17 -0
  43. data/lib/vendor/linux/lib/rb-inotify/event.rb +139 -0
  44. data/lib/vendor/linux/lib/rb-inotify/native.rb +31 -0
  45. data/lib/vendor/linux/lib/rb-inotify/native/flags.rb +89 -0
  46. data/lib/vendor/linux/lib/rb-inotify/notifier.rb +308 -0
  47. data/lib/vendor/linux/lib/rb-inotify/watcher.rb +83 -0
  48. data/lib/vendor/linux/rb-inotify.gemspec +53 -0
  49. data/lib/vendor/windows/Gemfile +4 -0
  50. data/lib/vendor/windows/README.md +34 -0
  51. data/lib/vendor/windows/Rakefile +18 -0
  52. data/lib/vendor/windows/lib/rb-fchange.rb +14 -0
  53. data/lib/vendor/windows/lib/rb-fchange/event.rb +29 -0
  54. data/lib/vendor/windows/lib/rb-fchange/native.rb +45 -0
  55. data/lib/vendor/windows/lib/rb-fchange/native/flags.rb +78 -0
  56. data/lib/vendor/windows/lib/rb-fchange/notifier.rb +149 -0
  57. data/lib/vendor/windows/lib/rb-fchange/version.rb +3 -0
  58. data/lib/vendor/windows/lib/rb-fchange/watcher.rb +99 -0
  59. data/lib/vendor/windows/rb-fchange.gemspec +34 -0
  60. data/lib/vendor/windows/spec/fixtures/folder1/file1.txt +0 -0
  61. data/lib/vendor/windows/spec/fixtures/folder1/folder2/file2.txt +0 -0
  62. data/lib/vendor/windows/spec/rb-fchange/fchange_spec.rb +119 -0
  63. data/lib/vendor/windows/spec/spec_helper.rb +21 -0
  64. metadata +87 -22
  65. data/lib/guard/version.rbc +0 -180
@@ -56,7 +56,7 @@ module Guard
56
56
  def debug(message, options = { })
57
57
  unless ENV['GUARD_ENV'] == 'test'
58
58
  reset_line if options[:reset]
59
- STDERR.puts color("DEBUG (#{Time.now.strftime('%T')}): ", :yellow) + message if ::Guard.options && ::Guard.options[:debug]
59
+ STDERR.puts color("DEBUG (#{Time.now.strftime('%T')}): ", :yellow) + message if ::Guard.options && ::Guard.options[:verbose]
60
60
  end
61
61
  end
62
62
 
@@ -119,7 +119,7 @@ module Guard
119
119
  #
120
120
  # color('Hello World', :red, :bright)
121
121
  #
122
- # @param [String] the text to colorize
122
+ # @param [String] text the text to colorize
123
123
  # @param [Array] color_options the color options
124
124
  #
125
125
  def color(text, *color_options)
@@ -1,6 +1,6 @@
1
1
  module Guard
2
2
  unless defined? Guard::VERSION
3
3
  # The current gem version of Guard
4
- VERSION = '0.8.8'
4
+ VERSION = '0.9.0'
5
5
  end
6
6
  end
@@ -99,7 +99,7 @@ module Guard
99
99
 
100
100
  # Executes a watcher action.
101
101
  #
102
- # @param [String, MatchData] the matched path or the match from the Regex
102
+ # @param [String, MatchData] matches the matched path or the match from the Regex
103
103
  # @return [String] the final paths
104
104
  #
105
105
  def call_action(matches)
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "rake"
6
+
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at http://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r(^spec/(.*)_spec.rb))
6
+ watch(%r(^lib/(.*)\.rb)) { |m| "spec/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Thibaud Guillaume-Gentil
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,254 @@
1
+ = rb-fsevent
2
+
3
+ Very simple & usable Mac OSX FSEvents API
4
+
5
+ - RubyCocoa not required!
6
+ - Signals are working (really)
7
+ - Tested on MRI 1.8.7 & 1.9.2
8
+ - Tested on JRuby 1.6.3
9
+
10
+ == Install
11
+
12
+ gem install rb-fsevent
13
+
14
+ == Usage
15
+
16
+ === Singular path
17
+
18
+ require 'rb-fsevent'
19
+
20
+ fsevent = FSEvent.new
21
+ fsevent.watch Dir.pwd do |directories|
22
+ puts "Detected change inside: #{directories.inspect}"
23
+ end
24
+ fsevent.run
25
+
26
+ === Multiple paths
27
+
28
+ require 'rb-fsevent'
29
+
30
+ paths = ['/tmp/path/one', '/tmp/path/two', Dir.pwd]
31
+
32
+ fsevent = FSEvent.new
33
+ fsevent.watch paths do |directories|
34
+ puts "Detected change inside: #{directories.inspect}"
35
+ end
36
+ fsevent.run
37
+
38
+ === Multiple paths and additional options as a Hash
39
+
40
+ require 'rb-fsevent'
41
+
42
+ paths = ['/tmp/path/one', '/tmp/path/two', Dir.pwd]
43
+ options = {:latency => 1.5, :no_defer => true }
44
+
45
+ fsevent = FSEvent.new
46
+ fsevent.watch paths, options do |directories|
47
+ puts "Detected change inside: #{directories.inspect}"
48
+ end
49
+ fsevent.run
50
+
51
+ === Multiple paths and additional options as an Array
52
+
53
+ require 'rb-fsevent'
54
+
55
+ paths = ['/tmp/path/one', '/tmp/path/two', Dir.pwd]
56
+ options = ['--latency', 1.5, '--no-defer']
57
+
58
+ fsevent = FSEvent.new
59
+ fsevent.watch paths, options do |directories|
60
+ puts "Detected change inside: #{directories.inspect}"
61
+ end
62
+ fsevent.run
63
+
64
+ == Options
65
+
66
+ When defining options using a hash or hash-like object, it gets checked for validity and converted to the appropriate fsevent_watch commandline arguments array when the FSEvent class is instantiated. This is obviously the safest and preferred method of passing in options.
67
+
68
+ You may, however, choose to pass in an array of commandline arguments as your options value and it will be passed on, unmodified, to the fsevent_watch binary when called.
69
+
70
+ So far, the following options are supported...
71
+
72
+ - :latency => 0.5 # in seconds
73
+ - :no_defer => true
74
+ - :watch_root => true
75
+ - :since_when => 18446744073709551615 # an FSEventStreamEventId
76
+
77
+ === Latency
78
+
79
+ The :latency parameter determines how long the service should wait after the first event before passing that information along to the client. If your latency is set to 4 seconds, and 300 changes occur in the first three, then the callback will be fired only once. If latency is set to 0.1 in the exact same scenario, you will see that callback fire somewhere closer to between 25 and 30 times.
80
+
81
+ Setting a higher latency value allows for more effective temporal coalescing, resulting in fewer callbacks and greater overall efficiency... at the cost of apparent responsiveness. Setting this to a reasonably high value (and NOT setting :no_defer) is particularly well suited for background, daemon, or batch processing applications.
82
+
83
+ Implementation note: It appears that FSEvents will only coalesce events from a maximum of 32 distinct subpaths, making the above completely accurate only when events are to fewer than 32 subpaths. Creating 300 files in one directory, for example, or 30 files in 10 subdirectories, but not 300 files within 300 subdirectories. In the latter case, you may receive 31 callbacks in one go after the latency period. As this appears to be an implementation detail, the number could potentially differ across OS revisions. It is entirely possible that this number is somehow configurable, but I have not yet discovered an accepted method of doing so.
84
+
85
+ === NoDefer
86
+
87
+ The :no_defer option changes the behavior of the latency parameter completely. Rather than waiting for $latency period of time before sending along events in an attempt to coalesce a potential deluge ahead of time, that first event is sent along to the client immediately and is followed by a $latency period of silence before sending along any additional events that occurred within that period.
88
+
89
+ This behavior is particularly useful for interactive applications where that feeling of apparent responsiveness is most important, but you still don't want to get overwhelmed by a series of events that occur in rapid succession.
90
+
91
+ === WatchRoot
92
+
93
+ The :watch_root option allows for catching the scenario where you start watching "~/src/demo_project" and either it is later renamed to "~/src/awesome_sauce_3000" or the path changes in such a manner that the original directory is now at "~/clients/foo/iteration4/demo_project".
94
+
95
+ Unfortunately, while this behavior is somewhat supported in the fsevent_watch binary built as part of this project, support for passing across detailed metadata is not (yet). As a result, you would not receive the appropriate RootChanged event and be able to react appropriately. Also, since the C code doesn't open watched directories and retain that file descriptor as part of path-specific callback metadata, we are unable to issue an F_GETPATH fcntl() to determine the directory's new path.
96
+
97
+ Please do not use this option until proper support is added in an upcoming (planned) release.
98
+
99
+ === SinceWhen
100
+
101
+ The FSEventStreamEventId passed in to :since_when is used as a base for reacting to historic events. Unfortunately, not only is the metadata for transitioning from historic to live events not currently passed along, but it is incorrectly passed as a change event on the root path, and only per-host event streams are currently supported. When using per-host event streams, the event IDs are not guaranteed to be unique or contiguous when shared volumes (firewire/USB/net/etc) are used on multiple macs.
102
+
103
+ Please do not use this option until proper support is added in an upcoming (planned) release, unless it's acceptable for you to receive that one fake event that's handled incorrectly when events transition from historical to live. Even in that scenario, there's no metadata available for determining the FSEventStreamEventId of the last received event.
104
+
105
+ WARNING: passing in 0 as the parameter to :since_when will return events for every directory modified since "the beginning of time".
106
+
107
+ == Debugging output
108
+
109
+ If the gem is installed with the environment variable FWDEBUG set to the string "true", then fsevent_watch will be built with its various DEBUG sections defined, and the output to STDERR is truly verbose (and hopefully helpful in debugging your application and not just fsevent_watch itself). If enough people find this to be directly useful when developing code that makes use of rb-fsevent, then it wouldn't be hard to clean this up and make it a feature enabled by a commandline argument instead. Until somebody files an issue, however, I will assume otherwise.
110
+
111
+ append_path called for: /tmp/moo/cow/
112
+ resolved path to: /private/tmp/moo/cow
113
+
114
+ config.sinceWhen 18446744073709551615
115
+ config.latency 0.300000
116
+ config.flags 00000000
117
+ config.paths
118
+ /private/tmp/moo/cow
119
+
120
+ FSEventStreamRef @ 0x100108540:
121
+ allocator = 0x7fff705a4ee0
122
+ callback = 0x10000151e
123
+ context = {0, 0x0, 0x0, 0x0, 0x0}
124
+ numPathsToWatch = 1
125
+ pathsToWatch = 0x7fff705a4ee0
126
+ pathsToWatch[0] = '/private/tmp/moo/cow'
127
+ latestEventId = -1
128
+ latency = 300000 (microseconds)
129
+ flags = 0x00000000
130
+ runLoop = 0x0
131
+ runLoopMode = 0x0
132
+
133
+
134
+ FSEventStreamCallback fired!
135
+ numEvents: 32
136
+ event path: /private/tmp/moo/cow/1/a/
137
+ event flags: 00000000
138
+ event ID: 1023767
139
+ event path: /private/tmp/moo/cow/1/b/
140
+ event flags: 00000000
141
+ event ID: 1023782
142
+ event path: /private/tmp/moo/cow/1/c/
143
+ event flags: 00000000
144
+ event ID: 1023797
145
+ event path: /private/tmp/moo/cow/1/d/
146
+ event flags: 00000000
147
+ event ID: 1023812
148
+ event path: /private/tmp/moo/cow/1/e/
149
+ event flags: 00000000
150
+ event ID: 1023827
151
+ event path: /private/tmp/moo/cow/1/f/
152
+ event flags: 00000000
153
+ event ID: 1023842
154
+ event path: /private/tmp/moo/cow/1/g/
155
+ event flags: 00000000
156
+ event ID: 1023857
157
+ event path: /private/tmp/moo/cow/1/h/
158
+ event flags: 00000000
159
+ event ID: 1023872
160
+ event path: /private/tmp/moo/cow/1/i/
161
+ event flags: 00000000
162
+ event ID: 1023887
163
+ event path: /private/tmp/moo/cow/1/j/
164
+ event flags: 00000000
165
+ event ID: 1023902
166
+ event path: /private/tmp/moo/cow/1/k/
167
+ event flags: 00000000
168
+ event ID: 1023917
169
+ event path: /private/tmp/moo/cow/1/l/
170
+ event flags: 00000000
171
+ event ID: 1023932
172
+ event path: /private/tmp/moo/cow/1/m/
173
+ event flags: 00000000
174
+ event ID: 1023947
175
+ event path: /private/tmp/moo/cow/1/n/
176
+ event flags: 00000000
177
+ event ID: 1023962
178
+ event path: /private/tmp/moo/cow/1/o/
179
+ event flags: 00000000
180
+ event ID: 1023977
181
+ event path: /private/tmp/moo/cow/1/p/
182
+ event flags: 00000000
183
+ event ID: 1023992
184
+ event path: /private/tmp/moo/cow/1/q/
185
+ event flags: 00000000
186
+ event ID: 1024007
187
+ event path: /private/tmp/moo/cow/1/r/
188
+ event flags: 00000000
189
+ event ID: 1024022
190
+ event path: /private/tmp/moo/cow/1/s/
191
+ event flags: 00000000
192
+ event ID: 1024037
193
+ event path: /private/tmp/moo/cow/1/t/
194
+ event flags: 00000000
195
+ event ID: 1024052
196
+ event path: /private/tmp/moo/cow/1/u/
197
+ event flags: 00000000
198
+ event ID: 1024067
199
+ event path: /private/tmp/moo/cow/1/v/
200
+ event flags: 00000000
201
+ event ID: 1024082
202
+ event path: /private/tmp/moo/cow/1/w/
203
+ event flags: 00000000
204
+ event ID: 1024097
205
+ event path: /private/tmp/moo/cow/1/x/
206
+ event flags: 00000000
207
+ event ID: 1024112
208
+ event path: /private/tmp/moo/cow/1/y/
209
+ event flags: 00000000
210
+ event ID: 1024127
211
+ event path: /private/tmp/moo/cow/1/z/
212
+ event flags: 00000000
213
+ event ID: 1024142
214
+ event path: /private/tmp/moo/cow/1/
215
+ event flags: 00000000
216
+ event ID: 1024145
217
+ event path: /private/tmp/moo/cow/2/a/
218
+ event flags: 00000000
219
+ event ID: 1024160
220
+ event path: /private/tmp/moo/cow/2/b/
221
+ event flags: 00000000
222
+ event ID: 1024175
223
+ event path: /private/tmp/moo/cow/2/c/
224
+ event flags: 00000000
225
+ event ID: 1024190
226
+ event path: /private/tmp/moo/cow/2/d/
227
+ event flags: 00000000
228
+ event ID: 1024205
229
+ event path: /private/tmp/moo/cow/2/e/
230
+ event flags: 00000000
231
+ event ID: 1024220
232
+
233
+ == Note about FFI
234
+
235
+ rb-fsevent doesn't use {ruby-ffi}[http://github.com/ffi/ffi] anymore because it sadly doesn't allow for catching Signals. You can still see the code in the {ffi branch}[http://github.com/thibaudgg/rb-fsevent/tree/ffi].
236
+
237
+ == Development
238
+
239
+ - Source hosted at {GitHub}[http://github.com/thibaudgg/rb-fsevent]
240
+ - Report issues/Questions/Feature requests on {GitHub Issues}[http://github.com/thibaudgg/rb-fsevent/issues]
241
+
242
+ Pull requests are quite welcome! Please ensure that your commits are in a topic branch for each individual changeset than can be reasonably isolated. It is also important to ensure that your changes are well tested... whether that means new tests, modified tests, or fixing a scenario where the existing tests currently fail. If you have rvm and the required rubies currently installed, we have a helper task for running the testsuite in all of them:
243
+
244
+ rake spec:portability
245
+
246
+ The list of tested RVM targets is currently:
247
+
248
+ %w[1.8.6 1.8.7 1.9.2 jruby-head]
249
+
250
+ == Authors
251
+
252
+ - {Thibaud Guillaume-Gentil}[http://github.com/thibaudgg]
253
+ - {Travis Tilley}[http://github.com/ttilley]
254
+
@@ -0,0 +1,21 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task :default => :spec
7
+
8
+ namespace(:spec) do
9
+ desc "Run all specs on multiple ruby versions (requires rvm)"
10
+ task(:portability) do
11
+ %w[1.8.6 1.8.7 1.9.2 jruby-head].each do |version|
12
+ system <<-BASH
13
+ bash -c 'source ~/.rvm/scripts/rvm;
14
+ rvm #{version};
15
+ echo "--------- version #{version} ----------\n";
16
+ bundle install;
17
+ rake spec'
18
+ BASH
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,61 @@
1
+ # Workaround to make Rubygems believe it builds a native gem
2
+ require 'mkmf'
3
+ create_makefile('none')
4
+
5
+ # TODO: determine whether we really need to be working around instead of with mkmf
6
+
7
+ if `uname -s`.chomp != 'Darwin'
8
+ puts "Warning! Only Darwin (Mac OS X) systems are supported, nothing will be compiled"
9
+ else
10
+ begin
11
+ xcode_path = %x[xcode-select -print-path].to_s.strip!
12
+ rescue Errno::ENOENT
13
+ end
14
+
15
+ raise "Could not find a suitable Xcode installation" unless xcode_path
16
+
17
+ gem_root = File.expand_path(File.join('..'))
18
+ darwin_version = `uname -r`.to_i
19
+ sdk_version = { 9 => '10.5', 10 => '10.6', 11 => '10.7' }[darwin_version]
20
+
21
+ raise "Only Darwin systems greater than 8 (Mac OS X 10.5+) are supported" unless sdk_version
22
+
23
+ core_flags = %W{
24
+ -isysroot #{xcode_path}/SDKs/MacOSX#{sdk_version}.sdk
25
+ -mmacosx-version-min=#{sdk_version} -mdynamic-no-pic -std=gnu99
26
+ }
27
+
28
+ cflags = core_flags + %w{-Os -pipe}
29
+
30
+ wflags = %w{
31
+ -Wmissing-prototypes -Wreturn-type -Wmissing-braces -Wparentheses -Wswitch
32
+ -Wunused-function -Wunused-label -Wunused-parameter -Wunused-variable
33
+ -Wunused-value -Wuninitialized -Wunknown-pragmas -Wshadow
34
+ -Wfour-char-constants -Wsign-compare -Wnewline-eof -Wconversion
35
+ -Wshorten-64-to-32 -Wglobal-constructors -pedantic
36
+ }
37
+
38
+ ldflags = %w{
39
+ -dead_strip -framework CoreServices
40
+ }
41
+
42
+ cc_opts = core_flags + ldflags
43
+
44
+ cc_opts += %w{
45
+ -D DEBUG=true
46
+ } if ENV['FWDEBUG'] == "true"
47
+
48
+ cc_bin = `which clang || which gcc`.to_s.strip!
49
+
50
+ compile_command = "CFLAGS='#{cflags.join(' ')} #{wflags.join(' ')}' #{cc_bin} #{cc_opts.join(' ')} -o '#{gem_root}/bin/fsevent_watch' fsevent/fsevent_watch.c"
51
+
52
+ STDERR.puts(compile_command)
53
+
54
+ # Compile the actual fsevent_watch binary
55
+ system "mkdir -p #{File.join(gem_root, 'bin')}"
56
+ system compile_command
57
+
58
+ unless File.executable?("#{gem_root}/bin/fsevent_watch")
59
+ raise "Compilation of fsevent_watch failed (see README)"
60
+ end
61
+ end
@@ -0,0 +1,226 @@
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <unistd.h>
4
+
5
+ #include <CoreServices/CoreServices.h>
6
+
7
+
8
+ // Structure for storing metadata parsed from the commandline
9
+ static struct {
10
+ FSEventStreamEventId sinceWhen;
11
+ CFTimeInterval latency;
12
+ FSEventStreamCreateFlags flags;
13
+ CFMutableArrayRef paths;
14
+ } config = {
15
+ (UInt64) kFSEventStreamEventIdSinceNow,
16
+ (double) 0.3,
17
+ (UInt32) kFSEventStreamCreateFlagNone,
18
+ NULL
19
+ };
20
+
21
+ // Prototypes
22
+ static void append_path(const char *path);
23
+ static inline void parse_cli_settings(int argc, const char *argv[]);
24
+ static void callback(FSEventStreamRef streamRef,
25
+ void *clientCallBackInfo,
26
+ size_t numEvents,
27
+ void *eventPaths,
28
+ const FSEventStreamEventFlags eventFlags[],
29
+ const FSEventStreamEventId eventIds[]);
30
+
31
+
32
+ // Resolve a path and append it to the CLI settings structure
33
+ // The FSEvents API will, internally, resolve paths using a similar scheme.
34
+ // Performing this ahead of time makes things less confusing, IMHO.
35
+ static void append_path(const char *path)
36
+ {
37
+ #ifdef DEBUG
38
+ fprintf(stderr, "\n");
39
+ fprintf(stderr, "append_path called for: %s\n", path);
40
+ #endif
41
+
42
+ char fullPath[PATH_MAX];
43
+
44
+ if (realpath(path, fullPath) == NULL) {
45
+ #ifdef DEBUG
46
+ fprintf(stderr, " realpath not directly resolvable from path\n");
47
+ #endif
48
+
49
+ if (path[0] != '/') {
50
+ #ifdef DEBUG
51
+ fprintf(stderr, " passed path is not absolute\n");
52
+ #endif
53
+ size_t len;
54
+ getcwd(fullPath, sizeof(fullPath));
55
+ #ifdef DEBUG
56
+ fprintf(stderr, " result of getcwd: %s\n", fullPath);
57
+ #endif
58
+ len = strlen(fullPath);
59
+ fullPath[len] = '/';
60
+ strlcpy(&fullPath[len + 1], path, sizeof(fullPath) - (len + 1));
61
+ } else {
62
+ #ifdef DEBUG
63
+ fprintf(stderr, " assuming path does not YET exist\n");
64
+ #endif
65
+ strlcpy(fullPath, path, sizeof(fullPath));
66
+ }
67
+ }
68
+
69
+ #ifdef DEBUG
70
+ fprintf(stderr, " resolved path to: %s\n", fullPath);
71
+ fprintf(stderr, "\n");
72
+ fflush(stderr);
73
+ #endif
74
+
75
+ CFStringRef pathRef = CFStringCreateWithCString(kCFAllocatorDefault,
76
+ fullPath,
77
+ kCFStringEncodingUTF8);
78
+ CFArrayAppendValue(config.paths, pathRef);
79
+ CFRelease(pathRef);
80
+ }
81
+
82
+ // Parse commandline settings
83
+ static inline void parse_cli_settings(int argc, const char *argv[])
84
+ {
85
+ config.paths = CFArrayCreateMutable(NULL,
86
+ (CFIndex)0,
87
+ &kCFTypeArrayCallBacks);
88
+
89
+ for (int i = 1; i < argc; i++) {
90
+ if (strcmp(argv[i], "--since-when") == 0) {
91
+ config.sinceWhen = strtoull(argv[++i], NULL, 0);
92
+ } else if (strcmp(argv[i], "--latency") == 0) {
93
+ config.latency = strtod(argv[++i], NULL);
94
+ } else if (strcmp(argv[i], "--no-defer") == 0) {
95
+ config.flags |= kFSEventStreamCreateFlagNoDefer;
96
+ } else if (strcmp(argv[i], "--watch-root") == 0) {
97
+ config.flags |= kFSEventStreamCreateFlagWatchRoot;
98
+ } else if (strcmp(argv[i], "--ignore-self") == 0) {
99
+ #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
100
+ config.flags |= kFSEventStreamCreateFlagIgnoreSelf;
101
+ #else
102
+ fprintf(stderr, "MacOSX10.6.sdk is required for --ignore-self\n");
103
+ #endif
104
+ } else {
105
+ append_path(argv[i]);
106
+ }
107
+ }
108
+
109
+ if (CFArrayGetCount(config.paths) == 0) {
110
+ append_path(".");
111
+ }
112
+
113
+ #ifdef DEBUG
114
+ fprintf(stderr, "config.sinceWhen %llu\n", config.sinceWhen);
115
+ fprintf(stderr, "config.latency %f\n", config.latency);
116
+ fprintf(stderr, "config.flags %#.8x\n", config.flags);
117
+ fprintf(stderr, "config.paths\n");
118
+
119
+ long numpaths = CFArrayGetCount(config.paths);
120
+
121
+ for (long i = 0; i < numpaths; i++) {
122
+ char path[PATH_MAX];
123
+ CFStringGetCString(CFArrayGetValueAtIndex(config.paths, i),
124
+ path,
125
+ PATH_MAX,
126
+ kCFStringEncodingUTF8);
127
+ fprintf(stderr, " %s\n", path);
128
+ }
129
+
130
+ fprintf(stderr, "\n");
131
+ fflush(stderr);
132
+ #endif
133
+ }
134
+
135
+ static void callback(FSEventStreamRef streamRef,
136
+ void *clientCallBackInfo,
137
+ size_t numEvents,
138
+ void *eventPaths,
139
+ const FSEventStreamEventFlags eventFlags[],
140
+ const FSEventStreamEventId eventIds[])
141
+ {
142
+ char **paths = eventPaths;
143
+
144
+ #ifdef DEBUG
145
+ fprintf(stderr, "\n");
146
+ fprintf(stderr, "FSEventStreamCallback fired!\n");
147
+ fprintf(stderr, " numEvents: %lu\n", numEvents);
148
+
149
+ for (size_t i = 0; i < numEvents; i++) {
150
+ fprintf(stderr, " event path: %s\n", paths[i]);
151
+ fprintf(stderr, " event flags: %#.8x\n", eventFlags[i]);
152
+ fprintf(stderr, " event ID: %llu\n", eventIds[i]);
153
+ }
154
+
155
+ fprintf(stderr, "\n");
156
+ fflush(stderr);
157
+ #endif
158
+
159
+ for (size_t i = 0; i < numEvents; i++) {
160
+ fprintf(stdout, "%s", paths[i]);
161
+ fprintf(stdout, ":");
162
+ }
163
+
164
+ fprintf(stdout, "\n");
165
+ fflush(stdout);
166
+ }
167
+
168
+ int main(int argc, const char *argv[])
169
+ {
170
+ /*
171
+ * a subprocess will initially inherit the process group of its parent. the
172
+ * process group may have a control terminal associated with it, which would
173
+ * be the first tty device opened by the group leader. typically the group
174
+ * leader is your shell and the control terminal is your login device. a
175
+ * subset of signals triggered on the control terminal are sent to all members
176
+ * of the process group, in large part to facilitate sane and consistent
177
+ * cleanup (ex: control terminal was closed).
178
+ *
179
+ * so why the overly descriptive lecture style comment?
180
+ * 1. SIGINT and SIGQUIT are among the signals with this behavior
181
+ * 2. a number of applications gank the above for their own use
182
+ * 3. ruby's insanely useful "guard" is one of these applications
183
+ * 4. despite having some level of understanding of POSIX signals and a few
184
+ * of the scenarios that might cause problems, i learned this one only
185
+ * after reading ruby 1.9's process.c
186
+ * 5. if left completely undocumented, even slightly obscure bugfixes
187
+ * may be removed as cruft by a future maintainer
188
+ *
189
+ * hindsight is 20/20 addition: if you're single-threaded and blocking on IO
190
+ * with a subprocess, then handlers for deferrable signals might not get run
191
+ * when you expect them to. In the case of Ruby 1.8, that means making use of
192
+ * IO::select, which will preserve correct signal handling behavior.
193
+ */
194
+ if (setpgid(0,0) < 0) {
195
+ fprintf(stderr, "Unable to set new process group.\n");
196
+ return 1;
197
+ }
198
+
199
+ parse_cli_settings(argc, argv);
200
+
201
+ FSEventStreamContext context = {0, NULL, NULL, NULL, NULL};
202
+ FSEventStreamRef stream;
203
+ stream = FSEventStreamCreate(kCFAllocatorDefault,
204
+ (FSEventStreamCallback)&callback,
205
+ &context,
206
+ config.paths,
207
+ config.sinceWhen,
208
+ config.latency,
209
+ config.flags);
210
+
211
+ #ifdef DEBUG
212
+ FSEventStreamShow(stream);
213
+ fprintf(stderr, "\n");
214
+ fflush(stderr);
215
+ #endif
216
+
217
+ FSEventStreamScheduleWithRunLoop(stream,
218
+ CFRunLoopGetCurrent(),
219
+ kCFRunLoopDefaultMode);
220
+ FSEventStreamStart(stream);
221
+ CFRunLoopRun();
222
+ FSEventStreamFlushSync(stream);
223
+ FSEventStreamStop(stream);
224
+
225
+ return 0;
226
+ }