| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  | require "formula" | 
					
						
							|  |  |  | require "ostruct" | 
					
						
							| 
									
										
										
										
											2019-04-17 18:25:08 +09:00
										 |  |  | require "cli/parser" | 
					
						
							| 
									
										
										
										
											2010-09-25 12:49:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 22:41:47 -05:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2016-09-26 01:44:51 +02:00
										 |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |   def deps_args | 
					
						
							|  |  |  |     Homebrew::CLI::Parser.new do | 
					
						
							|  |  |  |       usage_banner <<~EOS | 
					
						
							| 
									
										
										
										
											2019-08-06 14:17:17 -04:00
										 |  |  |         `deps` [<options>] [<formula>] | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 16:44:17 -04:00
										 |  |  |         Show dependencies for <formula>. Additional options specific to <formula> | 
					
						
							|  |  |  |         may be appended to the command. When given multiple formula arguments, | 
					
						
							|  |  |  |         show the intersection of dependencies for each formula. | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |       EOS | 
					
						
							|  |  |  |       switch "-n", | 
					
						
							| 
									
										
										
										
											2019-08-20 00:04:14 -04:00
										 |  |  |              description: "Sort dependencies in topological order." | 
					
						
							| 
									
										
										
										
											2019-03-14 16:44:17 -04:00
										 |  |  |       switch "--1", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "Only show dependencies one level down, instead of recursing." | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |       switch "--union", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "Show the union of dependencies for multiple <formula>, instead of the intersection." | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |       switch "--full-name", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "List dependencies by their full name." | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |       switch "--include-build", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "Include `:build` dependencies for <formula>." | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |       switch "--include-optional", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "Include `:optional` dependencies for <formula>." | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |       switch "--include-test", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "Include `:test` dependencies for <formula> (non-recursive)." | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |       switch "--skip-recommended", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "Skip `:recommended` dependencies for <formula>." | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |       switch "--include-requirements", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "Include requirements in addition to dependencies for <formula>." | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |       switch "--tree", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "Show dependencies as a tree. When given multiple formula arguments, "\ | 
					
						
							|  |  |  |                           "show individual trees for each formula." | 
					
						
							| 
									
										
										
										
											2019-03-06 23:44:46 -05:00
										 |  |  |       switch "--annotate", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "Mark any build, test, optional, or recommended dependencies as "\ | 
					
						
							|  |  |  |                           "such in the output." | 
					
						
							| 
									
										
										
										
											2019-03-14 16:44:17 -04:00
										 |  |  |       switch "--installed", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "List dependencies for formulae that are currently installed. If <formula> is "\ | 
					
						
							|  |  |  |                           "specified, list only its dependencies that are currently installed." | 
					
						
							| 
									
										
										
										
											2019-03-14 16:44:17 -04:00
										 |  |  |       switch "--all", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "List dependencies for all available formulae." | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |       switch "--for-each", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "Switch into the mode used by the `--all` option, but only list dependencies "\ | 
					
						
							| 
									
										
										
										
											2019-08-20 00:04:14 -04:00
										 |  |  |                           "for each provided <formula>, one formula per line. This is used for "\ | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |                           "debugging the `--installed`/`--all` display mode." | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |       switch :verbose | 
					
						
							|  |  |  |       switch :debug | 
					
						
							| 
									
										
										
										
											2019-01-29 19:39:41 +00:00
										 |  |  |       conflicts "--installed", "--all" | 
					
						
							| 
									
										
										
										
											2019-01-30 13:18:02 +00:00
										 |  |  |       formula_options | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |   def deps | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |     deps_args.parse | 
					
						
							| 
									
										
										
										
											2019-11-06 10:20:33 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Formulary.enable_factory_cache! | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-06 10:20:20 +00:00
										 |  |  |     recursive = !args.send("1?") | 
					
						
							| 
									
										
										
										
											2020-03-04 17:28:15 +00:00
										 |  |  |     installed = args.installed? || args.formulae.all?(&:opt_or_installed_prefix_keg) | 
					
						
							| 
									
										
										
										
											2020-02-12 22:30:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @use_runtime_dependencies = installed && recursive && | 
					
						
							|  |  |  |                                 !args.include_build? && | 
					
						
							|  |  |  |                                 !args.include_test? && | 
					
						
							|  |  |  |                                 !args.include_optional? && | 
					
						
							|  |  |  |                                 !args.skip_recommended? | 
					
						
							| 
									
										
										
										
											2019-11-06 10:20:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if args.tree? | 
					
						
							|  |  |  |       if args.installed? | 
					
						
							|  |  |  |         puts_deps_tree Formula.installed.sort, recursive | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2020-03-04 17:28:15 +00:00
										 |  |  |         raise FormulaUnspecifiedError if args.no_named? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 17:28:15 +00:00
										 |  |  |         puts_deps_tree args.formulae, recursive | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  |       return | 
					
						
							| 
									
										
										
										
											2019-11-06 10:20:20 +00:00
										 |  |  |     elsif args.all? | 
					
						
							|  |  |  |       puts_deps Formula.sort, recursive | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  |       return | 
					
						
							| 
									
										
										
										
											2020-03-04 17:28:15 +00:00
										 |  |  |     elsif !args.no_named? && args.for_each? | 
					
						
							|  |  |  |       puts_deps args.formulae, recursive | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  |       return | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 17:28:15 +00:00
										 |  |  |     if args.no_named? | 
					
						
							| 
									
										
										
										
											2019-11-06 10:20:20 +00:00
										 |  |  |       raise FormulaUnspecifiedError unless args.installed? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-06 10:20:20 +00:00
										 |  |  |       puts_deps Formula.installed.sort, recursive | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  |       return | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 17:28:15 +00:00
										 |  |  |     all_deps = deps_for_formulae(args.formulae, recursive, &(args.union? ? :| : :&)) | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  |     all_deps = condense_requirements(all_deps) | 
					
						
							| 
									
										
										
										
											2019-11-06 10:20:20 +00:00
										 |  |  |     all_deps.select!(&:installed?) if args.installed? | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  |     all_deps.map!(&method(:dep_display_name)) | 
					
						
							|  |  |  |     all_deps.uniq! | 
					
						
							| 
									
										
										
										
											2019-11-06 10:20:20 +00:00
										 |  |  |     all_deps.sort! unless args.n? | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  |     puts all_deps | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2013-06-09 12:59:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |   def condense_requirements(deps) | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |     return deps if args.include_requirements? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  |     deps.select { |dep| dep.is_a? Dependency } | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def dep_display_name(dep) | 
					
						
							|  |  |  |     str = if dep.is_a? Requirement | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |       if args.include_requirements? | 
					
						
							| 
									
										
										
										
											2018-01-14 13:27:43 +00:00
										 |  |  |         ":#{dep.display_s}" | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |       else | 
					
						
							|  |  |  |         # This shouldn't happen, but we'll put something here to help debugging | 
					
						
							|  |  |  |         "::#{dep.name}" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |     elsif args.full_name? | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  |       dep.to_formula.full_name | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  |       dep.name | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |     if args.annotate? | 
					
						
							| 
									
										
										
										
											2019-03-06 23:44:46 -05:00
										 |  |  |       str = "#{str} " if args.tree? | 
					
						
							|  |  |  |       str = "#{str} [build]" if dep.build? | 
					
						
							|  |  |  |       str = "#{str} [test]" if dep.test? | 
					
						
							|  |  |  |       str = "#{str} [optional]" if dep.optional? | 
					
						
							|  |  |  |       str = "#{str} [recommended]" if dep.recommended? | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |     str | 
					
						
							| 
									
										
										
										
											2016-12-20 03:39:30 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   def deps_for_formula(f, recursive = false) | 
					
						
							| 
									
										
										
										
											2018-03-24 16:55:16 +00:00
										 |  |  |     includes, ignores = argv_includes_ignores(ARGV) | 
					
						
							| 
									
										
										
										
											2015-03-20 21:31:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-27 12:36:21 +00:00
										 |  |  |     deps = f.runtime_dependencies if @use_runtime_dependencies | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-29 17:27:21 -04:00
										 |  |  |     if recursive | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  |       deps ||= recursive_includes(Dependency,  f, includes, ignores) | 
					
						
							|  |  |  |       reqs   = recursive_includes(Requirement, f, includes, ignores) | 
					
						
							| 
									
										
										
										
											2013-10-29 17:27:21 -04:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  |       deps ||= reject_ignores(f.deps, ignores, includes) | 
					
						
							|  |  |  |       reqs   = reject_ignores(f.requirements, ignores, includes) | 
					
						
							| 
									
										
										
										
											2013-10-29 17:27:21 -04:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-02-27 12:56:42 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |     deps + reqs.to_a | 
					
						
							| 
									
										
										
										
											2013-10-29 17:27:21 -04:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   def deps_for_formulae(formulae, recursive = false, &block) | 
					
						
							| 
									
										
										
										
											2018-09-02 20:14:54 +01:00
										 |  |  |     formulae.map { |f| deps_for_formula(f, recursive) }.reduce(&block) | 
					
						
							| 
									
										
										
										
											2013-06-22 12:54:45 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-06 23:44:46 -05:00
										 |  |  |   def puts_deps(formulae, recursive = false) | 
					
						
							| 
									
										
										
										
											2016-12-20 03:39:30 -05:00
										 |  |  |     formulae.each do |f| | 
					
						
							| 
									
										
										
										
											2019-03-06 23:44:46 -05:00
										 |  |  |       deps = deps_for_formula(f, recursive) | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |       deps = condense_requirements(deps) | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  |       deps.sort_by!(&:name) | 
					
						
							|  |  |  |       deps.map!(&method(:dep_display_name)) | 
					
						
							| 
									
										
										
										
											2016-12-20 03:39:30 -05:00
										 |  |  |       puts "#{f.full_name}: #{deps.join(" ")}" | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2013-06-22 12:54:45 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |   def puts_deps_tree(formulae, recursive = false) | 
					
						
							| 
									
										
										
										
											2013-06-22 12:54:45 -05:00
										 |  |  |     formulae.each do |f| | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |       puts f.full_name | 
					
						
							|  |  |  |       @dep_stack = [] | 
					
						
							|  |  |  |       recursive_deps_tree(f, "", recursive) | 
					
						
							| 
									
										
										
										
											2013-06-22 12:54:45 -05:00
										 |  |  |       puts | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |   def recursive_deps_tree(f, prefix, recursive) | 
					
						
							| 
									
										
										
										
											2019-12-27 17:35:18 -05:00
										 |  |  |     includes, ignores = argv_includes_ignores(ARGV) | 
					
						
							| 
									
										
										
										
											2020-02-12 22:30:44 +01:00
										 |  |  |     dependables = @use_runtime_dependencies ? f.runtime_dependencies : f.deps | 
					
						
							|  |  |  |     deps = reject_ignores(dependables, ignores, includes) | 
					
						
							| 
									
										
										
										
											2019-12-27 17:35:18 -05:00
										 |  |  |     reqs = reject_ignores(f.requirements, ignores, includes) | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |     dependables = reqs + deps | 
					
						
							| 
									
										
										
										
											2019-12-27 17:35:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |     max = dependables.length - 1
 | 
					
						
							|  |  |  |     @dep_stack.push f.name | 
					
						
							|  |  |  |     dependables.each_with_index do |dep, i| | 
					
						
							| 
									
										
										
										
											2019-01-23 08:34:24 +05:30
										 |  |  |       next if !args.include_requirements? && dep.is_a?(Requirement) | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |       tree_lines = if i == max | 
					
						
							| 
									
										
										
										
											2016-12-20 03:59:15 -05:00
										 |  |  |         "└──" | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         "├──" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |       display_s = "#{tree_lines} #{dep_display_name(dep)}" | 
					
						
							|  |  |  |       is_circular = @dep_stack.include?(dep.name) | 
					
						
							|  |  |  |       display_s = "#{display_s} (CIRCULAR DEPENDENCY)" if is_circular | 
					
						
							|  |  |  |       puts "#{prefix}#{display_s}" | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |       next if !recursive || is_circular | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |       prefix_addition = if i == max | 
					
						
							|  |  |  |         "    " | 
					
						
							| 
									
										
										
										
											2016-12-20 03:59:15 -05:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |         "│   " | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-19 13:11:32 +00:00
										 |  |  |       recursive_deps_tree(Formulary.factory(dep.name), prefix + prefix_addition, true) if dep.is_a? Dependency | 
					
						
							| 
									
										
										
										
											2013-06-09 12:59:42 -05:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-03-26 10:55:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-03 00:41:51 -04:00
										 |  |  |     @dep_stack.pop | 
					
						
							| 
									
										
										
										
											2013-06-09 12:59:42 -05:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | end |