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.
97 98 99 100 101 |
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 97 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>
92 93 94 |
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 92 def ignored_patterns @ignored_patterns end |
#source_path ⇒ String (readonly)
Returns path where should look for source files.
88 89 90 |
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 88 def source_path @source_path end |
Class Method Details
.group_filter_paths(paths, groups) ⇒ Array<String>
Returns filtered list of file paths.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 177 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)) end end end |
.relative_paths_from(paths, context_path) ⇒ Array<String>
Returns mapped list of file paths.
199 200 201 202 203 204 |
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 199 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
104 105 106 107 |
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 104 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
168 169 170 |
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 168 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
120 121 122 123 124 |
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 120 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.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 135 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 "You can't search from folder (#{searching_path}) that is not sub folder of the source_path (#{source_path}) expanded to (#{@source_path_abs})." 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 |