Sha256: 625e93fc1156eae94f7cfcb7b04132840b8fc48409f99bd3666dc6679dea6b3e

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require "zip"
require "pathname"

module Fontist
  module Utils
    module ZipExtractor
      # fonts_sub_dir is unused now, but formulas have this option
      # rubocop:disable Lint/UnusedMethodArgument
      def zip_extract(resource, download: true, fonts_sub_dir: "")
        zip_file = download_file(resource) if download
        zip_file ||= resource.urls.first

        Fontist.ui.say(%(Installing font "#{key}".))
        fonts_paths = unzip_fonts(zip_file)
        block_given? ? yield(fonts_paths) : fonts_paths
      end
      # rubocop:enable Lint/UnusedMethodArgument

      alias_method :unzip, :zip_extract

      private

      # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
      def unzip_fonts(file)
        Zip.on_exists_proc = true

        Array.new.tap do |fonts|
          Zip::File.open(file) do |zip_file|
            zip_file.glob("**/*.{ttf,ttc,otf}").each do |entry|
              if entry.name
                filename = Pathname.new(entry.name).basename
                font_path = fonts_path.join(filename.to_s)
                fonts.push(font_path.to_s)

                entry.extract(font_path)
              end
            end
          end
        end
      end
      # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fontist-1.6.0 lib/fontist/utils/zip_extractor.rb