| 
									
										
										
										
											2024-03-21 22:04:10 -07:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:04:10 -07:00
										 |  |  | require "abstract_command" | 
					
						
							|  |  |  | require "fileutils" | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  | require "stringio" | 
					
						
							|  |  |  | require "formula" | 
					
						
							| 
									
										
										
										
											2025-08-28 23:36:49 +00:00
										 |  |  | require "cask/download" | 
					
						
							|  |  |  | require "unpack_strategy" | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2024-03-21 22:04:10 -07:00
										 |  |  |   module DevCmd | 
					
						
							|  |  |  |     class Unpack < AbstractCommand | 
					
						
							|  |  |  |       include FileUtils | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:04:10 -07:00
										 |  |  |       cmd_args do | 
					
						
							|  |  |  |         description <<~EOS | 
					
						
							| 
									
										
										
										
											2025-09-01 10:56:00 +00:00
										 |  |  |           Unpack the files for the <formula> or <cask> into subdirectories of the current | 
					
						
							| 
									
										
										
										
											2024-03-21 22:04:10 -07:00
										 |  |  |           working directory. | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |         flag   "--destdir=", | 
					
						
							|  |  |  |                description: "Create subdirectories in the directory named by <path> instead." | 
					
						
							|  |  |  |         switch "--patch", | 
					
						
							|  |  |  |                description: "Patches for <formula> will be applied to the unpacked source." | 
					
						
							|  |  |  |         switch "-g", "--git", | 
					
						
							|  |  |  |                description: "Initialise a Git repository in the unpacked source. This is useful for creating " \ | 
					
						
							|  |  |  |                             "patches for the software." | 
					
						
							|  |  |  |         switch "-f", "--force", | 
					
						
							|  |  |  |                description: "Overwrite the destination directory if it already exists." | 
					
						
							| 
									
										
										
										
											2025-08-28 23:36:49 +00:00
										 |  |  |         switch "--formula", "--formulae", | 
					
						
							|  |  |  |                description: "Treat all named arguments as formulae." | 
					
						
							|  |  |  |         switch "--cask", "--casks", | 
					
						
							|  |  |  |                description: "Treat all named arguments as casks." | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:04:10 -07:00
										 |  |  |         conflicts "--git", "--patch" | 
					
						
							| 
									
										
										
										
											2025-08-28 23:36:49 +00:00
										 |  |  |         conflicts "--formula", "--cask" | 
					
						
							|  |  |  |         conflicts "--cask", "--patch" | 
					
						
							|  |  |  |         conflicts "--cask", "--git" | 
					
						
							| 
									
										
										
										
											2018-11-11 18:00:11 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 23:36:49 +00:00
										 |  |  |         named_args [:formula, :cask], min: 1
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:04:10 -07:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-11-11 18:00:11 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:04:10 -07:00
										 |  |  |       sig { override.void } | 
					
						
							|  |  |  |       def run | 
					
						
							| 
									
										
										
										
											2025-09-01 10:56:00 +00:00
										 |  |  |         formulae_and_casks = if args.casks? | 
					
						
							| 
									
										
										
										
											2025-08-29 08:38:17 +00:00
										 |  |  |           args.named.to_formulae_and_casks(only: :cask) | 
					
						
							| 
									
										
										
										
											2025-09-01 10:56:00 +00:00
										 |  |  |         elsif args.formulae? | 
					
						
							| 
									
										
										
										
											2025-08-29 08:38:17 +00:00
										 |  |  |           args.named.to_formulae_and_casks(only: :formula) | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           args.named.to_formulae_and_casks | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:04:10 -07:00
										 |  |  |         if (dir = args.destdir) | 
					
						
							|  |  |  |           unpack_dir = Pathname.new(dir).expand_path | 
					
						
							|  |  |  |           unpack_dir.mkpath | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           unpack_dir = Pathname.pwd | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-27 06:26:32 +00:00
										 |  |  |         odie "Cannot write to #{unpack_dir}" unless unpack_dir.writable? | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 23:36:49 +00:00
										 |  |  |         formulae_and_casks.each do |formula_or_cask| | 
					
						
							| 
									
										
										
										
											2025-09-02 15:23:04 +08:00
										 |  |  |           if formula_or_cask.is_a?(Cask::Cask) | 
					
						
							| 
									
										
										
										
											2025-08-28 23:36:49 +00:00
										 |  |  |             unpack_cask(formula_or_cask, unpack_dir) | 
					
						
							| 
									
										
										
										
											2025-09-02 15:23:04 +08:00
										 |  |  |           elsif (formula = T.cast(formula_or_cask, Formula)) | 
					
						
							|  |  |  |             unpack_formula(formula, unpack_dir) | 
					
						
							| 
									
										
										
										
											2025-08-28 23:36:49 +00:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 23:36:49 +00:00
										 |  |  |       private | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 23:36:49 +00:00
										 |  |  |       sig { params(formula: Formula, unpack_dir: Pathname).void } | 
					
						
							|  |  |  |       def unpack_formula(formula, unpack_dir) | 
					
						
							|  |  |  |         stage_dir = unpack_dir/"#{formula.name}-#{formula.version}" | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 23:36:49 +00:00
										 |  |  |         if stage_dir.exist? | 
					
						
							|  |  |  |           odie "Destination #{stage_dir} already exists!" unless args.force? | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 23:36:49 +00:00
										 |  |  |           rm_rf stage_dir | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 23:36:49 +00:00
										 |  |  |         oh1 "Unpacking #{Formatter.identifier(formula.full_name)} to: #{stage_dir}" | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 23:36:49 +00:00
										 |  |  |         # show messages about tar | 
					
						
							|  |  |  |         with_env VERBOSE: "1" do | 
					
						
							|  |  |  |           formula.brew do | 
					
						
							|  |  |  |             formula.patch if args.patch? | 
					
						
							|  |  |  |             cp_r getwd, stage_dir, preserve: true | 
					
						
							| 
									
										
										
										
											2024-03-21 22:04:10 -07:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2025-08-28 23:36:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return unless args.git? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ohai "Setting up Git repository" | 
					
						
							|  |  |  |         cd(stage_dir) do | 
					
						
							|  |  |  |           system "git", "init", "-q" | 
					
						
							|  |  |  |           system "git", "add", "-A" | 
					
						
							|  |  |  |           system "git", "commit", "-q", "-m", "brew-unpack" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       sig { params(cask: Cask::Cask, unpack_dir: Pathname).void } | 
					
						
							|  |  |  |       def unpack_cask(cask, unpack_dir) | 
					
						
							|  |  |  |         stage_dir = unpack_dir/"#{cask.token}-#{cask.version}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if stage_dir.exist? | 
					
						
							|  |  |  |           odie "Destination #{stage_dir} already exists!" unless args.force? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           rm_rf stage_dir | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         oh1 "Unpacking #{Formatter.identifier(cask.full_name)} to: #{stage_dir}" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-01 10:56:00 +00:00
										 |  |  |         download = Cask::Download.new(cask, quarantine: true) | 
					
						
							| 
									
										
										
										
											2025-09-01 10:15:00 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         downloaded_path = if download.downloaded? | 
					
						
							|  |  |  |           download.cached_download | 
					
						
							| 
									
										
										
										
											2025-09-01 10:56:00 +00:00
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2025-09-01 10:15:00 -07:00
										 |  |  |           download.fetch(quiet: false) | 
					
						
							| 
									
										
										
										
											2025-09-01 10:56:00 +00:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2025-08-28 23:36:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         stage_dir.mkpath | 
					
						
							|  |  |  |         UnpackStrategy.detect(downloaded_path).extract_nestedly(to: stage_dir, verbose: true) | 
					
						
							| 
									
										
										
										
											2023-04-04 22:13:58 -07:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |