Class: Epuber::Compiler::FileResolver
- Inherits:
-
Object
- Object
- Epuber::Compiler::FileResolver
- Defined in:
- lib/epuber/compiler/file_resolver.rb
Defined Under Namespace
Classes: ResolveError
Constant Summary collapse
- PATH_TYPES =
[:spine, :manifest, :package, nil]
Instance Attribute Summary collapse
- #dest_finder ⇒ FileFinders::Imaginary readonly
-
#destination_path ⇒ String
readonly
Path where will be stored result files.
-
#files ⇒ Array<FileTypes::Abstract>
readonly
Totally all files.
-
#manifest_files ⇒ Array<FileTypes::Abstract>
readonly
All files that has to be in manifest file (OPF).
-
#package_files ⇒ Array<FileTypes::Abstract>
readonly
All files that will be copied to EPUB package.
- #source_finder ⇒ FileFinders::Normal readonly
-
#source_path ⇒ String
readonly
Path where should look for source files.
-
#spine_files ⇒ Array<FileTypes::Abstract>
readonly
All files that will be in spine.
Instance Method Summary collapse
- #add_file(file) ⇒ Object
- #add_file_from_request(file_request, path_type = :manifest) ⇒ Object
-
#file_from_request(file_request) ⇒ FileTypes::AbstractFile+
Get instance of file from request instance.
-
#file_with_destination_path(path, path_type = :manifest) ⇒ FileTypes::AbstractFile
Method to get instance of file on specific path.
-
#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.
-
#initialize(source_path, destination_path) ⇒ FileResolver
constructor
A new instance of FileResolver.
-
#unneeded_files_in_destination ⇒ Array<String>
Method to find all files that should be deleted, because they are not in files in receiver.
Constructor Details
#initialize(source_path, destination_path) ⇒ FileResolver
Returns a new instance of FileResolver.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/epuber/compiler/file_resolver.rb', line 69 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_finder ⇒ FileFinders::Imaginary (readonly)
63 64 65 |
# File 'lib/epuber/compiler/file_resolver.rb', line 63 def dest_finder @dest_finder end |
#destination_path ⇒ String (readonly)
Returns path where will be stored result files.
37 38 39 |
# File 'lib/epuber/compiler/file_resolver.rb', line 37 def destination_path @destination_path end |
#files ⇒ Array<FileTypes::Abstract> (readonly)
Returns totally all files.
54 55 56 |
# File 'lib/epuber/compiler/file_resolver.rb', line 54 def files @files end |
#manifest_files ⇒ Array<FileTypes::Abstract> (readonly)
Returns all files that has to be in manifest file (OPF).
46 47 48 |
# File 'lib/epuber/compiler/file_resolver.rb', line 46 def manifest_files @manifest_files end |
#package_files ⇒ Array<FileTypes::Abstract> (readonly)
Returns all files that will be copied to EPUB package.
50 51 52 |
# File 'lib/epuber/compiler/file_resolver.rb', line 50 def package_files @package_files end |
#source_finder ⇒ FileFinders::Normal (readonly)
59 60 61 |
# File 'lib/epuber/compiler/file_resolver.rb', line 59 def source_finder @source_finder end |
#source_path ⇒ String (readonly)
Returns path where should look for source files.
33 34 35 |
# File 'lib/epuber/compiler/file_resolver.rb', line 33 def source_path @source_path end |
#spine_files ⇒ Array<FileTypes::Abstract> (readonly)
Returns all files that will be in spine.
42 43 44 |
# File 'lib/epuber/compiler/file_resolver.rb', line 42 def spine_files @spine_files end |
Instance Method Details
#add_file(file) ⇒ Object
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 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/epuber/compiler/file_resolver.rb', line 117 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 ... unless file.try(:file_request).nil? @request_to_files[file.file_request] << (existing_file || file) end # return existing file if already exists, new file will be thrown away return existing_file unless existing_file.nil? if [:spine].include?(type) @spine_files << file end if [:spine, :manifest].include?(type) @manifest_files << file end if [:spine, :manifest, :package].include?(type) @package_files << file end @files << file dest_finder.add_file(file.destination_path) @final_destination_path_to_file[file.final_destination_path] = file if file.respond_to?(:source_path) && !file.source_path.nil? @source_path_to_file[file.source_path] = file end if file.respond_to?(:abs_source_path) && !file.abs_source_path.nil? @abs_source_path_to_file[file.abs_source_path] = file end end |
#add_file_from_request(file_request, path_type = :manifest) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/epuber/compiler/file_resolver.rb', line 91 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
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/epuber/compiler/file_resolver.rb', line 170 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
213 214 215 216 |
# File 'lib/epuber/compiler/file_resolver.rb', line 213 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[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
201 202 203 204 |
# File 'lib/epuber/compiler/file_resolver.rb', line 201 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_destination ⇒ Array<String>
Method to find all files that should be deleted, because they are not in files in receiver
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/epuber/compiler/file_resolver.rb', line 222 def unneeded_files_in_destination requested_paths = files.map do |file| file.pkg_destination_path end existing_paths = FileFinders::Normal.new(destination_path).find_all('*') unnecessary_paths = existing_paths - requested_paths unnecessary_paths.select! do |path| !::File.directory?(File.join(destination_path, path)) end unnecessary_paths end |