Sha256: 3aa0f98ae803f5639ec0db36079097cf8ecb70d5c5062877d73fc243e013478f

Contents?: true

Size: 1.65 KB

Versions: 13

Compression:

Stored size: 1.65 KB

Contents

# Some code borrowed from http://rails.aizatto.com/2007/11/28/taming-the-autotest-beast-with-fsevents/

class Listener
  
  def initialize(&block)
    require 'osx/foundation'
    begin
      @spec_run_time = Time.now
      callback = lambda do |stream, ctx, num_events, paths, marks, event_ids|
        changed_files = extract_changed_files_from_paths(split_paths(paths, num_events))        
        @spec_run_time = Time.now
        yield changed_files
      end

      OSX.require_framework '/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework'
      stream = OSX::FSEventStreamCreate(OSX::KCFAllocatorDefault, callback, nil, [Dir.pwd], OSX::KFSEventStreamEventIdSinceNow, 0.5, 0)
      unless stream
        puts "Failed to create stream"
        exit
      end

      OSX::FSEventStreamScheduleWithRunLoop(stream, OSX::CFRunLoopGetCurrent(), OSX::KCFRunLoopDefaultMode)
      unless OSX::FSEventStreamStart(stream)
        puts "Failed to start stream"
        exit 
      end

      OSX::CFRunLoopRun()
    rescue Interrupt
      OSX::FSEventStreamStop(stream)
      OSX::FSEventStreamInvalidate(stream)
      OSX::FSEventStreamRelease(stream)
    end
  end
  
  def split_paths(paths, num_events)
    paths.regard_as('*')
    rpaths = []        
    num_events.times { |i| rpaths << paths[i] }
    rpaths    
  end
  
  def extract_changed_files_from_paths(paths)
    changed_files = []
    paths.each do |path|
      Dir.glob(path + "*").each do |file|
        next if Inspector.file_is_invalid?(file)
        file_time = File.stat(file).mtime
        changed_files << file if file_time > @spec_run_time
      end
    end
    changed_files
  end
  
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
guillaumegentil-rspactor-0.2.1 lib/listener.rb
guillaumegentil-rspactor-0.2.2 lib/listener.rb
guillaumegentil-rspactor-0.2.3 lib/listener.rb
guillaumegentil-rspactor-0.2.4 lib/listener.rb
guillaumegentil-rspactor-0.2.5 lib/listener.rb
guillaumegentil-rspactor-0.2.6 lib/listener.rb
guillaumegentil-rspactor-0.2.7 lib/listener.rb
guillaumegentil-rspactor-0.2.8 lib/listener.rb
pelle-rspactor-0.2.2 lib/listener.rb
pelle-rspactor-0.2.3 lib/listener.rb
pelle-rspactor-0.2.4 lib/listener.rb
rspactor-0.0.2 lib/listener.rb
rspactor-0.2.0 lib/listener.rb