| 
									
										
										
										
											2016-04-08 16:28:43 +02:00
										 |  |  | #:  * `linkapps` [`--local`] [<formulae>]: | 
					
						
							| 
									
										
										
										
											2016-09-18 19:57:19 +01:00
										 |  |  | #:    Find installed formulae that provide `.app`-style macOS apps and symlink them | 
					
						
							| 
									
										
										
										
											2017-01-09 12:31:00 +00:00
										 |  |  | #:    into `/Applications`, allowing for easier access (deprecated). | 
					
						
							|  |  |  | #: | 
					
						
							|  |  |  | #:    Unfortunately `brew linkapps` cannot behave nicely with e.g. Spotlight using | 
					
						
							|  |  |  | #:    either aliases or symlinks and Homebrew formulae do not build "proper" `.app` | 
					
						
							|  |  |  | #:    bundles that can be relocated. Instead, please consider using `brew cask` and | 
					
						
							|  |  |  | #:    migrate formulae using `.app`s to casks. | 
					
						
							| 
									
										
										
										
											2016-04-08 16:28:43 +02:00
										 |  |  | #: | 
					
						
							|  |  |  | #:    If no <formulae> are provided, all of them will have their apps symlinked. | 
					
						
							|  |  |  | #: | 
					
						
							|  |  |  | #:    If provided, `--local` will symlink them into the user's `~/Applications` | 
					
						
							|  |  |  | #:    directory instead of the system directory. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  | require "keg" | 
					
						
							| 
									
										
										
										
											2015-08-13 20:35:22 +08:00
										 |  |  | require "formula" | 
					
						
							| 
									
										
										
										
											2013-09-17 06:55:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 22:41:47 -05:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2016-09-26 01:44:51 +02:00
										 |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 06:55:13 -07:00
										 |  |  |   def linkapps | 
					
						
							| 
									
										
										
										
											2017-01-09 12:31:00 +00:00
										 |  |  |     opoo <<-EOS.undent
 | 
					
						
							|  |  |  |       `brew linkapps` has been deprecated and will eventually be removed! | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       Unfortunately `brew linkapps` cannot behave nicely with e.g. Spotlight using | 
					
						
							|  |  |  |       either aliases or symlinks and Homebrew formulae do not build "proper" `.app` | 
					
						
							|  |  |  |       bundles that can be relocated. Instead, please consider using `brew cask` and | 
					
						
							|  |  |  |       migrate formulae using `.app`s to casks. | 
					
						
							|  |  |  |     EOS | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-17 15:32:44 +01:00
										 |  |  |     target_dir = linkapps_target(local: ARGV.include?("--local")) | 
					
						
							| 
									
										
										
										
											2013-09-17 06:55:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-08 07:54:43 +01:00
										 |  |  |     unless target_dir.directory? | 
					
						
							| 
									
										
										
										
											2013-09-17 06:55:13 -07:00
										 |  |  |       opoo "#{target_dir} does not exist, stopping." | 
					
						
							|  |  |  |       puts "Run `mkdir #{target_dir}` first." | 
					
						
							|  |  |  |       exit 1
 | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-18 15:13:04 +08:00
										 |  |  |     if ARGV.named.empty? | 
					
						
							| 
									
										
										
										
											2015-08-13 20:35:22 +08:00
										 |  |  |       kegs = Formula.racks.map do |rack| | 
					
						
							| 
									
										
										
										
											2014-12-18 15:13:04 +08:00
										 |  |  |         keg = rack.subdirs.map { |d| Keg.new(d) } | 
					
						
							|  |  |  |         next if keg.empty? | 
					
						
							| 
									
										
										
										
											2015-12-08 07:54:43 +01:00
										 |  |  |         keg.detect(&:linked?) || keg.max_by(&:version) | 
					
						
							| 
									
										
										
										
											2014-12-18 15:13:04 +08:00
										 |  |  |       end | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       kegs = ARGV.kegs | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2013-09-17 06:55:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-08 07:54:43 +01:00
										 |  |  |     link_count = 0
 | 
					
						
							| 
									
										
										
										
											2014-12-18 15:13:04 +08:00
										 |  |  |     kegs.each do |keg| | 
					
						
							| 
									
										
										
										
											2015-10-20 07:05:21 +02:00
										 |  |  |       keg.apps.each do |app| | 
					
						
							| 
									
										
										
										
											2015-12-08 07:54:43 +01:00
										 |  |  |         puts "Linking: #{app}" | 
					
						
							|  |  |  |         target_app = target_dir/app.basename | 
					
						
							| 
									
										
										
										
											2013-09-17 06:55:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-08 07:54:43 +01:00
										 |  |  |         if target_app.exist? && !target_app.symlink? | 
					
						
							|  |  |  |           onoe "#{target_app} already exists, skipping." | 
					
						
							| 
									
										
										
										
											2013-09-17 06:55:13 -07:00
										 |  |  |           next | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2015-04-17 02:08:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-08 07:54:43 +01:00
										 |  |  |         # We prefer system `ln` over `FileUtils.ln_sf` because the latter seems | 
					
						
							|  |  |  |         # to have weird failure conditions (that were observed in the past). | 
					
						
							| 
									
										
										
										
											2013-09-17 06:55:13 -07:00
										 |  |  |         system "ln", "-sf", app, target_dir | 
					
						
							| 
									
										
										
										
											2015-12-08 07:54:43 +01:00
										 |  |  |         link_count += 1
 | 
					
						
							| 
									
										
										
										
											2013-09-17 06:55:13 -07:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-12-08 07:54:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if link_count.zero? | 
					
						
							|  |  |  |       puts "No apps linked to #{target_dir}" if ARGV.verbose? | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2017-03-11 11:33:12 +01:00
										 |  |  |       puts "Linked #{Formatter.pluralize(link_count, "app")} to #{target_dir}" | 
					
						
							| 
									
										
										
										
											2015-12-08 07:54:43 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def linkapps_target(opts = {}) | 
					
						
							|  |  |  |     local = opts.fetch(:local, false) | 
					
						
							|  |  |  |     Pathname.new(local ? "~/Applications" : "/Applications").expand_path | 
					
						
							| 
									
										
										
										
											2013-09-17 06:55:13 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | end |