| 
									
										
										
										
											2020-10-20 00:56:50 +02:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-23 20:59:21 +02:00
										 |  |  | module UnpackStrategy | 
					
						
							| 
									
										
										
										
											2020-08-09 06:09:05 +02:00
										 |  |  |   # Strategy for unpacking ZIP archives. | 
					
						
							| 
									
										
										
										
											2018-07-23 20:59:21 +02:00
										 |  |  |   class Zip | 
					
						
							|  |  |  |     include UnpackStrategy | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-05 22:29:41 -07:00
										 |  |  |     sig { override.returns(T::Array[String]) } | 
					
						
							| 
									
										
										
										
											2018-07-30 09:49:59 +02:00
										 |  |  |     def self.extensions | 
					
						
							|  |  |  |       [".zip"] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-05 22:29:41 -07:00
										 |  |  |     sig { override.params(path: Pathname).returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2018-07-29 10:04:51 +02:00
										 |  |  |     def self.can_extract?(path) | 
					
						
							|  |  |  |       path.magic_number.match?(/\APK(\003\004|\005\006)/n) | 
					
						
							| 
									
										
										
										
											2018-07-23 20:59:21 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-17 22:45:55 -08:00
										 |  |  |     sig { | 
					
						
							| 
									
										
										
										
											2020-10-20 00:56:50 +02:00
										 |  |  |       override.params(unpack_dir: Pathname, basename: Pathname, verbose: T::Boolean) | 
					
						
							|  |  |  |               .returns(SystemCommand::Result) | 
					
						
							| 
									
										
										
										
											2021-01-17 22:45:55 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-23 20:59:21 +02:00
										 |  |  |     def extract_to_dir(unpack_dir, basename:, verbose:) | 
					
						
							| 
									
										
										
										
											2023-05-31 00:06:55 +08:00
										 |  |  |       unzip = if which("unzip").blank? | 
					
						
							|  |  |  |         begin | 
					
						
							|  |  |  |           Formula["unzip"] | 
					
						
							|  |  |  |         rescue FormulaUnavailableError | 
					
						
							|  |  |  |           nil | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2022-03-02 13:12:47 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-27 20:27:00 +01:00
										 |  |  |       with_env(TZ: "UTC") do | 
					
						
							|  |  |  |         quiet_flags = verbose ? [] : ["-qq"] | 
					
						
							|  |  |  |         result = system_command! "unzip", | 
					
						
							|  |  |  |                                  args:         [*quiet_flags, "-o", path, "-d", unpack_dir], | 
					
						
							| 
									
										
										
										
											2022-06-15 05:40:43 +01:00
										 |  |  |                                  env:          { "PATH" => PATH.new(unzip&.opt_bin, ENV.fetch("PATH")) }, | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |                                  verbose:, | 
					
						
							| 
									
										
										
										
											2021-04-27 20:27:00 +01:00
										 |  |  |                                  print_stderr: false | 
					
						
							| 
									
										
										
										
											2018-09-13 23:17:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-27 20:27:00 +01:00
										 |  |  |         FileUtils.rm_rf unpack_dir/"__MACOSX" | 
					
						
							| 
									
										
										
										
											2018-09-13 23:17:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-27 20:27:00 +01:00
										 |  |  |         result | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-07-23 20:59:21 +02:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							| 
									
										
										
										
											2018-08-01 07:41:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-20 13:00:53 -08:00
										 |  |  | require "extend/os/unpack_strategy/zip" |