| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | module Homebrew extend self | 
					
						
							|  |  |  |   def list | 
					
						
							| 
									
										
										
										
											2012-08-28 10:59:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Use of exec means we don't explicitly exit | 
					
						
							|  |  |  |     list_unbrewed if ARGV.flag? '--unbrewed' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Unbrewed uses the PREFIX, which will exist | 
					
						
							|  |  |  |     # Things below use the CELLAR, which doesn't until the first formula is installed. | 
					
						
							| 
									
										
										
										
											2012-08-28 10:52:51 -07:00
										 |  |  |     return unless HOMEBREW_CELLAR.exist? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-11 16:41:08 +01:00
										 |  |  |     if ARGV.include? '--pinned' | 
					
						
							|  |  |  |       require 'formula' | 
					
						
							|  |  |  |       list_pinned | 
					
						
							|  |  |  |     elsif ARGV.include? '--versions' | 
					
						
							| 
									
										
										
										
											2012-08-28 10:59:46 -07:00
										 |  |  |       list_versions | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |     elsif ARGV.named.empty? | 
					
						
							|  |  |  |       ENV['CLICOLOR'] = nil | 
					
						
							| 
									
										
										
										
											2012-08-28 10:59:46 -07:00
										 |  |  |       exec 'ls', *ARGV.options_only << HOMEBREW_CELLAR | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |     elsif ARGV.verbose? or not $stdout.tty? | 
					
						
							|  |  |  |       exec "find", *ARGV.kegs + %w[-not -type d -print] | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       ARGV.kegs.each{ |keg| PrettyListing.new keg } | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2012-08-28 10:59:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 00:49:56 -05:00
										 |  |  |   private | 
					
						
							| 
									
										
										
										
											2012-08-28 10:59:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-31 15:18:37 -05:00
										 |  |  |   UNBREWED_EXCLUDE_FILES = %w[.DS_Store] | 
					
						
							|  |  |  |   UNBREWED_EXCLUDE_PATHS = %w[
 | 
					
						
							|  |  |  |     bin/brew | 
					
						
							|  |  |  |     lib/gdk-pixbuf-2.0/* | 
					
						
							|  |  |  |     lib/gio/* | 
					
						
							|  |  |  |     lib/node_modules/* | 
					
						
							|  |  |  |     lib/python[23].[0-9]/* | 
					
						
							|  |  |  |     share/info/dir | 
					
						
							|  |  |  |     share/man/man1/brew.1
 | 
					
						
							|  |  |  |     share/man/whatis | 
					
						
							|  |  |  |   ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-28 10:59:46 -07:00
										 |  |  |   def list_unbrewed | 
					
						
							| 
									
										
										
										
											2013-07-31 15:18:37 -05:00
										 |  |  |     dirs  = HOMEBREW_PREFIX.subdirs.map { |dir| dir.basename.to_s } | 
					
						
							| 
									
										
										
										
											2012-08-28 10:59:46 -07:00
										 |  |  |     dirs -= %w[Library Cellar .git] | 
					
						
							| 
									
										
										
										
											2012-11-10 11:26:47 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-31 15:18:37 -05:00
										 |  |  |     # Exclude the repository and cache, if they are located under the prefix | 
					
						
							|  |  |  |     dirs.delete HOMEBREW_CACHE.relative_path_from(HOMEBREW_PREFIX).to_s | 
					
						
							|  |  |  |     dirs.delete HOMEBREW_REPOSITORY.relative_path_from(HOMEBREW_PREFIX).to_s | 
					
						
							|  |  |  |     dirs.delete 'etc' | 
					
						
							|  |  |  |     dirs.delete 'var' | 
					
						
							| 
									
										
										
										
											2012-11-12 09:56:45 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-31 15:18:37 -05:00
										 |  |  |     args = dirs + %w[-type f (] | 
					
						
							|  |  |  |     args.concat UNBREWED_EXCLUDE_FILES.map { |f| %W[! -name #{f}] }.flatten | 
					
						
							|  |  |  |     args.concat UNBREWED_EXCLUDE_PATHS.map { |d| %W[! -path #{d}] }.flatten | 
					
						
							|  |  |  |     args.concat %w[)] | 
					
						
							| 
									
										
										
										
											2012-11-10 11:26:47 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-28 10:59:46 -07:00
										 |  |  |     cd HOMEBREW_PREFIX | 
					
						
							| 
									
										
										
										
											2013-07-31 15:18:37 -05:00
										 |  |  |     exec 'find', *args | 
					
						
							| 
									
										
										
										
											2012-08-28 10:59:46 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def list_versions | 
					
						
							|  |  |  |     if ARGV.named.empty? | 
					
						
							|  |  |  |       HOMEBREW_CELLAR.children.select{ |pn| pn.directory? } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       ARGV.named.map{ |n| HOMEBREW_CELLAR+n }.select{ |pn| pn.exist? } | 
					
						
							|  |  |  |     end.each do |d| | 
					
						
							|  |  |  |       versions = d.children.select{ |pn| pn.directory? }.map{ |pn| pn.basename.to_s } | 
					
						
							|  |  |  |       puts "#{d.basename} #{versions*' '}" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2013-03-11 16:41:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def list_pinned | 
					
						
							|  |  |  |     if ARGV.named.empty? | 
					
						
							|  |  |  |       HOMEBREW_CELLAR.children.select{ |pn| pn.directory? } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       ARGV.named.map{ |n| HOMEBREW_CELLAR+n }.select{ |pn| pn.exist? } | 
					
						
							|  |  |  |     end.select do |d| | 
					
						
							| 
									
										
										
										
											2013-06-11 22:13:39 +02:00
										 |  |  |       keg_pin = (HOMEBREW_LIBRARY/"PinnedKegs"/d.basename.to_s) | 
					
						
							|  |  |  |       keg_pin.exist? or keg_pin.symlink? | 
					
						
							| 
									
										
										
										
											2013-03-11 16:41:08 +01:00
										 |  |  |     end.each do |d| | 
					
						
							| 
									
										
										
										
											2013-04-01 11:22:15 -05:00
										 |  |  |       puts d.basename | 
					
						
							| 
									
										
										
										
											2013-03-11 16:41:08 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class PrettyListing | 
					
						
							|  |  |  |   def initialize path | 
					
						
							|  |  |  |     Pathname.new(path).children.sort{ |a,b| a.to_s.downcase <=> b.to_s.downcase }.each do |pn| | 
					
						
							|  |  |  |       case pn.basename.to_s | 
					
						
							|  |  |  |       when 'bin', 'sbin' | 
					
						
							|  |  |  |         pn.find { |pnn| puts pnn unless pnn.directory? } | 
					
						
							|  |  |  |       when 'lib' | 
					
						
							|  |  |  |         print_dir pn do |pnn| | 
					
						
							|  |  |  |           # dylibs have multiple symlinks and we don't care about them | 
					
						
							|  |  |  |           (pnn.extname == '.dylib' or pnn.extname == '.pc') and not pnn.symlink? | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         if pn.directory? | 
					
						
							|  |  |  |           if pn.symlink? | 
					
						
							|  |  |  |             puts "#{pn} -> #{pn.readlink}" | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             print_dir pn | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2012-09-09 13:19:53 -07:00
										 |  |  |         elsif FORMULA_META_FILES.should_list? pn.basename.to_s | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |           puts pn | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def print_dir root | 
					
						
							|  |  |  |     dirs = [] | 
					
						
							|  |  |  |     remaining_root_files = [] | 
					
						
							|  |  |  |     other = '' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     root.children.sort.each do |pn| | 
					
						
							|  |  |  |       if pn.directory? | 
					
						
							|  |  |  |         dirs << pn | 
					
						
							|  |  |  |       elsif block_given? and yield pn | 
					
						
							|  |  |  |         puts pn | 
					
						
							|  |  |  |         other = 'other ' | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         remaining_root_files << pn unless pn.basename.to_s == '.DS_Store' | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dirs.each do |d| | 
					
						
							|  |  |  |       files = [] | 
					
						
							|  |  |  |       d.find { |pn| files << pn unless pn.directory? } | 
					
						
							|  |  |  |       print_remaining_files files, d | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     print_remaining_files remaining_root_files, root, other | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def print_remaining_files files, root, other = '' | 
					
						
							|  |  |  |     case files.length | 
					
						
							|  |  |  |     when 0
 | 
					
						
							|  |  |  |       # noop | 
					
						
							|  |  |  |     when 1
 | 
					
						
							|  |  |  |       puts files | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       puts "#{root}/ (#{files.length} #{other}files)" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |