Sha256: 87ac3f2950643b985da9df3bac4494ffda1ed8f56c2c987104d8da64493652cc

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

require 'facet/dir/self/recurse'

module Kernel

  # A Customized version of the autoreload from Facets. Also
  # checks template files, and keeps a dirty flag.
  
  def autoreload(check_interval=10, glob = '.')
    Thread.new(Time.now) do |start_time|
      check_dirty = lambda do |file,hash,file_mtime|
        if File.exists?(file) and (mtime = File.stat(file).mtime) > (file_mtime[file] || start_time)
          hash[file] = mtime 
        end
      end
      file_mtime = {}

      files = []
      Dir.recurse(glob) do |f|
        if f !~ /\/_darcs\// and f =~ /(.?)\.x(.?)/
          files << f 
        end
      end
      
      loop do
        need_reload = Hash.new
        sleep check_interval 
        $LOADED_FEATURES.each do |feature|
          check_dirty.call(feature,need_reload,file_mtime)
          $LOAD_PATH.each {|lp| check_dirty.call(File.join(lp, feature),need_reload,file_mtime)}
        end
                
        unless need_reload.empty?
          $autoreload_dirty = true
          need_reload.each_pair do |file,mtime|
            file_mtime[file] = mtime
            Logger.debug "File '#{ file }' reloaded" if $DBG
            begin
              load(file)
            rescue Exception => e
              Logger.info e.inspect
            end
          end
        end
        
        # Check for template files.
        
        files.each do |file|
          if File.exists?(file) and (mtime = File.stat(file).mtime) > (file_mtime[file] || start_time)
            $autoreload_dirty = true
            file_mtime[file] = mtime
            Logger.debug "File '#{ file }' changed" if $DBG
          end
        end
      
      end # loop
    
    end
  end

end

# Michael Neumann
# Tom Sawyer
# George Moschovitis <gm@navel.gr>

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
glue-0.31.0 lib/glue/autoreload.rb
glue-0.30.0 lib/glue/autoreload.rb