class Rerun::OSXWatcher

Attributes

last_check[R]
stream[R]
valid_extensions[R]

Public Instance Methods

extract_changed_files_from_paths(paths) click to toggle source
# File lib/rerun/osxwatcher.rb, line 63
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
file_changed?(file) click to toggle source
# File lib/rerun/osxwatcher.rb, line 75
def file_changed?(file)
  File.stat(file).mtime > last_check
end
file_extension(file) click to toggle source
# File lib/rerun/osxwatcher.rb, line 87
def file_extension(file)
  file =~ %r\.(\w+)$/ and $1
end
ignore_file?(file) click to toggle source
# File lib/rerun/osxwatcher.rb, line 83
def ignore_file?(file)
  File.basename(file).index('.') == 0 or not valid_extension?(file)
end
ignore_path?(path) click to toggle source
# File lib/rerun/osxwatcher.rb, line 79
def ignore_path?(path)
  path =~ %r(?:^|\/)\.(git|svn)/
end
split_paths(paths, num_events) click to toggle source
# File lib/rerun/osxwatcher.rb, line 56
def split_paths(paths, num_events)
  paths.regard_as('*')
  rpaths = []
  num_events.times { |i| rpaths << paths[i] }
  rpaths
end
start() click to toggle source
# File lib/rerun/osxwatcher.rb, line 11
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
stop() click to toggle source
# File lib/rerun/osxwatcher.rb, line 48
def stop
  @thread.kill
end
timestamp_checked() click to toggle source
# File lib/rerun/osxwatcher.rb, line 52
def timestamp_checked
  @last_check = Time.now
end
valid_extension?(file) click to toggle source
# File lib/rerun/osxwatcher.rb, line 91
def valid_extension?(file)
  valid_extensions.nil? or valid_extensions.include?(file_extension(file))
end