Skip to content

Instantly share code, notes, and snippets.

@mattetti
Created March 18, 2011 07:19
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattetti/875724 to your computer and use it in GitHub Desktop.
Save mattetti/875724 to your computer and use it in GitHub Desktop.
FSEvents API implementation in MacRuby
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
@andymorris
Copy link

I'm a complete noob with MacRuby trying to use this on a project. 'rvm install macruby' then 'macruby file.rb' doesn't work because KCFAllocatorDefault is undefined. Adding 'framework "Foundation"' fixes that, but then KFSEventStreamEventIdSinceNow is not defined, nor FSEventStreamCreate() and probably more. What am I missing?

@mattetti
Copy link
Author

mattetti commented May 11, 2011 via email

@andymorris
Copy link

Uninstalled through rvm, installed the .pkg from the website, but got the same result.

@lrz
Copy link

lrz commented May 11, 2011

Have you tried installing the latest BridgeSupport Preview 3 package too? (http://www.macruby.org/files/) It is possible that this is a BridgeSupport bug that we fixed since, as I remember we added fixes for FSEvents specifically.

@andymorris
Copy link

That was it, thanks both of you!

@ttilley
Copy link

ttilley commented Aug 25, 2011

I got really bored: ttilley/mrb-fsevent@b472717

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment