| 
									
										
										
										
											2025-06-17 14:56:10 +01:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2021-08-06 02:30:44 -04:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-09 19:06:16 +01:00
										 |  |  | require "cachable" | 
					
						
							| 
									
										
										
										
											2025-07-18 15:01:34 +01:00
										 |  |  | require "api" | 
					
						
							| 
									
										
										
										
											2025-07-22 17:48:32 +01:00
										 |  |  | require "api/source_download" | 
					
						
							| 
									
										
										
										
											2025-07-18 15:01:34 +01:00
										 |  |  | require "download_queue" | 
					
						
							| 
									
										
										
										
											2023-02-16 21:49:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 02:30:44 -04:00
										 |  |  | module Homebrew | 
					
						
							|  |  |  |   module API | 
					
						
							|  |  |  |     # Helper functions for using the cask JSON API. | 
					
						
							|  |  |  |     module Cask | 
					
						
							| 
									
										
										
										
											2024-02-25 15:14:22 -08:00
										 |  |  |       extend Cachable | 
					
						
							| 
									
										
										
										
											2021-08-06 02:30:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-04 12:08:01 -04:00
										 |  |  |       DEFAULT_API_FILENAME = "cask.jws.json" | 
					
						
							| 
									
										
										
										
											2024-07-03 13:41:52 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |       private_class_method :cache | 
					
						
							| 
									
										
										
										
											2023-02-16 21:49:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-10 22:45:59 -04:00
										 |  |  |       sig { params(name: String).returns(T::Hash[String, T.untyped]) } | 
					
						
							|  |  |  |       def self.cask_json(name) | 
					
						
							|  |  |  |         fetch_cask_json! name if !cache.key?("cask_json") || !cache.fetch("cask_json").key?(name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cache.fetch("cask_json").fetch(name) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       sig { params(name: String, download_queue: T.nilable(DownloadQueue)).void } | 
					
						
							|  |  |  |       def self.fetch_cask_json!(name, download_queue: nil) | 
					
						
							|  |  |  |         endpoint = "cask/#{name}.json" | 
					
						
							|  |  |  |         json_cask, updated = Homebrew::API.fetch_json_api_file endpoint, download_queue: download_queue | 
					
						
							|  |  |  |         return if download_queue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         json_cask = JSON.parse((HOMEBREW_CACHE_API/endpoint).read) unless updated | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cache["cask_json"] ||= {} | 
					
						
							|  |  |  |         cache["cask_json"][name] = json_cask | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2022-12-29 02:48:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-29 14:49:08 +01:00
										 |  |  |       sig { params(cask: ::Cask::Cask, download_queue: T.nilable(Homebrew::DownloadQueue)).returns(Homebrew::API::SourceDownload) } | 
					
						
							|  |  |  |       def self.source_download(cask, download_queue: nil) | 
					
						
							| 
									
										
										
										
											2025-07-14 14:48:08 +01:00
										 |  |  |         path = cask.ruby_source_path.to_s | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |         sha256 = cask.ruby_source_checksum[:sha256] | 
					
						
							|  |  |  |         checksum = Checksum.new(sha256) if sha256 | 
					
						
							|  |  |  |         git_head = cask.tap_git_head || "HEAD" | 
					
						
							|  |  |  |         tap = cask.tap&.full_name || "Homebrew/homebrew-cask" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-22 17:48:32 +01:00
										 |  |  |         download = Homebrew::API::SourceDownload.new( | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |           "https://raw.githubusercontent.com/#{tap}/#{git_head}/#{path}", | 
					
						
							|  |  |  |           checksum, | 
					
						
							|  |  |  |           mirrors: [ | 
					
						
							|  |  |  |             "#{HOMEBREW_API_DEFAULT_DOMAIN}/cask-source/#{File.basename(path)}", | 
					
						
							|  |  |  |           ], | 
					
						
							|  |  |  |           cache:   HOMEBREW_CACHE_API_SOURCE/"#{tap}/#{git_head}/Cask", | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2025-07-29 14:49:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if download_queue | 
					
						
							|  |  |  |           download_queue.enqueue(download) | 
					
						
							| 
									
										
										
										
											2025-07-31 05:40:40 +01:00
										 |  |  |         elsif !download.symlink_location.exist? | 
					
						
							| 
									
										
										
										
											2025-07-29 14:49:08 +01:00
										 |  |  |           download.fetch | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         download | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       sig { params(cask: ::Cask::Cask).returns(::Cask::Cask) } | 
					
						
							|  |  |  |       def self.source_download_cask(cask) | 
					
						
							|  |  |  |         download = source_download(cask) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |         ::Cask::CaskLoader::FromPathLoader.new(download.symlink_location) | 
					
						
							|  |  |  |                                           .load(config: cask.config) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-02-16 21:49:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-17 14:56:10 +01:00
										 |  |  |       sig { returns(Pathname) } | 
					
						
							| 
									
										
										
										
											2024-07-03 13:41:52 -04:00
										 |  |  |       def self.cached_json_file_path | 
					
						
							| 
									
										
										
										
											2025-08-11 00:36:10 -04:00
										 |  |  |         HOMEBREW_CACHE_API/DEFAULT_API_FILENAME | 
					
						
							| 
									
										
										
										
											2024-07-03 13:41:52 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-18 15:01:34 +01:00
										 |  |  |       sig { | 
					
						
							| 
									
										
										
										
											2025-09-03 15:30:31 -04:00
										 |  |  |         params(download_queue: T.nilable(::Homebrew::DownloadQueue), stale_seconds: T.nilable(Integer)) | 
					
						
							| 
									
										
										
										
											2025-07-18 15:01:34 +01:00
										 |  |  |           .returns([T.any(T::Array[T.untyped], T::Hash[String, T.untyped]), T::Boolean]) | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2025-09-03 15:30:31 -04:00
										 |  |  |       def self.fetch_api!(download_queue: nil, stale_seconds: nil) | 
					
						
							| 
									
										
										
										
											2025-08-11 00:36:10 -04:00
										 |  |  |         Homebrew::API.fetch_json_api_file DEFAULT_API_FILENAME, stale_seconds:, download_queue: | 
					
						
							| 
									
										
										
										
											2025-07-18 15:01:34 +01:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       sig { | 
					
						
							| 
									
										
										
										
											2025-09-03 15:30:31 -04:00
										 |  |  |         params(download_queue: T.nilable(::Homebrew::DownloadQueue), stale_seconds: T.nilable(Integer)) | 
					
						
							| 
									
										
										
										
											2025-07-18 15:01:34 +01:00
										 |  |  |           .returns([T.any(T::Array[T.untyped], T::Hash[String, T.untyped]), T::Boolean]) | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2025-09-03 15:30:31 -04:00
										 |  |  |       def self.fetch_tap_migrations!(download_queue: nil, stale_seconds: nil) | 
					
						
							| 
									
										
										
										
											2025-07-18 15:01:34 +01:00
										 |  |  |         Homebrew::API.fetch_json_api_file "cask_tap_migrations.jws.json", stale_seconds:, download_queue: | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |       sig { returns(T::Boolean) } | 
					
						
							|  |  |  |       def self.download_and_cache_data! | 
					
						
							| 
									
										
										
										
											2025-07-18 15:01:34 +01:00
										 |  |  |         json_casks, updated = fetch_api! | 
					
						
							| 
									
										
										
										
											2023-04-08 14:10:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |         cache["renames"] = {} | 
					
						
							|  |  |  |         cache["casks"] = json_casks.to_h do |json_cask| | 
					
						
							|  |  |  |           token = json_cask["token"] | 
					
						
							| 
									
										
										
										
											2023-04-08 14:10:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |           json_cask.fetch("old_tokens", []).each do |old_token| | 
					
						
							|  |  |  |             cache["renames"][old_token] = token | 
					
						
							| 
									
										
										
										
											2023-02-16 21:49:03 +00:00
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |           [token, json_cask.except("token")] | 
					
						
							| 
									
										
										
										
											2023-02-16 21:49:03 +00:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |         updated | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       private_class_method :download_and_cache_data! | 
					
						
							| 
									
										
										
										
											2023-02-16 21:49:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-17 14:56:10 +01:00
										 |  |  |       sig { returns(T::Hash[String, T::Hash[String, T.untyped]]) } | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |       def self.all_casks | 
					
						
							|  |  |  |         unless cache.key?("casks") | 
					
						
							|  |  |  |           json_updated = download_and_cache_data! | 
					
						
							|  |  |  |           write_names(regenerate: json_updated) | 
					
						
							| 
									
										
										
										
											2023-04-08 14:10:58 +02:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |         cache.fetch("casks") | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-04-08 14:10:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |       sig { returns(T::Hash[String, String]) } | 
					
						
							|  |  |  |       def self.all_renames | 
					
						
							|  |  |  |         unless cache.key?("renames") | 
					
						
							|  |  |  |           json_updated = download_and_cache_data! | 
					
						
							|  |  |  |           write_names(regenerate: json_updated) | 
					
						
							| 
									
										
										
										
											2023-02-16 21:49:03 +00:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |         cache.fetch("renames") | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-02-16 21:49:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-18 15:01:34 +01:00
										 |  |  |       sig { returns(T::Hash[String, T.untyped]) } | 
					
						
							|  |  |  |       def self.tap_migrations | 
					
						
							|  |  |  |         unless cache.key?("tap_migrations") | 
					
						
							|  |  |  |           json_migrations, = fetch_tap_migrations! | 
					
						
							|  |  |  |           cache["tap_migrations"] = json_migrations | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cache.fetch("tap_migrations") | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |       sig { params(regenerate: T::Boolean).void } | 
					
						
							|  |  |  |       def self.write_names(regenerate: false) | 
					
						
							|  |  |  |         download_and_cache_data! unless cache.key?("casks") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-14 14:48:08 +01:00
										 |  |  |         Homebrew::API.write_names_file!(all_casks.keys, "cask", regenerate:) | 
					
						
							| 
									
										
										
										
											2021-08-06 02:30:44 -04:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |