Class: RSpactor::Listener

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

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(valid_extensions = nil) ⇒ Listener

Returns a new instance of Listener.



9
10
11
12
13
14
15
16
17
18
# File 'lib/rspactor/listener.rb', line 9

def initialize(valid_extensions = nil)
  @valid_extensions = valid_extensions
  timestamp_checked
  
  @callback = lambda do |stream, ctx, num_events, paths, marks, event_ids|
    changed_files = extract_changed_files_from_paths(split_paths(paths, num_events))
    timestamp_checked
    yield changed_files unless changed_files.empty?
  end
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



7
8
9
# File 'lib/rspactor/listener.rb', line 7

def callback
  @callback
end

#last_checkObject (readonly)

Returns the value of attribute last_check.



7
8
9
# File 'lib/rspactor/listener.rb', line 7

def last_check
  @last_check
end

#valid_extensionsObject (readonly)

Returns the value of attribute valid_extensions.



7
8
9
# File 'lib/rspactor/listener.rb', line 7

def valid_extensions
  @valid_extensions
end

Instance Method Details

#extract_changed_files_from_paths(paths) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rspactor/listener.rb', line 54

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)


66
67
68
69
70
# File 'lib/rspactor/listener.rb', line 66

def file_changed?(file)
  File.stat(file).mtime > last_check
rescue Errno::ENOENT
  false
end

#file_extension(file) ⇒ Object



80
81
82
# File 'lib/rspactor/listener.rb', line 80

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

#ignore_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/rspactor/listener.rb', line 76

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

#ignore_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/rspactor/listener.rb', line 72

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

#run(directories) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rspactor/listener.rb', line 20

def run(directories)
  dirs = Array(directories)
  stream = OSX::FSEventStreamCreate(OSX::KCFAllocatorDefault, callback, nil, dirs, OSX::KFSEventStreamEventIdSinceNow, 0.5, 0)
  unless stream
    $stderr.puts "Failed to create stream"
    exit(1)
  end
  
  OSX::FSEventStreamScheduleWithRunLoop(stream, OSX::CFRunLoopGetCurrent(), OSX::KCFRunLoopDefaultMode)
  unless OSX::FSEventStreamStart(stream)
    $stderr.puts "Failed to start stream"
    exit(1)
  end
  
  begin
    OSX::CFRunLoopRun()
  rescue Interrupt
    OSX::FSEventStreamStop(stream)
    OSX::FSEventStreamInvalidate(stream)
    OSX::FSEventStreamRelease(stream)
  end
end

#split_paths(paths, num_events) ⇒ Object



47
48
49
50
51
52
# File 'lib/rspactor/listener.rb', line 47

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

#timestamp_checkedObject



43
44
45
# File 'lib/rspactor/listener.rb', line 43

def timestamp_checked
  @last_check = Time.now
end

#valid_extension?(file) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/rspactor/listener.rb', line 84

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