| 
									
										
										
										
											2024-03-18 20:31:44 -07:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 20:31:44 -07:00
										 |  |  | require "abstract_command" | 
					
						
							| 
									
										
										
										
											2024-03-21 21:31:25 -07:00
										 |  |  | require "fileutils" | 
					
						
							| 
									
										
										
										
											2019-01-18 22:17:46 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 22:41:47 -05:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2024-03-18 20:31:44 -07:00
										 |  |  |   module DevCmd | 
					
						
							|  |  |  |     class Cat < AbstractCommand | 
					
						
							|  |  |  |       include FileUtils | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 20:31:44 -07:00
										 |  |  |       cmd_args do | 
					
						
							|  |  |  |         description <<~EOS | 
					
						
							|  |  |  |           Display the source of a <formula> or <cask>. | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |         switch "--formula", "--formulae", | 
					
						
							|  |  |  |                description: "Treat all named arguments as formulae." | 
					
						
							|  |  |  |         switch "--cask", "--casks", | 
					
						
							|  |  |  |                description: "Treat all named arguments as casks." | 
					
						
							| 
									
										
										
										
											2020-11-19 16:01:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 20:31:44 -07:00
										 |  |  |         conflicts "--formula", "--cask" | 
					
						
							| 
									
										
										
										
											2019-01-18 22:17:46 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 20:31:44 -07:00
										 |  |  |         named_args [:formula, :cask], min: 1, without_api: true | 
					
						
							| 
									
										
										
										
											2023-03-13 18:31:26 -07:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-11-19 16:01:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 20:31:44 -07:00
										 |  |  |       sig { override.void } | 
					
						
							|  |  |  |       def run | 
					
						
							|  |  |  |         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 | 
					
						
							| 
									
										
										
										
											2025-08-02 01:30:01 +08:00
										 |  |  |             require "formula" | 
					
						
							| 
									
										
										
										
											2025-08-02 03:37:31 +08:00
										 |  |  |             Formula["bat"].ensure_installed!( | 
					
						
							| 
									
										
										
										
											2024-03-18 20:31:44 -07:00
										 |  |  |               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 | 
					
						
							| 
									
										
										
										
											2023-02-27 17:34:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 20:31:44 -07:00
										 |  |  |           args.named.to_paths.each do |path| | 
					
						
							|  |  |  |             next path if path.exist? | 
					
						
							| 
									
										
										
										
											2023-02-27 17:34:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 20:31:44 -07:00
										 |  |  |             path = path.basename(".rb") if args.cask? | 
					
						
							| 
									
										
										
										
											2023-02-27 17:34:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 20:31:44 -07:00
										 |  |  |             ofail "#{path}'s source doesn't exist on disk." | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 20:31:44 -07:00
										 |  |  |           safe_system pager, *args.named.to_paths | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-03-13 18:31:26 -07:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | end |