Module: BuildpackSupport::Component::Downloads

Includes:
WithTiming, Shell
Included in:
BaseComponent, VersionedDownloads
Defined in:
lib/buildpack_support/component/downloads.rb

Overview

A mixin that provides methods for downloading, caching, and unpacking files

Instance Method Summary collapse

Methods included from WithTiming

#with_timing

Methods included from Shell

#shell

Instance Method Details

#download(version, uri, name = @component_name) ⇒ Void

Downloads an item with the given name and version from the given URI, then yields the resultant file to the given block.

Parameters:

Returns:

  • (Void)


35
36
37
38
39
40
41
42
43
# File 'lib/buildpack_support/component/downloads.rb', line 35

def download(version, uri, name = @component_name)
  download_start_time = Time.now
  print "-----> Downloading #{name} #{version} from #{uri} "

  BuildpackSupport::Cache::ApplicationCache.new.get(uri) do |file, downloaded|
    puts downloaded ? "(#{(Time.now - download_start_time).duration})" : '(found in cache)'
    yield file
  end
end

#download_tar(version, uri, target_directory = @droplet.sandbox, name = @component_name) ⇒ Void

Downloads a given TAR file and expands it.

Parameters:

  • version (String)

    the version of the download

  • uri (String)

    the uri of the download

  • target_directory (Pathname) (defaults to: @droplet.sandbox)

    the directory to expand the TAR file to. Defaults to the component’s sandbox.

  • name (String) (defaults to: @component_name)

    an optional name for the download and expansion. Defaults to @component_name.

Returns:

  • (Void)


52
53
54
55
56
57
58
59
# File 'lib/buildpack_support/component/downloads.rb', line 52

def download_tar(version, uri, target_directory = @droplet.sandbox, name = @component_name)
  download(version, uri, name) do |file|
    with_timing "Expanding #{name} to #{target_directory.relative_path_from(@droplet.root)}" do
      FileUtils.mkdir_p target_directory
      shell "tar xzf #{file.path} -C #{target_directory} --strip 1 2>&1"
    end
  end
end

#download_zip(version, uri, strip_top_level = true, target_directory = @droplet.sandbox, name = @component_name) ⇒ Void

Downloads a given ZIP file and expands it.

Parameters:

  • strip_top_level (Boolean) (defaults to: true)

    whether to strip the top-level directory when expanding. Defaults to true.

  • target_directory (Pathname) (defaults to: @droplet.sandbox)

    the directory to expand the ZIP file to. Defaults to the component’s sandbox.

  • name (String) (defaults to: @component_name)

    an optional name for the download. Defaults to @component_name.

Returns:

  • (Void)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/buildpack_support/component/downloads.rb', line 67

def download_zip(version, uri, strip_top_level = true, target_directory = @droplet.sandbox, name = @component_name)
  download(version, uri, name) do |file|
    with_timing "Expanding #{name} to #{target_directory.relative_path_from(@droplet.root)}" do
      if strip_top_level
        Dir.mktmpdir do |root|
          shell "unzip -qq #{file.path} -d #{root} 2>&1"

          FileUtils.mkdir_p target_directory.parent
          FileUtils.mv Pathname.new(root).children.first, target_directory
        end
      else
        FileUtils.mkdir_p target_directory
        shell "unzip -qq #{file.path} -d #{target_directory} 2>&1"
      end
    end
  end
end