Module: Homeconf::FileFinder Private

Defined in:
lib/homeconf/file_finder.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Common methods for finding files.

Class Method Summary collapse

Class Method Details

.homeconf_dirs(homeconf_dir) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



12
13
14
15
16
17
18
19
20
# File 'lib/homeconf/file_finder.rb', line 12

def self.homeconf_dirs(homeconf_dir)
  homeconf_dir = realpath(homeconf_dir)
  ignores = ignores(homeconf_dir)
  homeconf_dir.children
              .select(&:directory?)
              .reject { |p| ignores.include?(p.to_s) }
              .map(&:to_s)
              .sort
end

.homeconf_files(homeconf_dir) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



22
23
24
25
26
27
28
29
30
# File 'lib/homeconf/file_finder.rb', line 22

def self.homeconf_files(homeconf_dir)
  homeconf_dir = realpath(homeconf_dir)
  ignores = ignores(homeconf_dir)
  homeconf_dir.children
              .select(&:file?)
              .reject { |p| ignores.include?(p.to_s) }
              .map(&:to_s)
              .sort
end

.ignores(filepath) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/homeconf/file_finder.rb', line 32

def self.ignores(filepath)
  homeconf_dir = realpath(filepath)
  ignore_file = homeconf_dir.join(IGNORE_FILE)
  ignores = Set.new

  return ignores.to_a unless File.file?(ignore_file) && File.readable?(ignore_file)

  ignores.add ignore_file.to_s # implicitly ignore IGNORE_FILE

  ignore_globs = File.readlines(ignore_file, chomp: true).grep_v(/^\s*$/).grep_v(/^\s*#/)
  ignore_globs.each do |g|
    Dir.glob(homeconf_dir.join(g).to_s, File::FNM_DOTMATCH).each { |f| ignores.add(f) }
  end

  ignores
end

.realpath(filepath) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/homeconf/file_finder.rb', line 49

def self.realpath(filepath)
  begin
    realpath = Pathname.new(filepath).expand_path.realpath
  rescue Errno::ENOENT
    raise InvalidHomeconfDir.new("Does not exist. #{filepath}")
  rescue => e
    raise e
  end
  raise InvalidHomeconfDir.new("Not a directory. #{filepath}") unless realpath.directory?
  realpath
end