| 
									
										
										
										
											2023-02-11 22:16:57 -08:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2021-08-06 02:30:44 -04:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-16 21:49:03 +00:00
										 |  |  | require "extend/cachable" | 
					
						
							| 
									
										
										
										
											2023-04-18 00:22:13 +01:00
										 |  |  | require "api/download" | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |       sig { params(token: String).returns(Hash) } | 
					
						
							|  |  |  |       def self.fetch(token) | 
					
						
							|  |  |  |         Homebrew::API.fetch "cask/#{token}.json" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2022-12-29 02:48:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |       sig { params(cask: ::Cask::Cask).returns(::Cask::Cask) } | 
					
						
							|  |  |  |       def self.source_download(cask) | 
					
						
							|  |  |  |         path = cask.ruby_source_path.to_s || "Casks/#{cask.token}.rb" | 
					
						
							|  |  |  |         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" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         download = Homebrew::API::Download.new( | 
					
						
							|  |  |  |           "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", | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         download.fetch | 
					
						
							|  |  |  |         ::Cask::CaskLoader::FromPathLoader.new(download.symlink_location) | 
					
						
							|  |  |  |                                           .load(config: cask.config) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-02-16 21:49:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-03 13:41:52 -04:00
										 |  |  |       def self.cached_json_file_path | 
					
						
							| 
									
										
										
										
											2024-07-04 12:08:01 -04:00
										 |  |  |         HOMEBREW_CACHE_API/DEFAULT_API_FILENAME | 
					
						
							| 
									
										
										
										
											2024-07-03 13:41:52 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |       sig { returns(T::Boolean) } | 
					
						
							|  |  |  |       def self.download_and_cache_data! | 
					
						
							| 
									
										
										
										
											2024-07-04 12:08:01 -04:00
										 |  |  |         json_casks, updated = Homebrew::API.fetch_json_api_file DEFAULT_API_FILENAME | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-26 21:21:38 -08:00
										 |  |  |       sig { returns(T::Hash[String, Hash]) } | 
					
						
							|  |  |  |       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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |         Homebrew::API.write_names_file(all_casks.keys, "cask", regenerate:) | 
					
						
							| 
									
										
										
										
											2021-08-06 02:30:44 -04:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |