2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-18 22:11:42 +03:00
|
|
|
require "fileutils"
|
2018-09-03 19:39:07 +01:00
|
|
|
require "cask/cache"
|
|
|
|
require "cask/quarantine"
|
|
|
|
require "cask/verify"
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2018-09-06 08:29:14 +02:00
|
|
|
module Cask
|
2016-09-24 13:52:43 +02:00
|
|
|
class Download
|
|
|
|
attr_reader :cask
|
|
|
|
|
2018-09-07 15:37:31 +00:00
|
|
|
def initialize(cask, force: false, quarantine: nil)
|
2016-09-24 13:52:43 +02:00
|
|
|
@cask = cask
|
|
|
|
@force = force
|
2018-08-31 13:16:11 +00:00
|
|
|
@quarantine = quarantine
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
|
|
|
clear_cache
|
|
|
|
fetch
|
2018-08-31 13:16:11 +00:00
|
|
|
quarantine
|
2016-09-24 13:52:43 +02:00
|
|
|
downloaded_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def downloader
|
2018-08-04 10:13:42 +02:00
|
|
|
@downloader ||= begin
|
|
|
|
strategy = DownloadStrategyDetector.detect(cask.url.to_s, cask.url.using)
|
|
|
|
strategy.new(cask.url.to_s, cask.token, cask.version, cache: Cache.path, **cask.url.specs)
|
2016-10-14 20:11:33 +02:00
|
|
|
end
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
|
|
|
|
2018-08-10 04:11:54 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :force
|
|
|
|
attr_accessor :downloaded_path
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def clear_cache
|
2019-05-31 20:37:57 +02:00
|
|
|
downloader.clear_cache if force
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def fetch
|
2018-08-04 10:13:42 +02:00
|
|
|
downloader.fetch
|
|
|
|
@downloaded_path = downloader.cached_location
|
2018-09-02 20:14:54 +01:00
|
|
|
rescue => e
|
2018-08-10 04:11:54 +02:00
|
|
|
error = CaskError.new("Download failed on Cask '#{cask}' with message: #{e}")
|
|
|
|
error.set_backtrace e.backtrace
|
|
|
|
raise error
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2018-08-31 13:16:11 +00:00
|
|
|
|
|
|
|
def quarantine
|
2018-09-07 15:37:31 +00:00
|
|
|
return if @quarantine.nil?
|
2018-08-31 13:16:11 +00:00
|
|
|
return unless Quarantine.available?
|
|
|
|
|
2018-09-08 14:00:44 +00:00
|
|
|
if @quarantine
|
|
|
|
Quarantine.cask!(cask: @cask, download_path: @downloaded_path)
|
|
|
|
else
|
|
|
|
Quarantine.release!(download_path: @downloaded_path)
|
|
|
|
end
|
2018-08-31 13:16:11 +00:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
end
|