diff --git a/Library/Homebrew/cask/cmd/--cache.rb b/Library/Homebrew/cask/cmd/--cache.rb index d3aaf8e027..7aa96ec33f 100644 --- a/Library/Homebrew/cask/cmd/--cache.rb +++ b/Library/Homebrew/cask/cmd/--cache.rb @@ -16,10 +16,14 @@ module Cask def run casks.each do |cask| - puts Download.new(cask).downloader.cached_location + puts self.class.cached_location(cask) end end + def self.cached_location(cask) + Download.new(cask).downloader.cached_location + end + def self.help "display the file used to cache the Cask" end diff --git a/Library/Homebrew/cmd/--cache.rb b/Library/Homebrew/cmd/--cache.rb index 225bf68747..de6c1ac0a0 100644 --- a/Library/Homebrew/cmd/--cache.rb +++ b/Library/Homebrew/cmd/--cache.rb @@ -2,6 +2,8 @@ require "fetch" require "cli/parser" +require "cask/cmd" +require "cask/cask_loader" module Homebrew module_function @@ -19,7 +21,12 @@ module Homebrew description: "Show the cache file used when building from source." switch "--force-bottle", description: "Show the cache file used when pouring a bottle." + switch "--formula", + description: "Show cache files for only formulae" + switch "--cask", + description: "Show cache files for only casks" conflicts "--build-from-source", "--force-bottle" + conflicts "--formula", "--cask" end end @@ -28,14 +35,38 @@ module Homebrew if args.no_named? puts HOMEBREW_CACHE + elsif args.formula? + args.named.each do |name| + print_formula_cache name + end + elsif args.cask? + args.named.each do |name| + print_cask_cache name + end else - args.formulae.each do |f| - if Fetch.fetch_bottle?(f) - puts f.bottle.cached_download - else - puts f.cached_download + args.named.each do |name| + print_formula_cache name + rescue FormulaUnavailableError + begin + print_cask_cache name + rescue Cask::CaskUnavailableError + odie "No available formula or cask with the name \"#{name}\"" end end end end + + def print_formula_cache(name) + formula = Formulary.factory name + if Fetch.fetch_bottle?(formula) + puts formula.bottle.cached_download + else + puts formula.cached_download + end + end + + def print_cask_cache(name) + cask = Cask::CaskLoader.load name + puts Cask::Cmd::Cache.cached_location(cask) + end end diff --git a/Library/Homebrew/cmd/home.rb b/Library/Homebrew/cmd/home.rb index e18726441b..e567febfab 100644 --- a/Library/Homebrew/cmd/home.rb +++ b/Library/Homebrew/cmd/home.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true require "cli/parser" +require "cask/cask_loader" +require "cask/exceptions" module Homebrew module_function @@ -23,7 +25,20 @@ module Homebrew if args.no_named? exec_browser HOMEBREW_WWW else - exec_browser(*args.formulae.map(&:homepage)) + homepages = args.named.map do |name| + f = Formulary.factory(name) + puts "Opening homepage for formula #{name}" + f.homepage + rescue FormulaUnavailableError + begin + c = Cask::CaskLoader.load(name) + puts "Opening homepage for cask #{name}" + c.homepage + rescue Cask::CaskUnavailableError + odie "No available formula or cask with the name \"#{name}\"" + end + end + exec_browser(*homepages) end end end diff --git a/Library/Homebrew/test/cmd/--cache_spec.rb b/Library/Homebrew/test/cmd/--cache_spec.rb index 42b452ec60..ea8c263fc7 100644 --- a/Library/Homebrew/test/cmd/--cache_spec.rb +++ b/Library/Homebrew/test/cmd/--cache_spec.rb @@ -13,4 +13,23 @@ describe "brew --cache", :integration_test do .and not_to_output.to_stderr .and be_a_success end + + it "prints the cache files for a given Cask" do + expect { brew "--cache", cask_path("local-caffeine") } + .to output(%r{#{HOMEBREW_CACHE}/downloads/[\da-f]{64}--caffeine\.zip}).to_stdout + .and not_to_output.to_stderr + .and be_a_success + end + + it "prints the cache files for a given Formula and Cask" do + expect { brew "--cache", testball, cask_path("local-caffeine") } + .to output( + %r{ + #{HOMEBREW_CACHE}/downloads/[\da-f]{64}--testball-.*\n + #{HOMEBREW_CACHE}/downloads/[\da-f]{64}--caffeine\.zip + }x, + ).to_stdout + .and not_to_output.to_stderr + .and be_a_success + end end diff --git a/Library/Homebrew/test/cmd/home_spec.rb b/Library/Homebrew/test/cmd/home_spec.rb index 15aee21eb2..be1084ea51 100644 --- a/Library/Homebrew/test/cmd/home_spec.rb +++ b/Library/Homebrew/test/cmd/home_spec.rb @@ -1,17 +1,46 @@ # frozen_string_literal: true require "cmd/shared_examples/args_parse" +require "support/lib/config" describe "Homebrew.home_args" do it_behaves_like "parseable arguments" end describe "brew home", :integration_test do + let(:testballhome_homepage) { + Formula["testballhome"].homepage + } + + let(:local_caffeine_path) { + cask_path("local-caffeine") + } + + let(:local_caffeine_homepage) { + Cask::CaskLoader.load(local_caffeine_path).homepage + } + it "opens the homepage for a given Formula" do setup_test_formula "testballhome" expect { brew "home", "testballhome", "HOMEBREW_BROWSER" => "echo" } - .to output("#{Formula["testballhome"].homepage}\n").to_stdout + .to output(/#{testballhome_homepage}/).to_stdout + .and not_to_output.to_stderr + .and be_a_success + end + + it "opens the homepage for a given Cask" do + expect { brew "home", cask_path("local-caffeine"), "HOMEBREW_BROWSER" => "echo" } + .to output(/#{local_caffeine_homepage}/).to_stdout + .and not_to_output.to_stderr + .and be_a_success + end + + it "opens the homepages for a given formula and Cask" do + setup_test_formula "testballhome" + + expect { brew "home", "testballhome", cask_path("local-caffeine"), "HOMEBREW_BROWSER" => "echo" } + .to output(/#{testballhome_homepage} #{local_caffeine_homepage}/).to_stdout .and not_to_output.to_stderr .and be_a_success end diff --git a/docs/Manpage.md b/docs/Manpage.md index eff6277c94..0acac0de49 100644 --- a/docs/Manpage.md +++ b/docs/Manpage.md @@ -587,6 +587,10 @@ If *`formula`* is provided, display the file or directory used to cache *`formul Show the cache file used when building from source. * `--force-bottle`: Show the cache file used when pouring a bottle. +* `--formula`: + Show cache files for only formulae +* `--cask`: + Show cache files for only casks ### `--cellar` [*`formula`*] diff --git a/manpages/brew.1 b/manpages/brew.1 index d02b4cfaf4..ebec6a5b2f 100644 --- a/manpages/brew.1 +++ b/manpages/brew.1 @@ -763,6 +763,14 @@ Show the cache file used when building from source\. \fB\-\-force\-bottle\fR Show the cache file used when pouring a bottle\. . +.TP +\fB\-\-formula\fR +Show cache files for only formulae +. +.TP +\fB\-\-cask\fR +Show cache files for only casks +. .SS "\fB\-\-cellar\fR [\fIformula\fR]" Display Homebrew\'s Cellar path\. \fIDefault:\fR \fB$(brew \-\-prefix)/Cellar\fR, or if that directory doesn\'t exist, \fB$(brew \-\-repository)/Cellar\fR\. .