| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-05 13:50:54 -05:00
										 |  |  | module Utils | 
					
						
							| 
									
										
										
										
											2017-09-19 10:18:04 -07:00
										 |  |  |   def self.popen_read(*args, **options, &block) | 
					
						
							|  |  |  |     popen(args, "rb", options, &block) | 
					
						
							| 
									
										
										
										
											2014-07-05 13:50:54 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-22 16:56:37 -07:00
										 |  |  |   def self.safe_popen_read(*args, **options, &block) | 
					
						
							|  |  |  |     output = popen_read(*args, **options, &block) | 
					
						
							| 
									
										
										
										
											2018-08-01 06:25:31 +02:00
										 |  |  |     return output if $CHILD_STATUS.success? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-01 06:25:31 +02:00
										 |  |  |     raise ErrorDuringExecution.new(args, status: $CHILD_STATUS, output: [[:stdout, output]]) | 
					
						
							| 
									
										
										
										
											2018-06-22 16:56:37 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-19 10:18:04 -07:00
										 |  |  |   def self.popen_write(*args, **options, &block) | 
					
						
							|  |  |  |     popen(args, "wb", options, &block) | 
					
						
							| 
									
										
										
										
											2014-07-05 13:50:54 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-22 16:56:37 -07:00
										 |  |  |   def self.safe_popen_write(*args, **options, &block) | 
					
						
							| 
									
										
										
										
											2018-07-16 23:17:16 +02:00
										 |  |  |     output = popen_write(*args, **options, &block) | 
					
						
							| 
									
										
										
										
											2018-08-01 06:25:31 +02:00
										 |  |  |     return output if $CHILD_STATUS.success? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-01 06:25:31 +02:00
										 |  |  |     raise ErrorDuringExecution.new(args, status: $CHILD_STATUS, output: [[:stdout, output]]) | 
					
						
							| 
									
										
										
										
											2018-06-22 16:56:37 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-19 10:18:04 -07:00
										 |  |  |   def self.popen(args, mode, options = {}) | 
					
						
							| 
									
										
										
										
											2014-07-05 13:50:54 -05:00
										 |  |  |     IO.popen("-", mode) do |pipe| | 
					
						
							|  |  |  |       if pipe | 
					
						
							| 
									
										
										
										
											2016-09-23 22:02:23 +02:00
										 |  |  |         return pipe.read unless block_given? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 22:02:23 +02:00
										 |  |  |         yield pipe | 
					
						
							| 
									
										
										
										
											2014-07-05 13:50:54 -05:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2017-09-20 11:58:52 -07:00
										 |  |  |         options[:err] ||= :close unless ENV["HOMEBREW_STDERR"] | 
					
						
							| 
									
										
										
										
											2017-12-01 15:00:27 -08:00
										 |  |  |         begin | 
					
						
							|  |  |  |           exec(*args, options) | 
					
						
							|  |  |  |         rescue Errno::ENOENT | 
					
						
							|  |  |  |           $stderr.puts "brew: command not found: #{args[0]}" unless options[:err] == :close | 
					
						
							|  |  |  |           exit! 127
 | 
					
						
							|  |  |  |         rescue SystemCallError | 
					
						
							|  |  |  |           $stderr.puts "brew: exec failed: #{args[0]}" unless options[:err] == :close | 
					
						
							|  |  |  |           exit! 1
 | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2014-07-05 13:50:54 -05:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |