Sha256: 2ecabe938b7a88d18c40b6a9980e1b490736b9a1bbaf74602a13c0626b10a479

Contents?: true

Size: 1.33 KB

Versions: 8

Compression:

Stored size: 1.33 KB

Contents

require_relative 'action'

module Arli
  module Actions
    class UnzipFile < Action
      description 'Downloads and unzip remote ZIP archives'
      check_command 'unzip -h'
      check_pattern 'extract files to pipe'

      def execute
        return if library.url.nil?
        return if library.url !~ /\.zip$/i

        download!
        move_in_place!
      rescue Exception => e
        fuck
        raise(e)
      ensure
        delete_zip! rescue nil
      end

      private

      def move_in_place!
        if File.exist?(zip_archive)
          FileUtils.rm_rf(top_dir_inside_zip) if top_dir_inside_zip
          unzip(zip_archive, '.')
          FileUtils.move(top_dir_inside_zip, dir) if Dir.exist?(top_dir_inside_zip)
        end
      end

      def delete_zip!
        FileUtils.rm_f(zip_archive) if File.exist?(zip_archive)
      end

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

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

      # list the contents of the archive and grab the top level folder
      def top_dir_inside_zip
        @zip_folder ||= `unzip -Z1 #{zip_archive} | awk 'BEGIN{FS="/"}{print $1}' | uniq | tail -1`.chomp
      end

      def unzip(file, destination)
        `unzip -o #{file} -d #{destination}`
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
arli-1.4.0 lib/arli/actions/unzip_file.rb
arli-1.3.0 lib/arli/actions/unzip_file.rb
arli-1.2.1 lib/arli/actions/unzip_file.rb
arli-1.1.0 lib/arli/actions/unzip_file.rb
arli-1.0.2 lib/arli/actions/unzip_file.rb
arli-1.0.1 lib/arli/actions/unzip_file.rb
arli-1.0.0 lib/arli/actions/unzip_file.rb
arli-0.9.0 lib/arli/actions/unzip_file.rb