Class: Bolt::Listeners::Osx
- Inherits:
-
Object
- Object
- Bolt::Listeners::Osx
- Defined in:
- lib/bolt/listeners/osx.rb
Instance Attribute Summary collapse
-
#callback ⇒ Object
readonly
Returns the value of attribute callback.
-
#last_check ⇒ Object
readonly
Returns the value of attribute last_check.
-
#valid_extensions ⇒ Object
readonly
Returns the value of attribute valid_extensions.
Class Method Summary collapse
Instance Method Summary collapse
- #extract_changed_files_from_paths(paths) ⇒ Object
- #file_changed?(file) ⇒ Boolean
- #file_extension(file) ⇒ Object
- #ignore_file?(file) ⇒ Boolean
- #ignore_path?(path) ⇒ Boolean
-
#initialize(valid_extensions = nil) ⇒ Osx
constructor
A new instance of Osx.
- #run(directories) ⇒ Object
- #split_paths(paths, num_events) ⇒ Object
- #timestamp_checked ⇒ Object
- #valid_extension?(file) ⇒ Boolean
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) @callback = lambda do |stream, ctx, num_events, paths, marks, event_ids| changed_files = extract_changed_files_from_paths(split_paths(paths, num_events)) yield changed_files unless changed_files.empty? end run(Dir.pwd) end |
Instance Attribute Details
#callback ⇒ Object (readonly)
Returns the value of attribute callback.
12 13 14 |
# File 'lib/bolt/listeners/osx.rb', line 12 def callback @callback end |
#last_check ⇒ Object (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_extensions ⇒ Object (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
.start ⇒ Object
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
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
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
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_checked ⇒ Object
66 67 68 |
# File 'lib/bolt/listeners/osx.rb', line 66 def @last_check = Time.now end |
#valid_extension?(file) ⇒ 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 |