Sha256: 80687e354feed6074e220ce01c7b611f8cea033789f91bdd31ee0eddd61787f1

Contents?: true

Size: 1.44 KB

Versions: 7

Compression:

Stored size: 1.44 KB

Contents

require 'digest'
require 'openssl'

module RunLoop

  # Class for performing operations on directories.
  class Directory

    # Dir.glob ignores files that start with '.', but we often need to find
    # dotted files and directories.
    #
    # Ruby 2.* does the right thing by ignoring '..' and '.'.
    #
    # Ruby < 2.0 includes '..' and '.' in results which causes problems for some
    # of run-loop's internal methods.  In particular `reset_app_sandbox`.
    def self.recursive_glob_for_entries(base_dir)
      Dir.glob("#{base_dir}/{**/.*,**/*}").select do |entry|
        !(entry.end_with?('..') || entry.end_with?('.'))
      end
    end

    # Computes the digest of directory.
    #
    # @param path A path to a directory.
    # @raise ArgumentError When `path` is not a directory or path does not exist.
    def self.directory_digest(path)

      unless File.exist?(path)
        raise ArgumentError, "Expected '#{path}' to exist"
      end

      unless File.directory?(path)
        raise ArgumentError, "Expected '#{path}' to be a directory"
      end

      entries = self.recursive_glob_for_entries(path)

      if entries.empty?
        raise ArgumentError, "Expected a non-empty dir at '#{path}' found '#{entries}'"
      end

      sha = OpenSSL::Digest::SHA256.new
      self.recursive_glob_for_entries(path).each do |file|
        unless File.directory?(file)
          sha << File.read(file)
        end
      end
      sha.hexdigest
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
run_loop-1.5.1 lib/run_loop/directory.rb
run_loop-1.5.0 lib/run_loop/directory.rb
run_loop-1.4.1 lib/run_loop/directory.rb
run_loop-1.4.0 lib/run_loop/directory.rb
run_loop-1.3.3 lib/run_loop/directory.rb
run_loop-1.3.3.pre1 lib/run_loop/directory.rb
run_loop-1.3.2 lib/run_loop/directory.rb