| 
									
										
										
										
											2023-04-04 22:13:58 -07:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  | require "stringio" | 
					
						
							|  |  |  | require "formula" | 
					
						
							| 
									
										
										
										
											2019-04-17 18:25:08 +09:00
										 |  |  | require "cli/parser" | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   sig { returns(CLI::Parser) } | 
					
						
							| 
									
										
										
										
											2023-04-04 22:13:58 -07:00
										 |  |  |   def self.unpack_args | 
					
						
							| 
									
										
										
										
											2018-11-11 18:00:11 +05:30
										 |  |  |     Homebrew::CLI::Parser.new do | 
					
						
							| 
									
										
										
										
											2021-01-15 15:04:02 -05:00
										 |  |  |       description <<~EOS | 
					
						
							| 
									
										
										
										
											2019-01-30 21:32:35 +00:00
										 |  |  |         Unpack the source files for <formula> into subdirectories of the current | 
					
						
							| 
									
										
										
										
											2018-11-11 18:00:11 +05:30
										 |  |  |         working directory. | 
					
						
							|  |  |  |       EOS | 
					
						
							| 
									
										
										
										
											2019-08-06 13:23:19 -04:00
										 |  |  |       flag   "--destdir=", | 
					
						
							|  |  |  |              description: "Create subdirectories in the directory named by <path> instead." | 
					
						
							| 
									
										
										
										
											2018-11-11 18:00:11 +05:30
										 |  |  |       switch "--patch", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "Patches for <formula> will be applied to the unpacked source." | 
					
						
							| 
									
										
										
										
											2018-11-11 18:00:11 +05:30
										 |  |  |       switch "-g", "--git", | 
					
						
							| 
									
										
										
										
											2022-06-28 10:09:59 +01:00
										 |  |  |              description: "Initialise a Git repository in the unpacked source. This is useful for creating " \ | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |                           "patches for the software." | 
					
						
							| 
									
										
										
										
											2020-07-27 03:59:52 +02:00
										 |  |  |       switch "-f", "--force", | 
					
						
							|  |  |  |              description: "Overwrite the destination directory if it already exists." | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-29 19:39:41 +00:00
										 |  |  |       conflicts "--git", "--patch" | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |       named_args :formula, min: 1
 | 
					
						
							| 
									
										
										
										
											2018-11-11 18:00:11 +05:30
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 22:13:58 -07:00
										 |  |  |   def self.unpack | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |     args = unpack_args.parse | 
					
						
							| 
									
										
										
										
											2018-11-11 18:00:11 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 10:34:48 -04:00
										 |  |  |     formulae = args.named.to_formulae | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-12 18:33:37 +05:30
										 |  |  |     if (dir = args.destdir) | 
					
						
							| 
									
										
										
										
											2015-03-25 22:22:51 -04:00
										 |  |  |       unpack_dir = Pathname.new(dir).expand_path | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  |       unpack_dir.mkpath | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       unpack_dir = Pathname.pwd | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 00:02:22 -05:00
										 |  |  |     odie "Cannot write to #{unpack_dir}" unless unpack_dir.writable_real? | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     formulae.each do |f| | 
					
						
							| 
									
										
										
										
											2017-06-01 16:06:51 +02:00
										 |  |  |       stage_dir = unpack_dir/"#{f.name}-#{f.version}" | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if stage_dir.exist? | 
					
						
							| 
									
										
										
										
											2020-12-03 00:02:22 -05:00
										 |  |  |         odie "Destination #{stage_dir} already exists!" unless args.force? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  |         rm_rf stage_dir | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-09 11:31:51 +09:00
										 |  |  |       oh1 "Unpacking #{Formatter.identifier(f.full_name)} to: #{stage_dir}" | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 19:48:03 +02:00
										 |  |  |       # show messages about tar | 
					
						
							|  |  |  |       with_env VERBOSE: "1" do | 
					
						
							|  |  |  |         f.brew do | 
					
						
							|  |  |  |           f.patch if args.patch? | 
					
						
							|  |  |  |           cp_r getwd, stage_dir, preserve: true | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2015-04-24 14:38:27 +09:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 18:00:11 +05:30
										 |  |  |       next unless args.git? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 17:31:11 -04:00
										 |  |  |       ohai "Setting up Git repository" | 
					
						
							| 
									
										
										
										
											2023-04-04 22:13:58 -07:00
										 |  |  |       cd(stage_dir) do | 
					
						
							|  |  |  |         system "git", "init", "-q" | 
					
						
							|  |  |  |         system "git", "add", "-A" | 
					
						
							|  |  |  |         system "git", "commit", "-q", "-m", "brew-unpack" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-17 17:12:40 -05:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |