Sha256: 5885b43a824f26e7e6d025d3b84bf11c632b9a570c526978240a4e9b01dc1577

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

require "open-uri"

module Jets::Gems::Extract
  class Base
    class NotFound < RuntimeError; end

    attr_reader :project_root
    def initialize(name, options = {})
      @name = name
      @options = options
      ruby_folder = Jets::Gems.ruby_folder
      @downloads_root = options[:downloads_root] || "/tmp/jets/#{Jets.config.project_name}/serverlessgems/#{ruby_folder}"
    end

    def clean_downloads(folder)
      path = "#{@downloads_root}/downloads/#{folder}"
      say "Removing cache: #{path}"
      FileUtils.rm_rf(path)
    end

    # Using ` > /dev/null 2>&1` to suppress stderr message:
    #
    #    lchmod (file attributes) error: Function not implemented
    #
    def unzip(zipfile_path, parent_folder_dest)
      sh("cd #{parent_folder_dest} && unzip -qo #{zipfile_path} > /dev/null 2>&1")
    end

    def sh(command)
      say "=> #{command}".color(:green)
      success = system(command)
      abort("Command Failed #{command}") unless success
      success
    end

    # Returns the dest path
    def download_file(url, dest)
      if File.exist?(dest) && ENV["SG_FORCE_DOWNLOAD"].blank?
        say "File already downloaded #{dest}"
        return dest
      end

      say "Downloading..."
      downloaded = URI.open(url, "rb") { |read_file| read_file.read }

      FileUtils.mkdir_p(File.dirname(dest)) # ensure parent folder exists

      File.binwrite(dest, downloaded)

      dest
    end

    @@log_level = :info # default level is :info
    # @@log_level = :debug # uncomment to debug
    def log_level=(val)
      @@log_level = val
    end

    def say(message, level = :info)
      enabled = @@log_level == :debug || level == :debug
      puts(message) if enabled
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
serverlessgems-0.4.1 lib/jets/gems/extract/base.rb