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)


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

Instance Attribute Details

#ignored_patternsArray<String>

Returns:

  • (Array<String>)


92
93
94
# File 'lib/epuber/compiler/file_finders/abstract.rb', line 92

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



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.

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



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.

Parameters:

  • paths (Array<String>)

    list of file paths

  • context_path (String)

Returns:

  • (Array<String>)

    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

Raises:



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

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



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

Parameters:

  • pattern (String)

    pattern of the desired files

  • groups (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



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.

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



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