Sha256: 35330efb52ec8000dd372aa12ef168c368ed36a84afc9ef2a59b2b7256780769

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 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
            STDERR.puts "File '#{ file }' reloaded"
            begin
              load(file)
            rescue Exception => e
              STDERR.puts 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
            STDERR.puts "File '#{ file }' changed"
          end
        end
      
      end # loop
    
    end
  end

end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
glue-0.28.0 lib/glue/autoreload.rb