Class: Rerun::OSXWatcher

Inherits:
Watcher show all
Defined in:
lib/osxwatcher.rb

Constant Summary

Constants inherited from Watcher

Watcher::CREATED, Watcher::DELETED, Watcher::MODIFIED

Instance Attribute Summary collapse

Attributes inherited from Watcher

#directories, #priority, #sleep_time

Instance Method Summary collapse

Methods inherited from Watcher

#add_directory, #add_file, #initialize, #join, #prime, #remove_directory, #remove_file

Constructor Details

This class inherits a constructor from Rerun::Watcher

Instance Attribute Details

#last_checkObject (readonly)

Returns the value of attribute last_check.



18
19
20
# File 'lib/osxwatcher.rb', line 18

def last_check
  @last_check
end

#streamObject (readonly)

Returns the value of attribute stream.



19
20
21
# File 'lib/osxwatcher.rb', line 19

def stream
  @stream
end

#valid_extensionsObject (readonly)

Returns the value of attribute valid_extensions.



18
19
20
# File 'lib/osxwatcher.rb', line 18

def valid_extensions
  @valid_extensions
end

Instance Method Details

#extract_changed_files_from_paths(paths) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/osxwatcher.rb', line 73

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) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/osxwatcher.rb', line 85

def file_changed?(file)
  File.stat(file).mtime > last_check
end

#file_extension(file) ⇒ Object



97
98
99
# File 'lib/osxwatcher.rb', line 97

def file_extension(file)
  file =~ /\.(\w+)$/ and $1
end

#ignore_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/osxwatcher.rb', line 93

def ignore_file?(file)
  File.basename(file).index('.') == 0 or not valid_extension?(file)
end

#ignore_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/osxwatcher.rb', line 89

def ignore_path?(path)
  path =~ /(?:^|\/)\.(git|svn)/
end

#split_paths(paths, num_events) ⇒ Object



66
67
68
69
70
71
# File 'lib/osxwatcher.rb', line 66

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

#startObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/osxwatcher.rb', line 21

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

#stopObject



58
59
60
# File 'lib/osxwatcher.rb', line 58

def stop
  @thread.kill
end

#timestamp_checkedObject



62
63
64
# File 'lib/osxwatcher.rb', line 62

def timestamp_checked
  @last_check = Time.now
end

#valid_extension?(file) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/osxwatcher.rb', line 101

def valid_extension?(file)
  valid_extensions.nil? or valid_extensions.include?(file_extension(file))
end