Sha256: 7f0f343736ab8b37fde37a2f9441d9766abacee14b2faf31289b57dccd4b8deb

Contents?: true

Size: 1.78 KB

Versions: 229

Compression:

Stored size: 1.78 KB

Contents

module RGen

module Util

# WARNING: the mechanism of taking timestamps of directories in order to find out if the
# content has changed doesn't work reliably across all kinds of filesystems
#
class CachedGlob

  def initialize(dir_glob, file_glob)
    @dir_glob = dir_glob
    @file_glob = file_glob
    @root_dirs = []
    @dirs = {}
    @files = {}
    @timestamps = {}
  end

  # returns all files contained in directories matched by +dir_glob+ which match +file_glob+.
  # +file_glob+ must be relative to +dir_glob+.
  # dir_glob "*/a" with file_glob "**/*.txt" is basically equivalent with Dir.glob("*/a/**/*.txt")
  # the idea is that the file glob will only be re-eavluated when the content of one of the 
  # directories matched by dir_glob has changed.
  # this will only be faster than a normal Dir.glob if the number of dirs matched by dir_glob is
  # relatively large and changes in files affect only a few of them at a time.
  def glob
    root_dirs = Dir.glob(@dir_glob)
    (@root_dirs - root_dirs).each do |d|
      remove_root_dir(d)
    end
    (@root_dirs & root_dirs).each do |d|
      update_root_dir(d) if dir_changed?(d)
    end
    (root_dirs - @root_dirs).each do |d|
      update_root_dir(d)
    end
    @root_dirs = root_dirs
    @root_dirs.sort.collect{|d| @files[d]}.flatten
  end

  private

  def dir_changed?(dir)
    @dirs[dir].any?{|d| File.mtime(d) != @timestamps[dir][d]}
  end

  def update_root_dir(dir)
    @dirs[dir] = Dir.glob(dir+"/**/")
    @files[dir] = Dir.glob(dir+"/"+@file_glob)
    @timestamps[dir] = {}
    @dirs[dir].each do |d|
      @timestamps[dir][d] = File.mtime(d)
    end
  end

  def remove_root_dir(dir)
    @dirs.delete(dir)
    @files.delete(dir)
    @timestamps.delete(dir)
  end

end

end

end

Version data entries

229 entries across 229 versions & 3 rubygems

Version Path
rgen-0.10.2 lib/rgen/util/cached_glob.rb
rgen-0.10.0 lib/rgen/util/cached_glob.rb
rgen-0.9.1 ./lib/rgen/util/cached_glob.rb
rgen-0.8.3 lib/rgen/util/cached_glob.rb
rgen-0.8.4 lib/rgen/util/cached_glob.rb
rgen-0.9.0 lib/rgen/util/cached_glob.rb
puppet-retrospec-1.8.0 vendor/pup410/lib/puppet/vendor/rgen/lib/rgen/util/cached_glob.rb
puppet-retrospec-1.7.0 vendor/pup410/lib/puppet/vendor/rgen/lib/rgen/util/cached_glob.rb
puppet-4.10.12 lib/puppet/vendor/rgen/lib/rgen/util/cached_glob.rb
puppet-4.10.12-x86-mingw32 lib/puppet/vendor/rgen/lib/rgen/util/cached_glob.rb
puppet-4.10.12-x64-mingw32 lib/puppet/vendor/rgen/lib/rgen/util/cached_glob.rb
puppet-4.10.12-universal-darwin lib/puppet/vendor/rgen/lib/rgen/util/cached_glob.rb
puppet-4.10.11 lib/puppet/vendor/rgen/lib/rgen/util/cached_glob.rb
puppet-4.10.11-x86-mingw32 lib/puppet/vendor/rgen/lib/rgen/util/cached_glob.rb
puppet-4.10.11-x64-mingw32 lib/puppet/vendor/rgen/lib/rgen/util/cached_glob.rb
puppet-4.10.11-universal-darwin lib/puppet/vendor/rgen/lib/rgen/util/cached_glob.rb
puppet-4.10.10 lib/puppet/vendor/rgen/lib/rgen/util/cached_glob.rb
puppet-4.10.10-x86-mingw32 lib/puppet/vendor/rgen/lib/rgen/util/cached_glob.rb
puppet-4.10.10-x64-mingw32 lib/puppet/vendor/rgen/lib/rgen/util/cached_glob.rb
puppet-4.10.10-universal-darwin lib/puppet/vendor/rgen/lib/rgen/util/cached_glob.rb