From 06f7d7d696a7287e4af64a8ea0e3930d8039f293 Mon Sep 17 00:00:00 2001 From: Ethan Edwards Date: Sat, 24 Mar 2018 10:53:49 +0000 Subject: [PATCH] Allow bottles to use any DownloadStrategy --- Library/Homebrew/download_strategy.rb | 21 +++++++------------ Library/Homebrew/formulary.rb | 3 ++- Library/Homebrew/software_spec.rb | 16 ++++++++++---- .../Homebrew/test/download_strategies_spec.rb | 17 ++++++++++++++- 4 files changed, 38 insertions(+), 19 deletions(-) diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 2b594c9081..228cfd9215 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -6,6 +6,13 @@ class AbstractDownloadStrategy extend Forwardable include FileUtils + module Pourable + def stage + ohai "Pouring #{cached_location.basename}" + super + end + end + attr_reader :meta, :name, :version, :resource attr_reader :shutup @@ -15,6 +22,7 @@ class AbstractDownloadStrategy @url = resource.url @version = resource.version @meta = resource.specs + extend Pourable if meta[:bottle] end # Download and cache the resource as {#cached_location}. @@ -442,14 +450,6 @@ class NoUnzipCurlDownloadStrategy < CurlDownloadStrategy end end -# This strategy extracts our binary packages. -class CurlBottleDownloadStrategy < CurlDownloadStrategy - def stage - ohai "Pouring #{cached_location.basename}" - super - end -end - # This strategy extracts local binary packages. class LocalBottleDownloadStrategy < AbstractFileDownloadStrategy attr_reader :cached_location @@ -457,11 +457,6 @@ class LocalBottleDownloadStrategy < AbstractFileDownloadStrategy def initialize(path) @cached_location = path end - - def stage - ohai "Pouring #{cached_location.basename}" - super - end end # S3DownloadStrategy downloads tarballs from AWS S3. diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index 1b1987717a..5f6ae0723b 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -111,7 +111,8 @@ module Formulary # The name of the formula is found between the last slash and the last hyphen. formula_name = File.basename(bottle_name)[/(.+)-/, 1] resource = Resource.new(formula_name) { url bottle_name } - downloader = CurlBottleDownloadStrategy.new resource.name, resource + resource.specs[:bottle] = true + downloader = CurlDownloadStrategy.new resource.name, resource @bottle_filename = downloader.cached_location cached = @bottle_filename.exist? downloader.fetch diff --git a/Library/Homebrew/software_spec.rb b/Library/Homebrew/software_spec.rb index dd6026fcf4..1efff55920 100644 --- a/Library/Homebrew/software_spec.rb +++ b/Library/Homebrew/software_spec.rb @@ -283,13 +283,14 @@ class Bottle @name = formula.name @resource = Resource.new @resource.owner = formula + @resource.specs[:bottle] = true @spec = spec checksum, tag = spec.checksum_for(Utils::Bottles.tag) filename = Filename.create(formula, tag, spec.rebuild) - @resource.url(build_url(spec.root_url, filename)) - @resource.download_strategy = CurlBottleDownloadStrategy + @resource.url(build_url(spec.root_url, filename), + select_download_strategy(spec.root_url_specs)) @resource.version = formula.pkg_version @resource.checksum = checksum @prefix = spec.prefix @@ -315,6 +316,11 @@ class Bottle def build_url(root_url, filename) "#{root_url}/#{filename}" end + + def select_download_strategy(specs) + specs[:using] ||= CurlDownloadStrategy + specs + end end class BottleSpecification @@ -324,20 +330,22 @@ class BottleSpecification attr_rw :prefix, :cellar, :rebuild attr_accessor :tap - attr_reader :checksum, :collector + attr_reader :checksum, :collector, :root_url_specs def initialize @rebuild = 0 @prefix = DEFAULT_PREFIX @cellar = DEFAULT_CELLAR @collector = Utils::Bottles::Collector.new + @root_url_specs = {} end - def root_url(var = nil) + def root_url(var = nil, specs = {}) if var.nil? @root_url ||= "#{DEFAULT_DOMAIN}/#{Utils::Bottles::Bintray.repository(tap)}" else @root_url = var + @root_url_specs.merge!(specs) end end diff --git a/Library/Homebrew/test/download_strategies_spec.rb b/Library/Homebrew/test/download_strategies_spec.rb index b8b975e52a..d5437c1209 100644 --- a/Library/Homebrew/test/download_strategies_spec.rb +++ b/Library/Homebrew/test/download_strategies_spec.rb @@ -2,9 +2,10 @@ require "download_strategy" describe AbstractDownloadStrategy do subject { described_class.new(name, resource) } + let(:specs) { {} } let(:name) { "foo" } let(:url) { "http://example.com/foo.tar.gz" } - let(:resource) { double(Resource, url: url, mirrors: [], specs: {}, version: nil) } + let(:resource) { double(Resource, url: url, mirrors: [], specs: specs, version: nil) } let(:args) { %w[foo bar baz] } describe "#expand_safe_system_args" do @@ -34,6 +35,20 @@ describe AbstractDownloadStrategy do expect(subject.source_modified_time).to eq(File.mtime("foo")) end end + + context "when specs[:bottle]" do + let(:specs) { { bottle: true } } + + it "extends Pourable" do + expect(subject).to be_a_kind_of(AbstractDownloadStrategy::Pourable) + end + end + + context "without specs[:bottle]" do + it "is does not extend Pourable" do + expect(subject).to_not be_a_kind_of(AbstractDownloadStrategy::Pourable) + end + end end describe VCSDownloadStrategy do