Class: Epuber::Compiler::FileFinders::Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/epuber/compiler/file_finders/abstract.rb

Direct Known Subclasses

Imaginary, Normal

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_path) ⇒ Abstract

Returns a new instance of Abstract.

Parameters:

  • source_path (String)


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.expand_path(@source_path).unicode_normalize.freeze
  @ignored_patterns = []
end

Instance Attribute Details

#ignored_patternsArray<String>

Returns:

  • (Array<String>)


94
95
96
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 94

def ignored_patterns
  @ignored_patterns
end

#source_pathString (readonly)

Returns path where should look for source files.

Returns:

  • (String)

    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.

Parameters:

  • paths (Array<String>)

    list of file paths

  • groups (Array<Symbol>)

    list of groups to filter file paths

Returns:

  • (Array<String>)

    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.

Parameters:

  • paths (Array<String>)

    list of file paths

  • context_path (String)

Returns:

  • (Array<String>)

    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

Parameters:

  • groups (Array<Symbol> | Symbol) (defaults to: nil)

Raises:



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

Parameters:

  • pattern (String)

    pattern of the desired files

  • groups (Array<Symbol>) (defaults to: nil)

    list of group names, nil or empty array for all groups, for valid values see GROUP_EXTENSIONS

  • context_path (String) (defaults to: @source_path_abs)

    path for root of searching, it is also defines start folder of relative path

Returns:

  • (Array<String>)

    list of founded files



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

Parameters:

  • pattern (String)

    pattern of the desired files

  • groups (Array<Symbol> | Symbol) (defaults to: nil)

    list of group names, nil or empty array for all groups, for valid values see GROUP_EXTENSIONS

  • context_path (String) (defaults to: nil)

    path for root of searching, it is also defines start folder of relative path

Returns:

  • (Array<String>)

    list of founded files



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.

Parameters:

  • pattern (String)

    pattern of the desired files

  • groups (Array<Symbol>) (defaults to: nil)

    list of group names, nil or empty array for all groups, for valid values see GROUP_EXTENSIONS

  • context_path (String) (defaults to: nil)

    path for root of searching, it is also defines start folder of relative path

Returns:

  • (Array<String>)

    list of founded files



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.expand_path(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