| 
									
										
										
										
											2024-08-10 22:17:05 +01:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 16:00:47 -07:00
										 |  |  | require "abstract_command" | 
					
						
							|  |  |  | require "fileutils" | 
					
						
							| 
									
										
										
										
											2016-04-17 09:19:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 22:41:47 -05:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2024-04-01 16:00:47 -07:00
										 |  |  |   module Cmd | 
					
						
							|  |  |  |     class Prefix < AbstractCommand | 
					
						
							|  |  |  |       include FileUtils | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       UNBREWED_EXCLUDE_FILES = %w[.DS_Store].freeze | 
					
						
							|  |  |  |       UNBREWED_EXCLUDE_PATHS = %w[
 | 
					
						
							|  |  |  |         */.keepme | 
					
						
							|  |  |  |         .github/* | 
					
						
							|  |  |  |         bin/brew | 
					
						
							|  |  |  |         completions/zsh/_brew | 
					
						
							|  |  |  |         docs/* | 
					
						
							|  |  |  |         lib/gdk-pixbuf-2.0/* | 
					
						
							|  |  |  |         lib/gio/* | 
					
						
							|  |  |  |         lib/node_modules/* | 
					
						
							|  |  |  |         lib/python[23].[0-9]/* | 
					
						
							|  |  |  |         lib/python3.[0-9][0-9]/* | 
					
						
							|  |  |  |         lib/pypy/* | 
					
						
							|  |  |  |         lib/pypy3/* | 
					
						
							|  |  |  |         lib/ruby/gems/[12].* | 
					
						
							|  |  |  |         lib/ruby/site_ruby/[12].* | 
					
						
							|  |  |  |         lib/ruby/vendor_ruby/[12].* | 
					
						
							|  |  |  |         manpages/brew.1
 | 
					
						
							|  |  |  |         share/pypy/* | 
					
						
							|  |  |  |         share/pypy3/* | 
					
						
							|  |  |  |         share/info/dir | 
					
						
							|  |  |  |         share/man/whatis | 
					
						
							|  |  |  |         share/mime/* | 
					
						
							|  |  |  |         texlive/* | 
					
						
							|  |  |  |       ].freeze | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       sig { override.returns(String) } | 
					
						
							|  |  |  |       def self.command_name = "--prefix" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       cmd_args do | 
					
						
							|  |  |  |         description <<~EOS | 
					
						
							|  |  |  |           Display Homebrew's install path. *Default:* | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             - macOS ARM: `#{HOMEBREW_MACOS_ARM_DEFAULT_PREFIX}` | 
					
						
							|  |  |  |             - macOS Intel: `#{HOMEBREW_DEFAULT_PREFIX}` | 
					
						
							|  |  |  |             - Linux: `#{HOMEBREW_LINUX_DEFAULT_PREFIX}` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           If <formula> is provided, display the location where <formula> is or would be installed. | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |         switch "--unbrewed", | 
					
						
							|  |  |  |                description: "List files in Homebrew's prefix not installed by Homebrew." | 
					
						
							|  |  |  |         switch "--installed", | 
					
						
							|  |  |  |                description: "Outputs nothing and returns a failing status code if <formula> is not installed." | 
					
						
							| 
									
										
										
										
											2025-06-03 13:01:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 16:00:47 -07:00
										 |  |  |         conflicts "--unbrewed", "--installed" | 
					
						
							| 
									
										
										
										
											2019-01-30 21:29:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 16:00:47 -07:00
										 |  |  |         named_args :formula | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2021-01-30 08:58:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 16:00:47 -07:00
										 |  |  |       sig { override.void } | 
					
						
							|  |  |  |       def run | 
					
						
							|  |  |  |         raise UsageError, "`--installed` requires a formula argument." if args.installed? && args.no_named? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if args.unbrewed? | 
					
						
							|  |  |  |           raise UsageError, "`--unbrewed` does not take a formula argument." unless args.no_named? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           list_unbrewed | 
					
						
							|  |  |  |         elsif args.no_named? | 
					
						
							|  |  |  |           puts HOMEBREW_PREFIX | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           formulae = args.named.to_resolved_formulae | 
					
						
							|  |  |  |           prefixes = formulae.filter_map do |f| | 
					
						
							|  |  |  |             next nil if args.installed? && !f.opt_prefix.exist? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # this case will be short-circuited by brew.sh logic for a single formula | 
					
						
							|  |  |  |             f.opt_prefix | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           puts prefixes | 
					
						
							|  |  |  |           if args.installed? | 
					
						
							|  |  |  |             missing_formulae = formulae.reject(&:optlinked?) | 
					
						
							|  |  |  |                                        .map(&:name) | 
					
						
							|  |  |  |             return if missing_formulae.blank? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             raise NotAKegError, <<~EOS | 
					
						
							|  |  |  |               The following formulae are not installed: | 
					
						
							|  |  |  |               #{missing_formulae.join(" ")} | 
					
						
							|  |  |  |             EOS | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-11-30 08:41:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 16:00:47 -07:00
										 |  |  |       private | 
					
						
							| 
									
										
										
										
											2021-02-24 16:02:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-10 22:17:05 +01:00
										 |  |  |       sig { void } | 
					
						
							| 
									
										
										
										
											2024-04-01 16:00:47 -07:00
										 |  |  |       def list_unbrewed | 
					
						
							|  |  |  |         dirs  = HOMEBREW_PREFIX.subdirs.map { |dir| dir.basename.to_s } | 
					
						
							|  |  |  |         dirs -= %w[Library Cellar Caskroom .git] | 
					
						
							| 
									
										
										
										
											2020-11-30 08:41:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-30 11:10:23 +02:00
										 |  |  |         # Exclude cache, logs and repository, if they are located under the prefix. | 
					
						
							| 
									
										
										
										
											2024-04-01 16:00:47 -07:00
										 |  |  |         [HOMEBREW_CACHE, HOMEBREW_LOGS, HOMEBREW_REPOSITORY].each do |dir| | 
					
						
							|  |  |  |           dirs.delete dir.relative_path_from(HOMEBREW_PREFIX).to_s | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         dirs.delete "etc" | 
					
						
							|  |  |  |         dirs.delete "var" | 
					
						
							| 
									
										
										
										
											2020-11-30 08:41:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 16:00:47 -07:00
										 |  |  |         arguments = dirs.sort + %w[-type f (] | 
					
						
							|  |  |  |         arguments.concat UNBREWED_EXCLUDE_FILES.flat_map { |f| %W[! -name #{f}] } | 
					
						
							|  |  |  |         arguments.concat UNBREWED_EXCLUDE_PATHS.flat_map { |d| %W[! -path #{d}] } | 
					
						
							|  |  |  |         arguments.push ")" | 
					
						
							| 
									
										
										
										
											2020-11-30 08:41:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 16:00:47 -07:00
										 |  |  |         cd(HOMEBREW_PREFIX) { safe_system("find", *arguments) } | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2020-11-30 08:41:48 -05:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | end |