lib/fontist/import/formula_builder.rb in fontist-1.4.0 vs lib/fontist/import/formula_builder.rb in fontist-1.5.0

- old
+ new

@@ -37,11 +37,11 @@ @both_fonts ||= group_fonts end def group_fonts files = (@font_files + @font_collection_files.map(&:fonts)).flatten - raise FontNotFoundError, "No font found" if files.empty? + raise Errors::FontNotFoundError, "No font found" if files.empty? files end def description @@ -53,13 +53,54 @@ end def resources filename = name.gsub(" ", "_") + "." + @extractor.extension - options = { urls: [@url] + (@options[:mirror] || []), - sha256: Digest::SHA256.file(@archive).to_s } + { filename => resource_options } + end - { filename => options } + def resource_options + urls = [] + sha = [] + downloads do |url, path| + urls << url + sha << Digest::SHA256.file(path).to_s + end + + sha = prepare_sha256(sha) + + { urls: urls, sha256: sha } + end + + def downloads + yield @url, @archive + + mirrors.each do |url| + path = download(url) + next unless path + + yield url, path + end + end + + def mirrors + @options[:mirror] || [] + end + + def download(url) + Fontist::Utils::Downloader.download(url, progress_bar: true).path + rescue Errors::InvalidResourceError + Fontist.ui.say("WARN: a mirror is not found '#{url}'") + nil + end + + def prepare_sha256(input) + output = input.uniq + return output.first if output.size == 1 + + checksums = output.join(", ") + Fontist.ui.say("WARN: SHA256 differs (#{checksums})") + output end def font_collections return if @font_collection_files.empty?