Class: Bolt::Listeners::Osx

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/listeners/osx.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(valid_extensions = nil) ⇒ Osx

Returns a new instance of Osx.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bolt/listeners/osx.rb', line 14

def initialize(valid_extensions = nil)
  @valid_extensions = %w(rb erb builder haml rhtml rxml yml conf opts)
  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
  run(Dir.pwd)
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



12
13
14
# File 'lib/bolt/listeners/osx.rb', line 12

def callback
  @callback
end

#last_checkObject (readonly)

Returns the value of attribute last_check.



12
13
14
# File 'lib/bolt/listeners/osx.rb', line 12

def last_check
  @last_check
end

#valid_extensionsObject (readonly)

Returns the value of attribute valid_extensions.



12
13
14
# File 'lib/bolt/listeners/osx.rb', line 12

def valid_extensions
  @valid_extensions
end

Class Method Details

.startObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bolt/listeners/osx.rb', line 26

def self.start
  begin
    require 'osx/foundation'         
  rescue LoadError
    puts "** Could not load osx/foundation. RubyCocoa not installed? Falling back to Bolt::Listeners::Generic" if Bolt['verbose']
    return Bolt::Listeners::Generic.new
  end
  
  begin
    OSX.require_framework '/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework'
    return self.new
  rescue NameError
    puts "** There was an error loading Bolt::Listeners::Osx. Falling back to Bolt::Listeners::Generic" if Bolt['verbose']
    return Bolt::Listeners::Generic.new
  end
end

Instance Method Details

#extract_changed_files_from_paths(paths) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bolt/listeners/osx.rb', line 77

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)


89
90
91
92
93
# File 'lib/bolt/listeners/osx.rb', line 89

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

#file_extension(file) ⇒ Object



103
104
105
# File 'lib/bolt/listeners/osx.rb', line 103

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

#ignore_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/bolt/listeners/osx.rb', line 99

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

#ignore_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/bolt/listeners/osx.rb', line 95

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

#run(directories) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bolt/listeners/osx.rb', line 43

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



70
71
72
73
74
75
# File 'lib/bolt/listeners/osx.rb', line 70

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

#timestamp_checkedObject



66
67
68
# File 'lib/bolt/listeners/osx.rb', line 66

def timestamp_checked
  @last_check = Time.now
end

#valid_extension?(file) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/bolt/listeners/osx.rb', line 107

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