Class: Epuber::Compiler::FileResolver

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

Defined Under Namespace

Classes: ResolveError

Constant Summary collapse

PATH_TYPES =
[:spine, :manifest, :package, nil].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_path, destination_path) ⇒ FileResolver

Returns a new instance of FileResolver.

Parameters:

  • source_path (String)
  • destination_path (String)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/epuber/compiler/file_resolver.rb', line 68

def initialize(source_path, destination_path)
  @source_finder = FileFinders::Normal.new(source_path.unicode_normalize)
  @source_finder.ignored_patterns << ::File.join(Config::WORKING_PATH, '**')

  @dest_finder = FileFinders::Imaginary.new(destination_path.unicode_normalize)

  @source_path      = source_path.unicode_normalize
  @destination_path = destination_path.unicode_normalize

  @spine_files    = []
  @manifest_files = []
  @package_files  = []
  @files          = []

  @request_to_files = Hash.new { |hash, key| hash[key] = [] }
  @final_destination_path_to_file = {}
  @source_path_to_file = {}
  @abs_source_path_to_file = {}
end

Instance Attribute Details

#dest_finderFileFinders::Imaginary (readonly)



62
63
64
# File 'lib/epuber/compiler/file_resolver.rb', line 62

def dest_finder
  @dest_finder
end

#destination_pathString (readonly)

Returns path where will be stored result files.

Returns:

  • (String)

    path where will be stored result files



36
37
38
# File 'lib/epuber/compiler/file_resolver.rb', line 36

def destination_path
  @destination_path
end

#filesArray<Epuber::Compiler::FileTypes::AbstractFile> (readonly)

Returns totally all files.

Returns:



53
54
55
# File 'lib/epuber/compiler/file_resolver.rb', line 53

def files
  @files
end

#manifest_filesArray<Epuber::Compiler::FileTypes::AbstractFile> (readonly)

Returns all files that has to be in manifest file (OPF).

Returns:



45
46
47
# File 'lib/epuber/compiler/file_resolver.rb', line 45

def manifest_files
  @manifest_files
end

#package_filesArray<Epuber::Compiler::FileTypes::AbstractFile> (readonly)

Returns all files that will be copied to EPUB package.

Returns:



49
50
51
# File 'lib/epuber/compiler/file_resolver.rb', line 49

def package_files
  @package_files
end

#source_finderFileFinders::Normal (readonly)

Returns:



58
59
60
# File 'lib/epuber/compiler/file_resolver.rb', line 58

def source_finder
  @source_finder
end

#source_pathString (readonly)

Returns path where should look for source files (relative to project root).

Returns:

  • (String)

    path where should look for source files (relative to project root)



32
33
34
# File 'lib/epuber/compiler/file_resolver.rb', line 32

def source_path
  @source_path
end

#spine_filesArray<Epuber::Compiler::FileTypes::AbstractFile> (readonly)

Returns all files that will be in spine.

Returns:



41
42
43
# File 'lib/epuber/compiler/file_resolver.rb', line 41

def spine_files
  @spine_files
end

Class Method Details

.file_class_for(extname) ⇒ Class

Parameters:

  • extname (String)

    extension of file

Returns:

  • (Class)


247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/epuber/compiler/file_resolver.rb', line 247

def file_class_for(extname)
  mapping = {
    '.styl' => FileTypes::StylusFile,
    '.css' => FileTypes::CSSFile,

    '.coffee' => FileTypes::CoffeeScriptFile,

    '.bade' => FileTypes::BadeFile,
    '.xhtml' => FileTypes::XHTMLFile,
    '.html' => FileTypes::XHTMLFile,

    '.jpg' => FileTypes::ImageFile,
    '.jpeg' => FileTypes::ImageFile,
    '.png' => FileTypes::ImageFile,
  }

  mapping[extname] || FileTypes::StaticFile
end

.path_comps_for(root_path, path_type) ⇒ Array<String>

Returns path components.

Parameters:

  • root_path (String)

    path to root of the package

  • path_type (Symbol)

    path type of file

Returns:

  • (Array<String>)

    path components



271
272
273
274
275
276
277
278
# File 'lib/epuber/compiler/file_resolver.rb', line 271

def path_comps_for(root_path, path_type)
  case path_type
  when :spine, :manifest
    Array(root_path) + [Compiler::EPUB_CONTENT_FOLDER]
  when :package
    Array(root_path)
  end
end

.renamed_file_with_path(path) ⇒ String

Returns path with changed extension.

Parameters:

  • path (String)

    path to some file

Returns:

  • (String)

    path with changed extension



232
233
234
235
236
237
238
239
240
241
# File 'lib/epuber/compiler/file_resolver.rb', line 232

def renamed_file_with_path(path)
  extname     = File.extname(path)
  new_extname = FileFinders::EXTENSIONS_RENAME[extname]

  if new_extname.nil?
    path
  else
    path.sub(/#{Regexp.escape(extname)}$/, new_extname)
  end
end

Instance Method Details

#add_file(file) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/epuber/compiler/file_resolver.rb', line 116

def add_file(file)
  type = file.path_type

  unless PATH_TYPES.include?(type)
    raise "Unknown file.path_type #{type.inspect}, expected are :spine, :manifest, :package or nil"
  end

  resolve_destination_path(file)

  existing_file = @final_destination_path_to_file[file.final_destination_path]

  # save mapping from file_request to file, file_request can be different, but result file could be the same ...
  @request_to_files[file.file_request] << (existing_file || file) unless file.try(:file_request).nil?

  # return existing file if already exists, new file will be thrown away
  return existing_file unless existing_file.nil?

  @spine_files << file if [:spine].include?(type)

  @manifest_files << file if %i[spine manifest].include?(type)

  @package_files << file if %i[spine manifest package].include?(type)

  @files << file


  dest_finder.add_file(file.destination_path)

  @final_destination_path_to_file[file.final_destination_path] = file

  @source_path_to_file[file.source_path] = file if file.respond_to?(:source_path) && !file.source_path.nil?

  return unless file.respond_to?(:abs_source_path) && !file.abs_source_path.nil?

  @abs_source_path_to_file[file.abs_source_path] = file
end

#add_file_from_request(file_request, path_type = :manifest) ⇒ Object

Parameters:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/epuber/compiler/file_resolver.rb', line 90

def add_file_from_request(file_request, path_type = :manifest)
  if file_request.only_one
    file_path = @source_finder.find_file(file_request.source_pattern, groups: file_request.group)
    file_class = self.class.file_class_for(File.extname(file_path))

    file = file_class.new(file_path)
    file.file_request = file_request
    file.path_type = path_type

    add_file(file)
  else
    file_paths = @source_finder.find_all(file_request.source_pattern, groups: file_request.group)
    file_paths.map do |path|
      file_class = self.class.file_class_for(File.extname(path))

      file = file_class.new(path)
      file.file_request = file_request
      file.path_type = path_type

      add_file(file)
    end
  end
end

#file_from_request(file_request) ⇒ FileTypes::AbstractFile+

Get instance of file from request instance

Parameters:

Returns:



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/epuber/compiler/file_resolver.rb', line 159

def file_from_request(file_request)
  files = @request_to_files[file_request]

  if files.empty? && file_request.only_one
    begin
      path  = @source_finder.find_file(file_request.source_pattern, groups: file_request.group)
      file  = file_with_source_path(path)

      unless file.nil?
        @request_to_files[file_request] = file
        files = [file]
      end
    rescue FileFinders::FileNotFoundError, FileFinders::MultipleFilesFoundError
      # noop
    end
  end

  if file_request.only_one
    files.first # @request_to_files always returns array, see #initialize method
  else
    files
  end
end

#file_with_destination_path(path, path_type = :manifest) ⇒ FileTypes::AbstractFile

Method to get instance of file on specific path

Parameters:

  • path (String)

    path to file from destination folder

  • path_type (String) (defaults to: :manifest)

    path type of path (:spine, :manifest or :package)

Returns:



202
203
204
205
# File 'lib/epuber/compiler/file_resolver.rb', line 202

def file_with_destination_path(path, path_type = :manifest)
  final_path = File.join(*self.class.path_comps_for(destination_path, path_type), path.unicode_normalize)
  @final_destination_path_to_file[path] || @final_destination_path_to_file[final_path]
end

#file_with_source_path(source_path) ⇒ FileTypes::AbstractFile

Get instance of file from source path, but this is relative path from project root, if you want to find file with absolute source path see #file_with_abs_source_path

Parameters:

  • source_path (String)

Returns:



190
191
192
193
# File 'lib/epuber/compiler/file_resolver.rb', line 190

def file_with_source_path(source_path)
  source_path = source_path.unicode_normalize
  @source_path_to_file[source_path] || @abs_source_path_to_file[source_path]
end

#unneeded_files_in_destinationArray<String>

Method to find all files that should be deleted, because they are not in files in receiver

Returns:

  • (Array<String>)

    list of files that should be deleted in destination directory



211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/epuber/compiler/file_resolver.rb', line 211

def unneeded_files_in_destination
  requested_paths = files.map(&:pkg_destination_path)

  existing_paths = FileFinders::Normal.new(destination_path).find_all('*')

  unnecessary_paths = existing_paths - requested_paths

  unnecessary_paths.reject! do |path|
    ::File.directory?(File.join(destination_path, path))
  end

  unnecessary_paths
end