Class: XcodeInstall::Curl

Inherits:
Object
  • Object
show all
Defined in:
lib/xcode/install.rb

Constant Summary collapse

COOKIES_PATH =
Pathname.new('/tmp/curl-cookies.txt')

Instance Method Summary collapse

Instance Method Details

#fetch(url, directory = nil, cookies = nil, output = nil, progress = true) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/xcode/install.rb', line 15

def fetch(url, directory = nil, cookies = nil, output = nil, progress = true)
  options = cookies.nil? ? [] : ['--cookie', cookies, '--cookie-jar', COOKIES_PATH]
  # options << ' -vvv'

  uri = URI.parse(url)
  output ||= File.basename(uri.path)
  output = (Pathname.new(directory) + Pathname.new(output)) if directory

  retry_options = ['--retry', '3']
  progress = progress ? '--progress-bar' : '--silent'
  command = ['curl', *options, *retry_options, '--location', '--continue-at', '-', progress, '--output', output, url].map(&:to_s)

  # Run the curl command in a loop, retry when curl exit status is 18
  # "Partial file. Only a part of the file was transferred."
  # https://curl.haxx.se/mail/archive-2008-07/0098.html
  # https://github.com/KrauseFx/xcode-install/issues/210
  3.times do
    io = IO.popen(command)
    io.each { |line| puts line }
    io.close

    exit_code = $?.exitstatus
    return exit_code.zero? unless exit_code == 18
  end
  false
ensure
  FileUtils.rm_f(COOKIES_PATH)
end