Class: Epuber::Compiler::FileFinders::Abstract
- Inherits:
-
Object
- Object
- Epuber::Compiler::FileFinders::Abstract
- Defined in:
- lib/epuber/compiler/file_finders/abstract.rb
Instance Attribute Summary collapse
- #ignored_patterns ⇒ Array<String>
-
#source_path ⇒ String
readonly
Path where should look for source files.
Class Method Summary collapse
-
.group_filter_paths(paths, groups) ⇒ Array<String>
Filtered list of file paths.
-
.relative_paths_from(paths, context_path) ⇒ Array<String>
Mapped list of file paths.
Instance Method Summary collapse
- #assert_one_file(files, pattern: nil, groups: nil, context_path: nil) ⇒ Object
-
#find_all(pattern, groups: nil, context_path: @source_path_abs) ⇒ Array<String>
Looks for all files from #source_path recursively.
-
#find_file(pattern, groups: nil, context_path: nil, search_everywhere: true) ⇒ Array<String>
Method for finding file from context_path, if there is not matching file, it will continue to search from #source_path and after that it tries to search recursively from #source_path.
-
#find_files(pattern, groups: nil, context_path: nil, search_everywhere: true) ⇒ Array<String>
Method for finding files from context_path, if there is not matching file, it will continue to search from #source_path and after that it tries to search recursively from #source_path.
-
#initialize(source_path) ⇒ Abstract
constructor
A new instance of Abstract.
Constructor Details
#initialize(source_path) ⇒ Abstract
Returns a new instance of Abstract.
99 100 101 102 103 |
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 99 def initialize(source_path) @source_path = source_path.unicode_normalize.freeze @source_path_abs = ::File.(@source_path).unicode_normalize.freeze @ignored_patterns = [] end |
Instance Attribute Details
#ignored_patterns ⇒ Array<String>
94 95 96 |
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 94 def ignored_patterns @ignored_patterns end |
#source_path ⇒ String (readonly)
Returns path where should look for source files.
90 91 92 |
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 90 def source_path @source_path end |
Class Method Details
.group_filter_paths(paths, groups) ⇒ Array<String>
Returns filtered list of file paths.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 185 def self.group_filter_paths(paths, groups) # filter depend on group groups = Array(groups) if groups.empty? paths else valid_extensions = groups.map do |group| GROUP_EXTENSIONS.fetch(group) { raise ::StandardError, "Unknown file group #{group.inspect}" } end.flatten paths.select do |file_path| valid_extensions.include?(::File.extname(file_path).downcase) end end end |
.relative_paths_from(paths, context_path) ⇒ Array<String>
Returns mapped list of file paths.
207 208 209 210 211 212 |
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 207 def self.relative_paths_from(paths, context_path) context_pathname = Pathname.new(context_path) paths.map do |path| Pathname(path.unicode_normalize).relative_path_from(context_pathname).to_s end end |
Instance Method Details
#assert_one_file(files, pattern: nil, groups: nil, context_path: nil) ⇒ Object
107 108 109 110 |
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 107 def assert_one_file(files, pattern: nil, groups: nil, context_path: nil) raise FileNotFoundError.new(pattern, context_path) if files.empty? raise MultipleFilesFoundError.new(pattern, groups, context_path, files) if files.count >= 2 end |
#find_all(pattern, groups: nil, context_path: @source_path_abs) ⇒ Array<String>
Looks for all files from #source_path recursively
176 177 178 |
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 176 def find_all(pattern, groups: nil, context_path: @source_path_abs) __find_files("**/#{pattern}", groups, context_path) end |
#find_file(pattern, groups: nil, context_path: nil, search_everywhere: true) ⇒ Array<String>
Method for finding file from context_path, if there is not matching file, it will continue to search from #source_path and after that it tries to search recursively from #source_path.
When it founds too many (more than two) it will raise MultipleFilesFoundError
124 125 126 127 128 |
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 124 def find_file(pattern, groups: nil, context_path: nil, search_everywhere: true) files = find_files(pattern, groups: groups, context_path: context_path, search_everywhere: search_everywhere) assert_one_file(files, pattern: pattern, groups: groups, context_path: context_path) files.first end |
#find_files(pattern, groups: nil, context_path: nil, search_everywhere: true) ⇒ Array<String>
Method for finding files from context_path, if there is not matching file, it will continue to search from #source_path and after that it tries to search recursively from #source_path.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 140 def find_files(pattern, groups: nil, context_path: nil, search_everywhere: true) files = [] searching_path = context_path unless searching_path.nil? searching_path = ::File.(context_path, @source_path_abs) unless searching_path.start_with?(@source_path_abs) raise <<~ERROR You can't search from folder (#{searching_path}) that is not sub folder of the source_path (#{source_path}) expanded to (#{@source_path_abs}). ERROR end files = __find_files(pattern, groups, searching_path) end if files.empty? && context_path != source_path files = __find_files(pattern, groups, @source_path_abs, searching_path || @source_path_abs) end if files.empty? && search_everywhere && !pattern.start_with?('**') files = __find_files("**/#{pattern}", groups, @source_path_abs, searching_path || @source_path_abs) end files end |