Sha256: 3c1a5e213e36f8bfb9e67d65922ec8537debf9ecbc4502a291389c88d47c1c2f

Contents?: true

Size: 1.78 KB

Versions: 40

Compression:

Stored size: 1.78 KB

Contents

require "open-uri"

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

    attr_reader :source_url
    def initialize(name, options={})
      @name = name
      @options = options

      @downloads_root = options[:downloads_root] || "/tmp/jets/#{Jets.config.project_name}/lambdagems"
      @source_url = options[:source_url] || Jets.default_gems_source
    end

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

    def unzip(zipfile_path, parent_folder_dest)
      sh("cd #{parent_folder_dest} && unzip -qo #{zipfile_path}")
    end

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

    def url_exists?(url)
      exist = Jets::Gems::Exist.new(@options)
      exist.url_exists?(url)
    end

    # Returns the dest path
    def download_file(source_url, dest)
      say "Url #{source_url}"
      return unless url_exists?(source_url)

      if File.exist?(dest)
        say "File already downloaded #{dest}"
        return dest
      end

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

      File.open(dest, 'wb') do |saved_file|
        open(source_url, 'rb') do |read_file|
          saved_file.write(read_file.read)
        end
      end
      dest
    end

    def project_root
      @options[:project_root] || "."
    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

40 entries across 40 versions & 2 rubygems

Version Path
jets-gems-0.2.3 lib/jets/gems/extract/base.rb
jets-gems-0.2.2 lib/jets/gems/extract/base.rb
jets-gems-0.2.1 lib/jets/gems/extract/base.rb
jets-gems-0.2.0 lib/jets/gems/extract/base.rb
jets-gems-0.1.0 lib/jets/gems/extract/base.rb
jets-1.9.7 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.9.6 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.9.5 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.9.4 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.9.3 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.9.2 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.9.1 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.9.0 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.8.14 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.8.13 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.8.12 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.8.11 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.8.10 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.8.9 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.8.8 vendor/jets-gems/lib/jets/gems/extract/base.rb