Sha256: 3420f84dd1b746fb455b5e81885fc1ba36ff336add1bc2ca24f6147a91c8c1ea

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

require 'archive/zip'

module Arli
  module Installers
    class ZipFile
      attr_accessor :lib, :lib_dir

      def initialize(lib:)
        self.lib     = lib
        self.lib_dir = lib.name.gsub(/ /, '_')
        raise 'Invalid URL for this installer: ' + lib.url unless lib.url =~ /\.zip$/i
      end

      def install
        download!

        remove_library!
        remove_library_versions!

        unzip(zip_archive, '.')

        dir = dirs_matching(lib_dir).first

        FileUtils.move(dir, lib_dir) if dir

        FileUtils.rm_f(zip_archive) if File.exist?(zip_archive)
      end

      def remove_library!
        FileUtils.rm_rf(lib_dir)
      end

      def remove_library_versions!
        dirs = dirs_matching(lib_dir)
        dirs.delete(lib_dir) # we don't want to delete the actual library
        dirs.each { |d| FileUtils.rm_f(d) }
      end

      private

      def dirs_matching(name)
        Dir.glob("#{name}*").select { |d| File.directory?(d)  }
      end

      def zip_archive
        @zip_archive ||= File.basename(lib.url)
      end

      def download!
        File.write(zip_archive, Net::HTTP.get(URI.parse(lib.url)))
      end

      # def unzip(file, destination)
      #   Archive::Zip.extract(file, destination, on_error: :skip)
      # end
      def unzip(file, destination)
        `unzip -o #{file} -d #{destination}`
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
arli-0.5.1 lib/arli/installers/zip_file.rb