| 
									
										
										
										
											2025-06-17 16:33:16 +01:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  | require "abstract_command" | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  | require "formula" | 
					
						
							| 
									
										
										
										
											2018-06-05 23:19:18 -04:00
										 |  |  | require "fetch" | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  | require "cask/download" | 
					
						
							| 
									
										
										
										
											2025-06-17 16:33:16 +01:00
										 |  |  | require "download_queue" | 
					
						
							| 
									
										
										
										
											2011-03-12 09:40:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 22:41:47 -05:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |   module Cmd | 
					
						
							|  |  |  |     class FetchCmd < AbstractCommand | 
					
						
							|  |  |  |       include Fetch | 
					
						
							| 
									
										
										
										
											2025-08-13 12:23:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |       FETCH_MAX_TRIES = 5
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       cmd_args do | 
					
						
							|  |  |  |         description <<~EOS | 
					
						
							|  |  |  |           Download a bottle (if available) or source packages for <formula>e | 
					
						
							|  |  |  |           and binaries for <cask>s. For files, also print SHA-256 checksums. | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |         flag   "--os=", | 
					
						
							|  |  |  |                description: "Download for the given operating system. " \ | 
					
						
							|  |  |  |                             "(Pass `all` to download for all operating systems.)" | 
					
						
							|  |  |  |         flag   "--arch=", | 
					
						
							|  |  |  |                description: "Download for the given CPU architecture. " \ | 
					
						
							|  |  |  |                             "(Pass `all` to download for all architectures.)" | 
					
						
							|  |  |  |         flag   "--bottle-tag=", | 
					
						
							|  |  |  |                description: "Download a bottle for given tag." | 
					
						
							|  |  |  |         switch "--HEAD", | 
					
						
							|  |  |  |                description: "Fetch HEAD version instead of stable version." | 
					
						
							|  |  |  |         switch "-f", "--force", | 
					
						
							|  |  |  |                description: "Remove a previously cached version and re-fetch." | 
					
						
							|  |  |  |         switch "-v", "--verbose", | 
					
						
							|  |  |  |                description: "Do a verbose VCS checkout, if the URL represents a VCS. This is useful for " \ | 
					
						
							|  |  |  |                             "seeing if an existing VCS cache has been updated." | 
					
						
							|  |  |  |         switch "--retry", | 
					
						
							|  |  |  |                description: "Retry if downloading fails or re-download if the checksum of a previously cached " \ | 
					
						
							|  |  |  |                             "version no longer matches. Tries at most #{FETCH_MAX_TRIES} times with " \ | 
					
						
							|  |  |  |                             "exponential backoff." | 
					
						
							|  |  |  |         switch "--deps", | 
					
						
							|  |  |  |                description: "Also download dependencies for any listed <formula>." | 
					
						
							|  |  |  |         switch "-s", "--build-from-source", | 
					
						
							|  |  |  |                description: "Download source packages rather than a bottle." | 
					
						
							|  |  |  |         switch "--build-bottle", | 
					
						
							|  |  |  |                description: "Download source packages (for eventual bottling) rather than a bottle." | 
					
						
							|  |  |  |         switch "--force-bottle", | 
					
						
							|  |  |  |                description: "Download a bottle if it exists for the current or newest version of macOS, " \ | 
					
						
							|  |  |  |                             "even if it would not be used during installation." | 
					
						
							|  |  |  |         switch "--[no-]quarantine", | 
					
						
							|  |  |  |                description: "Disable/enable quarantining of downloads (default: enabled).", | 
					
						
							|  |  |  |                env:         :cask_opts_quarantine | 
					
						
							|  |  |  |         switch "--formula", "--formulae", | 
					
						
							|  |  |  |                description: "Treat all named arguments as formulae." | 
					
						
							|  |  |  |         switch "--cask", "--casks", | 
					
						
							|  |  |  |                description: "Treat all named arguments as casks." | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         conflicts "--build-from-source", "--build-bottle", "--force-bottle", "--bottle-tag" | 
					
						
							|  |  |  |         conflicts "--cask", "--HEAD" | 
					
						
							|  |  |  |         conflicts "--cask", "--deps" | 
					
						
							|  |  |  |         conflicts "--cask", "-s" | 
					
						
							|  |  |  |         conflicts "--cask", "--build-bottle" | 
					
						
							|  |  |  |         conflicts "--cask", "--force-bottle" | 
					
						
							|  |  |  |         conflicts "--cask", "--bottle-tag" | 
					
						
							|  |  |  |         conflicts "--formula", "--cask" | 
					
						
							|  |  |  |         conflicts "--os", "--bottle-tag" | 
					
						
							|  |  |  |         conflicts "--arch", "--bottle-tag" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         named_args [:formula, :cask], min: 1
 | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-10-27 23:44:32 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |       sig { override.void } | 
					
						
							|  |  |  |       def run | 
					
						
							|  |  |  |         Formulary.enable_factory_cache! | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         bucket = if args.deps? | 
					
						
							|  |  |  |           args.named.to_formulae_and_casks.flat_map do |formula_or_cask| | 
					
						
							|  |  |  |             case formula_or_cask | 
					
						
							|  |  |  |             when Formula | 
					
						
							|  |  |  |               formula = formula_or_cask | 
					
						
							|  |  |  |               [formula, *formula.recursive_dependencies.map(&:to_formula)] | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |               formula_or_cask | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           args.named.to_formulae_and_casks | 
					
						
							|  |  |  |         end.uniq | 
					
						
							| 
									
										
										
										
											2018-10-27 23:44:32 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |         os_arch_combinations = args.os_arch_combinations | 
					
						
							| 
									
										
										
										
											2023-03-25 11:56:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |         puts "Fetching: #{bucket * ", "}" if bucket.size > 1
 | 
					
						
							|  |  |  |         bucket.each do |formula_or_cask| | 
					
						
							|  |  |  |           case formula_or_cask | 
					
						
							|  |  |  |           when Formula | 
					
						
							| 
									
										
										
										
											2025-06-17 16:33:16 +01:00
										 |  |  |             formula = formula_or_cask | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |             ref = formula.loaded_from_api? ? formula.full_name : formula.path | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |             os_arch_combinations.each do |os, arch| | 
					
						
							|  |  |  |               SimulateSystem.with(os:, arch:) do | 
					
						
							|  |  |  |                 formula = Formulary.factory(ref, args.HEAD? ? :head : :stable) | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |                 formula.print_tap_action verb: "Fetching" | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |                 fetched_bottle = false | 
					
						
							|  |  |  |                 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, | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                   begin | 
					
						
							|  |  |  |                     formula.clear_cache if args.force? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     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) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if bottle.nil? | 
					
						
							|  |  |  |                       opoo "Bottle for tag #{bottle_tag.to_sym.inspect} is unavailable." | 
					
						
							|  |  |  |                       next | 
					
						
							|  |  |  |                     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-14 11:42:22 -04:00
										 |  |  |                     if (manifest_resource = bottle.github_packages_manifest_resource) | 
					
						
							| 
									
										
										
										
											2025-07-11 15:54:49 +01:00
										 |  |  |                       download_queue.enqueue(manifest_resource) | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |                     end | 
					
						
							| 
									
										
										
										
											2025-07-11 15:54:49 +01:00
										 |  |  |                     download_queue.enqueue(bottle) | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |                   rescue Interrupt | 
					
						
							|  |  |  |                     raise | 
					
						
							|  |  |  |                   rescue => e | 
					
						
							|  |  |  |                     raise if Homebrew::EnvConfig.developer? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     fetched_bottle = false | 
					
						
							|  |  |  |                     onoe e.message | 
					
						
							|  |  |  |                     opoo "Bottle fetch failed, fetching the source instead." | 
					
						
							|  |  |  |                   else | 
					
						
							|  |  |  |                     fetched_bottle = true | 
					
						
							|  |  |  |                   end | 
					
						
							| 
									
										
										
										
											2023-10-13 21:14:07 +01:00
										 |  |  |                 end | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |                 next if fetched_bottle | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-17 16:33:16 +01:00
										 |  |  |                 if (resource = formula.resource) | 
					
						
							| 
									
										
										
										
											2025-07-11 15:54:49 +01:00
										 |  |  |                   download_queue.enqueue(resource) | 
					
						
							| 
									
										
										
										
											2025-06-17 16:33:16 +01:00
										 |  |  |                 end | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-17 17:49:53 +01:00
										 |  |  |                 formula.enqueue_resources_and_patches(download_queue:) | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |               end | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  |             end | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |           else | 
					
						
							|  |  |  |             cask = formula_or_cask | 
					
						
							|  |  |  |             ref = cask.loaded_from_api? ? cask.full_name : cask.sourcefile_path | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |             os_arch_combinations.each do |os, arch| | 
					
						
							|  |  |  |               SimulateSystem.with(os:, arch:) do | 
					
						
							|  |  |  |                 cask = Cask::CaskLoader.load(ref) | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |                 if cask.url.nil? || cask.sha256.nil? | 
					
						
							|  |  |  |                   opoo "Cask #{cask} is not supported on os #{os} and arch #{arch}" | 
					
						
							|  |  |  |                   next | 
					
						
							|  |  |  |                 end | 
					
						
							| 
									
										
										
										
											2023-09-02 01:33:39 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |                 quarantine = args.quarantine? | 
					
						
							|  |  |  |                 quarantine = true if quarantine.nil? | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |                 download = Cask::Download.new(cask, quarantine:) | 
					
						
							| 
									
										
										
										
											2025-07-11 15:54:49 +01:00
										 |  |  |                 download_queue.enqueue(download) | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |               end | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2011-04-14 14:57:21 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-17 17:49:53 +01:00
										 |  |  |         download_queue.fetch | 
					
						
							| 
									
										
										
										
											2024-09-07 14:45:30 +02:00
										 |  |  |       ensure | 
					
						
							| 
									
										
										
										
											2024-07-14 11:42:22 -04:00
										 |  |  |         download_queue.shutdown | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-14 11:42:22 -04:00
										 |  |  |       private | 
					
						
							| 
									
										
										
										
											2013-10-31 14:28:49 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-11 15:54:49 +01:00
										 |  |  |       sig { returns(Integer) } | 
					
						
							|  |  |  |       def retries | 
					
						
							| 
									
										
										
										
											2025-07-21 08:36:36 +01:00
										 |  |  |         @retries ||= T.let(args.retry? ? FETCH_MAX_TRIES : 1, T.nilable(Integer)) | 
					
						
							| 
									
										
										
										
											2025-07-11 15:54:49 +01:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       sig { returns(DownloadQueue) } | 
					
						
							|  |  |  |       def download_queue | 
					
						
							|  |  |  |         @download_queue ||= T.let(begin | 
					
						
							| 
									
										
										
										
											2025-07-17 17:49:53 +01:00
										 |  |  |           DownloadQueue.new(retries:, force: args.force?) | 
					
						
							| 
									
										
										
										
											2025-07-11 15:54:49 +01:00
										 |  |  |         end, T.nilable(DownloadQueue)) | 
					
						
							| 
									
										
										
										
											2024-03-29 18:24:18 -07:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-03-12 09:40:10 -08:00
										 |  |  |   end | 
					
						
							|  |  |  | end |