Class: Babushka::Resource

Inherits:
Object show all
Extended by:
LogHelpers, PathHelpers, ShellHelpers
Defined in:
lib/babushka/resource.rb

Class Method Summary collapse

Methods included from LogHelpers

debug, deprecated!, log, log_block, log_error, log_ok, log_stderr, log_warn, removed!

Methods included from ShellHelpers

cmd_dir, current_username, log_shell, login_shell, raw_shell, shell, shell!, shell?, shell_cmd, sudo, which

Methods included from PathHelpers

cd, in_build_dir, in_download_dir

Class Method Details

.download(url, filename = url.to_s.p.basename) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/babushka/resource.rb', line 32

def self.download url, filename = url.to_s.p.basename
  if filename.p.exists? && !filename.p.empty?
    log_ok "Already downloaded #{filename}."
    filename
  else
    success = log_block "Downloading #{url}" do
      shell('curl', '-#', '-L', '-o' "#{filename}.tmp", url.to_s, :progress => /[\d\.]+%/) &&
        shell('mv', '-f', "#{filename}.tmp", filename)
    end

    filename if success
  end
end

.extract(url, &block) ⇒ Object


24
25
26
27
28
29
30
# File 'lib/babushka/resource.rb', line 24

def self.extract url, &block
  get url do |download_path|
    in_build_dir {
      Asset.for(download_path).extract(&block)
    }
  end
end

.get(url, &block) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/babushka/resource.rb', line 9

def self.get url, &block
  filename = URI.unescape(url.to_s).p.basename
  if filename.to_s.blank?
    log_error "Not a valid URL to download: #{url}"
  elsif url.to_s[%r{^git://}]
    GitHelpers.git(url, &block)
  else
    download_path = in_download_dir {|path|
      downloaded_file = download(url, filename)
      path / downloaded_file if downloaded_file
    }
    block.call download_path unless download_path.nil?
  end
end