| 
									
										
										
										
											2019-01-20 20:42:15 +05:30
										 |  |  | require "cli_parser" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 22:41:47 -05:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2016-09-26 01:44:51 +02:00
										 |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-20 20:42:15 +05:30
										 |  |  |   def commands_args | 
					
						
							|  |  |  |     Homebrew::CLI::Parser.new do | 
					
						
							|  |  |  |       usage_banner <<~EOS | 
					
						
							|  |  |  |         `commands` [<options>] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Show a list of built-in and external commands. | 
					
						
							|  |  |  |       EOS | 
					
						
							|  |  |  |       switch "--quiet", | 
					
						
							|  |  |  |         description: "List only the names of commands without the header." | 
					
						
							|  |  |  |       switch "--include-aliases", | 
					
						
							|  |  |  |         depends_on:  "--quiet", | 
					
						
							|  |  |  |         description: "Include the aliases of internal commands." | 
					
						
							|  |  |  |       switch :verbose | 
					
						
							|  |  |  |       switch :debug | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-14 16:58:26 -07:00
										 |  |  |   def commands | 
					
						
							| 
									
										
										
										
											2019-01-20 20:42:15 +05:30
										 |  |  |     commands_args.parse | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if args.quiet? | 
					
						
							| 
									
										
										
										
											2017-11-05 15:37:57 +00:00
										 |  |  |       cmds = internal_commands | 
					
						
							|  |  |  |       cmds += external_commands | 
					
						
							| 
									
										
										
										
											2016-09-07 20:08:51 +01:00
										 |  |  |       cmds += internal_developer_commands | 
					
						
							| 
									
										
										
										
											2019-01-20 20:42:15 +05:30
										 |  |  |       cmds += HOMEBREW_INTERNAL_COMMAND_ALIASES.keys if args.include_aliases? | 
					
						
							| 
									
										
										
										
											2016-10-02 20:39:43 +02:00
										 |  |  |       puts Formatter.columns(cmds.sort) | 
					
						
							| 
									
										
										
										
											2017-11-05 15:37:57 +00:00
										 |  |  |       return | 
					
						
							| 
									
										
										
										
											2013-09-14 16:58:26 -07:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-11-05 15:37:57 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Find commands in Homebrew/cmd | 
					
						
							|  |  |  |     puts "Built-in commands" | 
					
						
							|  |  |  |     puts Formatter.columns(internal_commands.sort) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Find commands in Homebrew/dev-cmd | 
					
						
							|  |  |  |     puts | 
					
						
							|  |  |  |     puts "Built-in developer commands" | 
					
						
							|  |  |  |     puts Formatter.columns(internal_developer_commands.sort) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     exts = external_commands | 
					
						
							|  |  |  |     return if exts.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Find commands in the PATH | 
					
						
							|  |  |  |     puts | 
					
						
							|  |  |  |     puts "External commands" | 
					
						
							|  |  |  |     puts Formatter.columns(exts) | 
					
						
							| 
									
										
										
										
											2013-09-14 16:58:26 -07:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2015-07-09 19:50:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def internal_commands | 
					
						
							| 
									
										
										
										
											2016-01-18 00:55:50 +01:00
										 |  |  |     find_internal_commands HOMEBREW_LIBRARY_PATH/"cmd" | 
					
						
							| 
									
										
										
										
											2015-09-12 19:24:41 +08:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 20:08:51 +01:00
										 |  |  |   def internal_developer_commands | 
					
						
							| 
									
										
										
										
											2016-01-18 00:55:50 +01:00
										 |  |  |     find_internal_commands HOMEBREW_LIBRARY_PATH/"dev-cmd" | 
					
						
							| 
									
										
										
										
											2015-07-09 19:50:53 +08:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def external_commands | 
					
						
							| 
									
										
										
										
											2017-11-05 15:37:57 +00:00
										 |  |  |     cmd_paths = PATH.new(ENV["PATH"]).append(Tap.cmd_directories) | 
					
						
							|  |  |  |     cmd_paths.each_with_object([]) do |path, cmds| | 
					
						
							| 
									
										
										
										
											2016-01-18 00:55:50 +01:00
										 |  |  |       Dir["#{path}/brew-*"].each do |file| | 
					
						
							|  |  |  |         next unless File.executable?(file) | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-18 00:55:50 +01:00
										 |  |  |         cmd = File.basename(file, ".rb")[5..-1] | 
					
						
							| 
									
										
										
										
											2017-11-05 15:37:57 +00:00
										 |  |  |         next if cmd.include?(".") | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-05 15:37:57 +00:00
										 |  |  |         cmds << cmd | 
					
						
							| 
									
										
										
										
											2016-01-18 00:55:50 +01:00
										 |  |  |       end | 
					
						
							|  |  |  |     end.sort | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def find_internal_commands(directory) | 
					
						
							| 
									
										
										
										
											2017-10-20 10:19:11 +01:00
										 |  |  |     Pathname.glob(directory/"*") | 
					
						
							|  |  |  |             .select(&:file?) | 
					
						
							|  |  |  |             .map { |f| f.basename.to_s.sub(/\.(?:rb|sh)$/, "") } | 
					
						
							| 
									
										
										
										
											2015-07-09 19:50:53 +08:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2013-09-14 16:58:26 -07:00
										 |  |  | end |