Class: RunLoop::Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/run_loop/directory.rb

Overview

Class for performing operations on directories.

Class Method Summary collapse

Class Method Details

.directory_digest(path) ⇒ Object

Computes the digest of directory.

Parameters:

  • path

    A path to a directory.

Raises:

  • ArgumentError When ‘path` is not a directory or path does not exist.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/run_loop/directory.rb', line 26

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

.recursive_glob_for_entries(base_dir) ⇒ Object

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`.



16
17
18
19
20
# File 'lib/run_loop/directory.rb', line 16

def self.recursive_glob_for_entries(base_dir)
  Dir.glob("#{base_dir}/{**/.*,**/*}").select do |entry|
    !(entry.end_with?('..') || entry.end_with?('.'))
  end
end