Class: Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/listener.rb

Overview

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Listener

Returns a new instance of Listener.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/listener.rb', line 5

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

Instance Method Details

#extract_changed_files_from_paths(paths) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/listener.rb', line 43

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

#split_paths(paths, num_events) ⇒ Object



36
37
38
39
40
41
# File 'lib/listener.rb', line 36

def split_paths(paths, num_events)
  paths.regard_as('*')
  rpaths = []        
  num_events.times { |i| rpaths << paths[i] }
  rpaths    
end