| 
									
										
										
										
											2023-08-08 13:54:59 -07:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 15:44:50 -07:00
										 |  |  | require "abstract_command" | 
					
						
							| 
									
										
										
										
											2018-06-05 23:19:18 -04:00
										 |  |  | require "fetch" | 
					
						
							| 
									
										
										
										
											2020-08-10 12:23:32 -04:00
										 |  |  | require "cask/download" | 
					
						
							| 
									
										
										
										
											2014-03-10 14:56:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 22:41:47 -05:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2024-04-01 15:44:50 -07:00
										 |  |  |   module Cmd | 
					
						
							|  |  |  |     class Cache < AbstractCommand | 
					
						
							|  |  |  |       include Fetch | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       sig { override.returns(String) } | 
					
						
							|  |  |  |       def self.command_name = "--cache" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       cmd_args do | 
					
						
							|  |  |  |         description <<~EOS | 
					
						
							|  |  |  |           Display Homebrew's download cache. See also `HOMEBREW_CACHE`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           If <formula> is provided, display the file or directory used to cache <formula>. | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |         flag   "--os=", | 
					
						
							|  |  |  |                description: "Show cache file for the given operating system. " \ | 
					
						
							|  |  |  |                             "(Pass `all` to show cache files for all operating systems.)" | 
					
						
							|  |  |  |         flag   "--arch=", | 
					
						
							|  |  |  |                description: "Show cache file for the given CPU architecture. " \ | 
					
						
							|  |  |  |                             "(Pass `all` to show cache files for all architectures.)" | 
					
						
							|  |  |  |         switch "-s", "--build-from-source", | 
					
						
							|  |  |  |                description: "Show the cache file used when building from source." | 
					
						
							|  |  |  |         switch "--force-bottle", | 
					
						
							|  |  |  |                description: "Show the cache file used when pouring a bottle." | 
					
						
							|  |  |  |         flag "--bottle-tag=", | 
					
						
							|  |  |  |              description: "Show the cache file used when pouring a bottle for the given tag." | 
					
						
							|  |  |  |         switch "--HEAD", | 
					
						
							|  |  |  |                description: "Show the cache file used when building from HEAD." | 
					
						
							|  |  |  |         switch "--formula", "--formulae", | 
					
						
							|  |  |  |                description: "Only show cache files for formulae." | 
					
						
							|  |  |  |         switch "--cask", "--casks", | 
					
						
							|  |  |  |                description: "Only show cache files for casks." | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         conflicts "--build-from-source", "--force-bottle", "--bottle-tag", "--HEAD", "--cask" | 
					
						
							|  |  |  |         conflicts "--formula", "--cask" | 
					
						
							|  |  |  |         conflicts "--os", "--bottle-tag" | 
					
						
							|  |  |  |         conflicts "--arch", "--bottle-tag" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         named_args [:formula, :cask] | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 15:44:50 -07:00
										 |  |  |       sig { override.void } | 
					
						
							|  |  |  |       def run | 
					
						
							|  |  |  |         if args.no_named? | 
					
						
							|  |  |  |           puts HOMEBREW_CACHE | 
					
						
							|  |  |  |           return | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 15:44:50 -07:00
										 |  |  |         formulae_or_casks = args.named.to_formulae_and_casks | 
					
						
							|  |  |  |         os_arch_combinations = args.os_arch_combinations | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         formulae_or_casks.each do |formula_or_cask| | 
					
						
							|  |  |  |           case formula_or_cask | 
					
						
							|  |  |  |           when Formula | 
					
						
							|  |  |  |             formula = formula_or_cask | 
					
						
							|  |  |  |             ref = formula.loaded_from_api? ? formula.full_name : formula.path | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             os_arch_combinations.each do |os, arch| | 
					
						
							|  |  |  |               SimulateSystem.with(os:, arch:) do | 
					
						
							|  |  |  |                 formula = Formulary.factory(ref) | 
					
						
							|  |  |  |                 print_formula_cache(formula, os:, arch:) | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           when Cask::Cask | 
					
						
							|  |  |  |             cask = formula_or_cask | 
					
						
							|  |  |  |             ref = cask.loaded_from_api? ? cask.full_name : cask.sourcefile_path | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             os_arch_combinations.each do |os, arch| | 
					
						
							|  |  |  |               next if os == :linux | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               SimulateSystem.with(os:, arch:) do | 
					
						
							|  |  |  |                 loaded_cask = Cask::CaskLoader.load(ref) | 
					
						
							|  |  |  |                 print_cask_cache(loaded_cask) | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             raise "Invalid type: #{formula_or_cask.class}" | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2014-03-10 14:56:03 -05:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-06-23 09:30:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 15:44:50 -07:00
										 |  |  |       private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       sig { params(formula: Formula, os: Symbol, arch: Symbol).void } | 
					
						
							|  |  |  |       def print_formula_cache(formula, os:, arch:) | 
					
						
							|  |  |  |         if fetch_bottle?( | 
					
						
							|  |  |  |           formula, | 
					
						
							|  |  |  |           force_bottle:               args.force_bottle?, | 
					
						
							|  |  |  |           bottle_tag:                 args.bottle_tag&.to_sym, | 
					
						
							|  |  |  |           build_from_source_formulae: args.build_from_source_formulae, | 
					
						
							|  |  |  |           os:                         args.os&.to_sym, | 
					
						
							|  |  |  |           arch:                       args.arch&.to_sym, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |           bottle_tag = if (bottle_tag = args.bottle_tag&.to_sym) | 
					
						
							|  |  |  |             Utils::Bottles::Tag.from_symbol(bottle_tag) | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             Utils::Bottles::Tag.new(system: os, arch:) | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           bottle = formula.bottle_for_tag(bottle_tag) | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 15:44:50 -07:00
										 |  |  |           if bottle.nil? | 
					
						
							|  |  |  |             opoo "Bottle for tag #{bottle_tag.to_sym.inspect} is unavailable." | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 15:44:50 -07:00
										 |  |  |           puts bottle.cached_download | 
					
						
							|  |  |  |         elsif args.HEAD? | 
					
						
							|  |  |  |           puts T.must(formula.head).cached_download | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           puts formula.cached_download | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 15:44:50 -07:00
										 |  |  |       sig { params(cask: Cask::Cask).void } | 
					
						
							|  |  |  |       def print_cask_cache(cask) | 
					
						
							|  |  |  |         puts Cask::Download.new(cask).downloader.cached_location | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-06-23 09:30:24 -04:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | end |