Class: Pod::Sandbox::PathList
- Inherits:
-
Object
- Object
- Pod::Sandbox::PathList
- Defined in:
- lib/cocoapods/sandbox/path_list.rb
Overview
A PathList once it has generated the list of the paths this is updated only if explicitly requested by calling #read_file_system
The PathList class is designed to perform multiple glob matches against a given directory. Basically, it generates a list of all the children paths and matches the globs patterns against them, resulting in just one access to the file system.
Instance Attribute Summary collapse
-
#root ⇒ Pathname
The root of the list whose files and directories are used to perform the matching operations.
Globbing collapse
-
#glob(patterns, options = {}) ⇒ Array<Pathname>
Similar to #glob but returns the absolute paths.
-
#relative_glob(patterns, options = {}) ⇒ Array<Pathname>
The list of relative paths that are case insensitively matched by a given pattern.
Instance Method Summary collapse
-
#dirs ⇒ Array<String>
The list of absolute the path of all the directories contained in #root.
-
#files ⇒ Array<String>
The list of absolute the path of all the files contained in #root.
-
#initialize(root) ⇒ PathList
constructor
A new instance of PathList.
-
#read_file_system ⇒ void
Reads the file system and populates the files and paths lists.
Constructor Details
#initialize(root) ⇒ PathList
Returns a new instance of PathList.
20 21 22 |
# File 'lib/cocoapods/sandbox/path_list.rb', line 20 def initialize(root) @root = root end |
Instance Attribute Details
#root ⇒ Pathname
Returns The root of the list whose files and directories are used to perform the matching operations.
16 17 18 |
# File 'lib/cocoapods/sandbox/path_list.rb', line 16 def root @root end |
Instance Method Details
#dirs ⇒ Array<String>
Returns The list of absolute the path of all the directories contained in #root.
35 36 37 38 |
# File 'lib/cocoapods/sandbox/path_list.rb', line 35 def dirs read_file_system unless @dirs @dirs end |
#files ⇒ Array<String>
Returns The list of absolute the path of all the files contained in #root.
27 28 29 30 |
# File 'lib/cocoapods/sandbox/path_list.rb', line 27 def files read_file_system unless @files @files end |
#glob(patterns, options = {}) ⇒ Array<Pathname>
Returns Similar to #glob but returns the absolute paths.
67 68 69 |
# File 'lib/cocoapods/sandbox/path_list.rb', line 67 def glob(patterns, = {}) relative_glob(patterns, ).map { |p| root + p } end |
#read_file_system ⇒ void
This method returns an undefined value.
Returns Reads the file system and populates the files and paths lists.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/cocoapods/sandbox/path_list.rb', line 43 def read_file_system unless root.exist? raise Informative, "Attempt to read non existent folder `#{root}`." end root_length = root.to_s.length + 1 escaped_root = escape_path_for_glob(root) paths = Dir.glob(escaped_root + '**/*', File::FNM_DOTMATCH) absolute_dirs = paths.select { |path| File.directory?(path) } relative_dirs = absolute_dirs.map { |p| p[root_length..-1] } absolute_paths = paths.reject { |p| p == "#{root}/." || p == "#{root}/.." } relative_paths = absolute_paths.map { |p| p[root_length..-1] } @files = relative_paths - relative_dirs @dirs = relative_dirs.map { |d| d.gsub(/\/\.\.?$/, '') }.reject { |d| d == '.' || d == '..' } .uniq end |
#relative_glob(patterns, options = {}) ⇒ Array<Pathname>
Returns The list of relative paths that are case insensitively matched by a given pattern. This method emulates Dir#glob with the File::FNM_CASEFOLD option.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/cocoapods/sandbox/path_list.rb', line 82 def relative_glob(patterns, = {}) return [] if patterns.empty? dir_pattern = [:dir_pattern] exclude_patterns = [:exclude_patterns] include_dirs = [:include_dirs] if include_dirs full_list = files + dirs else full_list = files end list = Array(patterns).map do |pattern| if pattern.is_a?(String) if directory?(pattern) && dir_pattern pattern += '/' unless pattern.end_with?('/') pattern += dir_pattern end = dir_glob_equivalent_patterns(pattern) full_list.select do |path| .any? do |p| File.fnmatch(p, path, File::FNM_CASEFOLD | File::FNM_PATHNAME) end end else full_list.select { |path| path.match(pattern) } end end.flatten list = list.map { |path| Pathname.new(path) } if exclude_patterns = { :dir_pattern => '**/*', :include_dirs => include_dirs } list -= relative_glob(exclude_patterns, ) end list end |