| 
									
										
										
										
											2023-03-13 18:31:26 -07:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 18:25:08 +09:00
										 |  |  | require "cli/parser" | 
					
						
							| 
									
										
										
										
											2019-01-18 22:17:46 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 22:41:47 -05:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   extend T::Sig | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   sig { returns(CLI::Parser) } | 
					
						
							| 
									
										
										
										
											2023-03-13 18:31:26 -07:00
										 |  |  |   def self.cat_args | 
					
						
							| 
									
										
										
										
											2019-01-18 22:17:46 +05:30
										 |  |  |     Homebrew::CLI::Parser.new do | 
					
						
							| 
									
										
										
										
											2021-01-15 15:04:02 -05:00
										 |  |  |       description <<~EOS | 
					
						
							| 
									
										
										
										
											2020-11-19 16:01:04 +01:00
										 |  |  |         Display the source of a <formula> or <cask>. | 
					
						
							| 
									
										
										
										
											2019-01-18 22:17:46 +05:30
										 |  |  |       EOS | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 16:01:04 +01:00
										 |  |  |       switch "--formula", "--formulae", | 
					
						
							|  |  |  |              description: "Treat all named arguments as formulae." | 
					
						
							|  |  |  |       switch "--cask", "--casks", | 
					
						
							|  |  |  |              description: "Treat all named arguments as casks." | 
					
						
							| 
									
										
										
										
											2020-11-27 11:41:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 16:01:04 +01:00
										 |  |  |       conflicts "--formula", "--cask" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-10 22:07:10 +08:00
										 |  |  |       named_args [:formula, :cask], min: 1
 | 
					
						
							| 
									
										
										
										
											2019-01-18 22:17:46 +05:30
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 18:31:26 -07:00
										 |  |  |   def self.cat | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |     args = cat_args.parse | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 18:31:26 -07:00
										 |  |  |     cd HOMEBREW_REPOSITORY do | 
					
						
							|  |  |  |       pager = if Homebrew::EnvConfig.bat? | 
					
						
							|  |  |  |         ENV["BAT_CONFIG_PATH"] = Homebrew::EnvConfig.bat_config_path | 
					
						
							|  |  |  |         ENV["BAT_THEME"] = Homebrew::EnvConfig.bat_theme | 
					
						
							|  |  |  |         ensure_formula_installed!( | 
					
						
							|  |  |  |           "bat", | 
					
						
							|  |  |  |           reason:           "displaying <formula>/<cask> source", | 
					
						
							|  |  |  |           # The user might want to capture the output of `brew cat ...` | 
					
						
							|  |  |  |           # Redirect stdout to stderr | 
					
						
							|  |  |  |           output_to_stderr: true, | 
					
						
							|  |  |  |         ).opt_bin/"bat" | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         "cat" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-11-19 16:01:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 18:31:26 -07:00
										 |  |  |       args.named.to_paths.each do |path| | 
					
						
							|  |  |  |         next path if path.exist? | 
					
						
							| 
									
										
										
										
											2023-02-27 17:34:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 18:31:26 -07:00
										 |  |  |         path = path.basename(".rb") if args.cask? | 
					
						
							| 
									
										
										
										
											2023-02-27 17:34:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 18:31:26 -07:00
										 |  |  |         ofail "#{path}'s source doesn't exist on disk." | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-02-27 17:34:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 18:31:26 -07:00
										 |  |  |       if Homebrew.failed? | 
					
						
							|  |  |  |         $stderr.puts "The name may be wrong, or the tap hasn't been tapped. Instead try:" | 
					
						
							|  |  |  |         treat_as = "--cask " if args.cask? | 
					
						
							|  |  |  |         treat_as = "--formula " if args.formula? | 
					
						
							|  |  |  |         $stderr.puts "  brew info --github #{treat_as}#{args.named.join(" ")}" | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-02-27 17:34:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 18:31:26 -07:00
										 |  |  |       safe_system pager, *args.named.to_paths | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | end |