Class: Nite::Owl::NiteOwl

Inherits:
Action
  • Object
show all
Includes:
Singleton
Defined in:
lib/nite/owl/niteowl.rb

Instance Attribute Summary

Attributes inherited from Action

#parent

Instance Method Summary collapse

Methods inherited from Action

#add, #after, #also, #call, call_all_deferred_actions, #cancel, #changes, #contains?, #created, #current_action, #defer, #delay, #deleted, #handle_delay, #if_not, #ignore, #modified, #only_if, #only_once, #remove, #renamed, #root, #run, #undefer

Constructor Details

#initializeNiteOwl

Returns a new instance of NiteOwl.



441
442
443
444
445
# File 'lib/nite/owl/niteowl.rb', line 441

def initialize
  super()
  @workers_thread = nil
  @queue = Queue.new
end

Instance Method Details

#eval_watch(dir, code) ⇒ Object



464
465
466
467
468
469
470
471
# File 'lib/nite/owl/niteowl.rb', line 464

def eval_watch(dir, code)
  Dir.chdir dir
  eval(code)
  if @actions.empty?
    puts "No actions configured in STDIN input stream"
    exit 1
  end
end

#startObject



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/nite/owl/niteowl.rb', line 477

def start
  @workers_thread = Thread.new {
    begin
      interval = 0.1
      event_interval = 0.5
      last_event_time = nil
      next_time = Time.now.to_f+interval
      events = {}
      while true
        until @queue.empty?
          event = @queue.pop(true) rescue nil
          if event
            name = event[0]
            flags = event[1]
            if events[name]
              new_flags = events[name] + flags
              if new_flags == [:delete, :create, :modify]
                new_flags = [:modify]
              end
              events[name] = new_flags
            else
              events[name] = flags
            end
            last_event_time = Time.now.to_f
          end
        end
        if last_event_time && Time.now.to_f >= (last_event_time+event_interval)
          events.each do |name,flags|
            begin
              Nite::Owl::NiteOwl.instance.call(name,flags.uniq)
            rescue Exception => e
              puts e.message
              puts e.backtrace
            end
          end
          events.clear
          last_event_time = nil
        end
        Action.call_all_deferred_actions()
        delay = next_time - Time.now.to_f
        if delay > 0
          sleep(delay)
        end
        next_time = Time.now.to_f+interval
      end
    rescue Exception => e
      puts e.message
      puts e.backtrace
    end
  }
  # start platform specific notifier
  pwd = Dir.pwd
  if RUBY_PLATFORM =~ /linux/
    require 'rb-inotify'
    puts "Nite Owl watching: #{pwd}"
    notifier = INotify::Notifier.new
    notifier.watch(pwd, :recursive, :modify, :create, :delete, :move) do |event|
      name = event.absolute_name.slice(pwd.size+1,event.absolute_name.size-pwd.size)
      flags = event.flags do |f|
        if f == :moved_to or f == :moved_from
          :rename
        else
          f
        end
      end
      @queue << [name,flags]
    end
    begin
      notifier.run
    rescue Interrupt => e
    end
  elsif RUBY_PLATFORM =~ /darwin/
    require 'rb-fsevent'
    puts "Nite Owl watching: #{pwd}"
    fsevent = FSEvent.new
    fsevent.watch pwd,{:file_events => true} do |paths, event_meta|
      event_meta['events'].each do |event|
        name = event['path']
        name = name.slice(pwd.size+1,name.size-pwd.size)
        flags = event['flags'].map do |f|
          if f == 'ItemCreated'
            :create
          elsif f == 'ItemModified'
            :modify
          elsif f == 'ItemRemoved'
            :delete
          elsif f == 'ItemRenamed'
            :rename
          end
        end.keep_if {|f| f}
        @queue << [name,flags]
      end
    end
    fsevent.run
  else
    puts "Platform unsupported"
    exit(1)
  end
end

#watch(dir) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/nite/owl/niteowl.rb', line 447

def watch(dir)
  Dir.chdir dir
  file = "Niteowl"
  unless File.file?(file) && File.readable?(file)
    file = "Niteowl.rb"
  end
  unless File.file?(file) && File.readable?(file)
    puts "No Niteowl[.rb] file found in: #{dir}"
    exit 1
  end
  load file
  if @actions.empty?
    puts "No actions configured in Niteowl file: #{dir}/#{file}"
    exit 1
  end
end

#whenever(files) ⇒ Object



473
474
475
# File 'lib/nite/owl/niteowl.rb', line 473

def whenever(files)
  add(NameIs.new(files))
end