Sha256: c64dde4081cbc076fef8794ff7bb796c01b5a84e31992d8e3759dd37fc1e4428

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

module Stack
  class Watcher
    attr_accessor :source, :target
    attr_accessor :generator
    attr_accessor :directory_watcher
    attr_accessor :keep_alive, :is_first_observe
    attr_accessor :thread
    
    def initialize(generator)
      self.generator = generator
      self.source = self.generator.source
      self.target = self.generator.target
      
      self.keep_alive = true
      
      require 'directory_watcher'
    end
    
    def observe
      dirs = ""
      
      Dir.chdir(self.source) do
        dirs = Dir['*'].select { |x| File.directory?(x) }
        dirs -= ['_stack']
        dirs = dirs.map { |x| "#{x}/**/*" }
        dirs += ['*']
      end
      
      self.directory_watcher = DirectoryWatcher.new(self.source)
      self.directory_watcher.interval = 1
      self.directory_watcher.glob = dirs
      
      self.directory_watcher.add_observer do |*args|
        if self.is_first_observe
          self.is_first_observe = false
        else
          time = Time.now.strftime("%Y-%m-%d %H:%M:%S")
          puts "[#{time}] #{args.size} files changed."
          self.generator.process!
        
          self.generator.transform!
        
          time = Time.now.strftime("%Y-%m-%d %H:%M:%S")
          puts "[#{time}] #{args.size} files processed."
        end
      end
      
      #self.directory_watcher.start
      
      self.thread = Thread.new {
        self.directory_watcher.start
      }
      
      trap("INT") { self.directory_watcher.stop }
      
      self.thread.join()
      
      #if self.keep_alive
      #  loop { sleep 1000 }
      #end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stack-0.0.9 lib/stack/watcher.rb
stack-0.0.8 lib/stack/watcher.rb