lib/fontist/import/recursive_extraction.rb in fontist-1.5.0 vs lib/fontist/import/recursive_extraction.rb in fontist-1.5.1

- old
+ new

@@ -2,15 +2,16 @@ require_relative "extractors" module Fontist module Import class RecursiveExtraction - BOTH_FONTS_PATTERN = "**/*.{ttf,otf,ttc}".freeze + BOTH_FONTS_PATTERN = "**/*.{ttf,otf,ttc}*".freeze ARCHIVE_EXTENSIONS = %w[zip msi exe cab].freeze - def initialize(archive) + def initialize(archive, subarchive: nil) @archive = archive + @subarchive = subarchive @operations = [] end def extension File.extname(filename(@archive)).sub(/^\./, "") @@ -55,10 +56,12 @@ extract_recursively(next_archive) end def operate_on_archive(archive) extractor = choose_extractor(archive) + Fontist.ui.say("Extracting #{archive} with #{extractor.class.name}") + save_operation(extractor) extractor.extract end # rubocop:disable Metrics/MethodLength @@ -85,12 +88,36 @@ fonts = Dir.glob(File.join(path, BOTH_FONTS_PATTERN)) !fonts.empty? end def find_archive(path) - Dir.children(path) - .map { |file_name| File.join(path, file_name) } - .max_by { |file_path| [file_type(file_path), File.size(file_path)] } + paths = Dir.children(path).map { |file| File.join(path, file) } + by_subarchive(paths) || by_size(paths) + end + + def by_subarchive(paths) + return unless @subarchive + + path_found = paths.detect do |path| + @subarchive == File.basename(path) + end + + return unless path_found + + save_operation_subarchive(path_found) + + path_found + end + + def save_operation_subarchive(path) + @operations.last[:options] ||= {} + @operations.last[:options][:subarchive] = File.basename(path) + end + + def by_size(paths) + paths.max_by do |path| + [file_type(path), File.size(path)] + end end def file_type(file_path) extension = File.extname(file_path).delete(".") ARCHIVE_EXTENSIONS.include?(extension) ? 1 : 0