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.
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_finder ⇒ FileFinders::Imaginary (readonly)
62 63 64 |
# File 'lib/epuber/compiler/file_resolver.rb', line 62 def dest_finder @dest_finder end |
#destination_path ⇒ String (readonly)
Returns 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 |
#files ⇒ Array<FileTypes::Abstract> (readonly)
Returns totally all files.
53 54 55 |
# File 'lib/epuber/compiler/file_resolver.rb', line 53 def files @files end |
#manifest_files ⇒ Array<FileTypes::Abstract> (readonly)
Returns all files that has to be in manifest file (OPF).
45 46 47 |
# File 'lib/epuber/compiler/file_resolver.rb', line 45 def manifest_files @manifest_files end |
#package_files ⇒ Array<FileTypes::Abstract> (readonly)
Returns all files that will be copied to EPUB package.
49 50 51 |
# File 'lib/epuber/compiler/file_resolver.rb', line 49 def package_files @package_files end |
#source_finder ⇒ FileFinders::Normal (readonly)
58 59 60 |
# File 'lib/epuber/compiler/file_resolver.rb', line 58 def source_finder @source_finder end |
#source_path ⇒ String (readonly)
Returns path where should look for source files.
32 33 34 |
# File 'lib/epuber/compiler/file_resolver.rb', line 32 def source_path @source_path end |
#spine_files ⇒ Array<FileTypes::Abstract> (readonly)
Returns all files that will be in spine.
41 42 43 |
# File 'lib/epuber/compiler/file_resolver.rb', line 41 def spine_files @spine_files 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 152 153 154 155 156 157 158 159 160 161 |
# 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 ... 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
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
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/epuber/compiler/file_resolver.rb', line 169 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
212 213 214 215 |
# File 'lib/epuber/compiler/file_resolver.rb', line 212 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
200 201 202 203 |
# File 'lib/epuber/compiler/file_resolver.rb', line 200 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
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/epuber/compiler/file_resolver.rb', line 221 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 |