Created
March 18, 2011 07:19
-
-
Save mattetti/875724 to your computer and use it in GitHub Desktop.
FSEvents API implementation in MacRuby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if RUBY_ENGINE == 'macruby' | |
framework 'CoreServices' | |
WATCHED_EXTS = "rb,builder,erb,nokogiri" | |
PASSENGER_RESTART_FILE = File.expand_path(File.join(File.dirname(__FILE__), "..", "tmp", "restart.txt")) | |
DELAY = 3 | |
def modified_files(path) | |
Dir.glob("#{File.expand_path(path)}/*.{#{WATCHED_EXTS}}").map do |filename| | |
begin | |
[File.mtime(filename), filename] | |
rescue Errno::ENOENT | |
nil | |
end | |
end.compact.map { |mtime, filename| filename if mtime > Time.now - DELAY}.compact | |
end | |
callback = Proc.new do |stream, client_callback_info, number_of_events, paths_pointer, event_flags, event_ids| | |
# cast the pointer as a char string since it's defined as a void pointer. | |
paths_pointer.cast!('*') | |
number_of_events.times do |n| | |
unless modified_files(paths_pointer[n]).empty? | |
puts "#{modified_files(paths_pointer[n]).join(" ")} modified, telling passenger to reload" | |
# restart passenger by touching tmp/restart.txt | |
`touch #{PASSENGER_RESTART_FILE}` | |
end | |
end | |
end | |
paths = [File.expand_path(File.join(File.dirname(__FILE__), ".."))] | |
stream = FSEventStreamCreate(KCFAllocatorDefault, callback, nil, paths, KFSEventStreamEventIdSinceNow, 0.0, 0) | |
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), KCFRunLoopDefaultMode) | |
FSEventStreamStart(stream) | |
NSRunLoop.currentRunLoop.runUntilDate(NSDate.distantFuture) | |
else | |
puts "This reloader script only works in MacRuby." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That was it, thanks both of you!