| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  | module Hbc | 
					
						
							|  |  |  |   class CLI | 
					
						
							|  |  |  |     class Doctor < Base | 
					
						
							|  |  |  |       def self.run | 
					
						
							| 
									
										
										
										
											2017-02-05 06:48:08 +01:00
										 |  |  |         ohai "Homebrew-Cask Version", Hbc.full_version | 
					
						
							|  |  |  |         ohai "Homebrew-Cask Install Location", render_install_location | 
					
						
							|  |  |  |         ohai "Homebrew-Cask Staging Location", render_staging_location(Hbc.caskroom) | 
					
						
							|  |  |  |         ohai "Homebrew-Cask Cached Downloads", render_cached_downloads | 
					
						
							| 
									
										
										
										
											2017-01-25 00:36:38 +01:00
										 |  |  |         ohai "Homebrew-Cask Taps:" | 
					
						
							| 
									
										
										
										
											2017-02-04 21:53:35 +01:00
										 |  |  |         puts render_taps(Hbc.default_tap, *alt_taps) | 
					
						
							| 
									
										
										
										
											2017-02-05 06:48:08 +01:00
										 |  |  |         ohai "Contents of $LOAD_PATH", render_load_path($LOAD_PATH) | 
					
						
							|  |  |  |         ohai "Environment Variables" | 
					
						
							| 
									
										
										
										
											2017-02-04 21:53:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         environment_variables = [ | 
					
						
							|  |  |  |           "RUBYLIB", | 
					
						
							|  |  |  |           "RUBYOPT", | 
					
						
							|  |  |  |           "RUBYPATH", | 
					
						
							|  |  |  |           "RBENV_VERSION", | 
					
						
							|  |  |  |           "CHRUBY_VERSION", | 
					
						
							|  |  |  |           "GEM_HOME", | 
					
						
							|  |  |  |           "GEM_PATH", | 
					
						
							|  |  |  |           "BUNDLE_PATH", | 
					
						
							|  |  |  |           "PATH", | 
					
						
							|  |  |  |           "SHELL", | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         (locale_variables + environment_variables).sort.each(&method(:render_env_var)) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def self.locale_variables | 
					
						
							| 
									
										
										
										
											2017-02-04 21:53:35 +01:00
										 |  |  |         ENV.keys.grep(/^(?:LC_\S+|LANG|LANGUAGE)\Z/).sort | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def self.none_string | 
					
						
							|  |  |  |         "<NONE>" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def self.error_string(string = "Error") | 
					
						
							| 
									
										
										
										
											2016-08-30 21:38:13 +02:00
										 |  |  |         Formatter.error("(#{string})") | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def self.render_with_none(string) | 
					
						
							|  |  |  |         return string if !string.nil? && string.respond_to?(:to_s) && !string.to_s.empty? | 
					
						
							|  |  |  |         none_string | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-25 00:36:38 +01:00
										 |  |  |       def self.alt_taps | 
					
						
							|  |  |  |         Tap.select { |t| t.cask_dir && t != Hbc.default_tap } | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def self.cask_count_for_tap(tap) | 
					
						
							|  |  |  |         count = tap.cask_files.count | 
					
						
							|  |  |  |         "#{count} #{count == 1 ? "cask" : "casks"}" | 
					
						
							|  |  |  |       rescue StandardError | 
					
						
							|  |  |  |         "0 #{error_string "error reading #{tap.path}"}" | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-25 00:36:38 +01:00
										 |  |  |       def self.render_taps(*taps) | 
					
						
							|  |  |  |         taps.collect do |tap| | 
					
						
							|  |  |  |           if tap.path.nil? || tap.path.to_s.empty? | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |             none_string | 
					
						
							|  |  |  |           else | 
					
						
							| 
									
										
										
										
											2017-01-25 00:36:38 +01:00
										 |  |  |             "#{tap.path} (#{cask_count_for_tap(tap)})" | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def self.render_env_var(var) | 
					
						
							| 
									
										
										
										
											2017-02-04 21:53:35 +01:00
										 |  |  |         return unless ENV.key?(var) | 
					
						
							|  |  |  |         var = %Q(#{var}="#{ENV[var]}") | 
					
						
							|  |  |  |         puts user_tilde(var) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-04 21:52:04 +01:00
										 |  |  |       def self.user_tilde(path) | 
					
						
							|  |  |  |         path.gsub(ENV["HOME"], "~") | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       # This could be done by calling into Homebrew, but the situation | 
					
						
							|  |  |  |       # where "doctor" is needed is precisely the situation where such | 
					
						
							|  |  |  |       # things are less dependable. | 
					
						
							|  |  |  |       def self.render_install_location | 
					
						
							| 
									
										
										
										
											2017-01-25 00:36:38 +01:00
										 |  |  |         locations = Dir.glob(HOMEBREW_CELLAR.join("brew-cask", "*")).reverse | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |         if locations.empty? | 
					
						
							|  |  |  |           none_string | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           locations.collect do |l| | 
					
						
							|  |  |  |             "#{l} #{error_string 'error: legacy install. Run "brew uninstall --force brew-cask".'}" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def self.render_staging_location(path) | 
					
						
							| 
									
										
										
										
											2017-02-04 21:52:04 +01:00
										 |  |  |         path = Pathname.new(user_tilde(path.to_s)) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |         if !path.exist? | 
					
						
							|  |  |  |           "#{path} #{error_string "error: path does not exist"}}" | 
					
						
							|  |  |  |         elsif !path.writable? | 
					
						
							|  |  |  |           "#{path} #{error_string "error: not writable by current user"}" | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           path | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def self.render_load_path(paths) | 
					
						
							| 
									
										
										
										
											2017-02-04 21:52:04 +01:00
										 |  |  |         paths.map(&method(:user_tilde)) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |         return "#{none_string} #{error_string}" if [*paths].empty? | 
					
						
							|  |  |  |         paths | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def self.render_cached_downloads | 
					
						
							|  |  |  |         cleanup = CLI::Cleanup.default | 
					
						
							| 
									
										
										
										
											2017-02-04 21:53:02 +01:00
										 |  |  |         count = cleanup.cache_files.count | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |         size = cleanup.disk_cleanup_size | 
					
						
							| 
									
										
										
										
											2017-02-04 21:53:02 +01:00
										 |  |  |         msg = user_tilde(Hbc.cache.to_s) | 
					
						
							|  |  |  |         msg << " (#{number_readable(count)} files, #{disk_usage_readable(size)})" unless count.zero? | 
					
						
							|  |  |  |         msg | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def self.help | 
					
						
							|  |  |  |         "checks for configuration issues" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | end |