Sha256: 10683cb0ce66eaf9b9e84e6c5b49df40902d79464b42914f5741c2c5fa972e13

Contents?: true

Size: 1.93 KB

Versions: 24

Compression:

Stored size: 1.93 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/lambdagems"
      @source_url = options[:source_url] || "https://gems.lambdagems.com"
    end

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

    def unpack_tarball(tarball_path)
      dest = project_root
      say "Unpacking into #{dest}"
      FileUtils.mkdir_p(dest)
      untar(tarball_path, dest)
    end

    def untar(tarball_path, parent_folder_dest)
      sh("tar -xzf #{tarball_path} -C #{parent_folder_dest}")
    end

    def sh(command)
      say "=> #{command}".colorize(: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

24 entries across 24 versions & 1 rubygems

Version Path
jets-1.2.1 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.2.0 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.1.5 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.1.4 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.1.3 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.1.2 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.1.1 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.1.0 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.0.18 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.0.17 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.0.16 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.0.15 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.0.13 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.0.12 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.0.11 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.0.10 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.0.9 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.0.8 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.0.7 vendor/jets-gems/lib/jets/gems/extract/base.rb
jets-1.0.6 vendor/jets-gems/lib/jets/gems/extract/base.rb