Sha256: 30f1fe94abd67790a1bb144c20cccd69180f083dbb8d7069dcd68330eb3eb82c

Contents?: true

Size: 944 Bytes

Versions: 2

Compression:

Stored size: 944 Bytes

Contents

require "dry-configurable"
require "down"
require "fileutils"
require "zip"

module SudachiInstaller
  #
  # download and expand archive if it is zip archive
  #
  class Downloader
    extend Dry::Configurable

    #
    # @param [string] url
    # @param [string] filename
    # @param [string] dest_dir
    #
    def download(url, filename, dest_dir)
      FileUtils.mkdir_p(dest_dir)

      Down.download(url, destination: File.join(dest_dir, filename))
      expand(File.join(dest_dir, filename), force: true) if filename.match?(/\.zip\z/i)
    end

    #
    # @param [String] path
    # @param [boolean] force
    #
    def expand(path, force: false)
      dir = File.dirname(path)

      Zip::File.open(path) { |zip|
        zip.each { |entry|
          dest = File.join(dir, entry.name)
          FileUtils.rm_rf dest if File.exist?(dest) && force
          entry.extract(File.join(dir, entry.name))
        }
      }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sudachi-installer-0.1.1 lib/sudachi-installer/downloader.rb
sudachi-installer-0.1.0 lib/sudachi-installer/downloader.rb