Merge pull request #1908 from reitermarkus/cleanup-cask-doctor

Simplify `brew cask doctor`.
This commit is contained in:
Vítor Galvão 2017-02-07 15:18:06 +00:00 committed by GitHub
commit 917b9f445d
2 changed files with 50 additions and 123 deletions

View File

@ -2,116 +2,39 @@ module Hbc
class CLI class CLI
class Doctor < Base class Doctor < Base
def self.run def self.run
ohai "macOS Release:", render_with_none_as_error(MacOS.full_version) ohai "Homebrew-Cask Version", Hbc.full_version
ohai "Hardware Architecture:", render_with_none_as_error("#{Hardware::CPU.type}-#{Hardware::CPU.bits}") ohai "Homebrew-Cask Install Location", render_install_location
ohai "Ruby Version:", render_with_none_as_error("#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}") ohai "Homebrew-Cask Staging Location", render_staging_location(Hbc.caskroom)
ohai "Ruby Path:", render_with_none_as_error(RbConfig.ruby) ohai "Homebrew-Cask Cached Downloads", render_cached_downloads
# TODO: consider removing most Homebrew constants from doctor output ohai "Homebrew-Cask Taps:"
ohai "Homebrew Version:", render_with_none_as_error(homebrew_version) puts render_taps(Hbc.default_tap, *alt_taps)
ohai "Homebrew Executable Path:", render_with_none_as_error(HOMEBREW_BREW_FILE) ohai "Contents of $LOAD_PATH", render_load_path($LOAD_PATH)
ohai "Homebrew Cellar Path:", render_with_none_as_error(homebrew_cellar) ohai "Environment Variables"
ohai "Homebrew Repository Path:", render_with_none_as_error(HOMEBREW_REPOSITORY)
ohai "Homebrew Origin:", render_with_none_as_error(homebrew_origin)
ohai "Homebrew-Cask Version:", render_with_none_as_error(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
ohai "Homebrew-Cask Default Tap Path:", render_tap_paths(Hbc.default_tap.path)
ohai "Homebrew-Cask Alternate Cask Taps:", render_tap_paths(alt_taps)
ohai "Homebrew-Cask Default Tap Cask Count:", render_with_none_as_error(default_cask_count)
ohai "Contents of $LOAD_PATH:", render_load_path($LOAD_PATH)
ohai "Contents of $RUBYLIB Environment Variable:", render_env_var("RUBYLIB")
ohai "Contents of $RUBYOPT Environment Variable:", render_env_var("RUBYOPT")
ohai "Contents of $RUBYPATH Environment Variable:", render_env_var("RUBYPATH")
ohai "Contents of $RBENV_VERSION Environment Variable:", render_env_var("RBENV_VERSION")
ohai "Contents of $CHRUBY_VERSION Environment Variable:", render_env_var("CHRUBY_VERSION")
ohai "Contents of $GEM_HOME Environment Variable:", render_env_var("GEM_HOME")
ohai "Contents of $GEM_PATH Environment Variable:", render_env_var("GEM_PATH")
ohai "Contents of $BUNDLE_PATH Environment Variable:", render_env_var("BUNDLE_PATH")
ohai "Contents of $PATH Environment Variable:", render_env_var("PATH")
ohai "Contents of $SHELL Environment Variable:", render_env_var("SHELL")
ohai "Contents of Locale Environment Variables:", render_with_none(locale_variables)
end
def self.alt_taps environment_variables = [
Tap.select { |t| t.cask_dir && t != Hbc.default_tap } "RUBYLIB",
.map(&:path) "RUBYOPT",
end "RUBYPATH",
"RBENV_VERSION",
"CHRUBY_VERSION",
"GEM_HOME",
"GEM_PATH",
"BUNDLE_PATH",
"PATH",
"SHELL",
]
def self.default_cask_count (locale_variables + environment_variables).sort.each(&method(:render_env_var))
Hbc.default_tap.cask_files.count
rescue StandardError
"0 #{error_string "Error reading #{Hbc.default_tap.path}"}"
end
def self.homebrew_origin
homebrew_origin = notfound_string
begin
Dir.chdir(HOMEBREW_REPOSITORY) do
homebrew_origin = SystemCommand.run("/usr/bin/git",
args: %w[config --get remote.origin.url],
print_stderr: false).stdout.strip
end
if homebrew_origin !~ /\S/
homebrew_origin = "#{none_string} #{error_string}"
elsif homebrew_origin !~ %r{(mxcl|Homebrew)/(home)?brew(\.git)?\Z}
homebrew_origin.concat " #{error_string "warning: nonstandard origin"}"
end
rescue StandardError
homebrew_origin = error_string "Not Found - Error running git"
end
homebrew_origin
end
def self.homebrew_cellar
homebrew_constants("cellar")
end
def self.homebrew_version
homebrew_constants("version")
end
def self.homebrew_taps
Tap::TAP_DIRECTORY
end
def self.homebrew_constants(name)
@homebrew_constants ||= {}
return @homebrew_constants[name] if @homebrew_constants.key?(name)
@homebrew_constants[name] = notfound_string
begin
@homebrew_constants[name] = SystemCommand.run!(HOMEBREW_BREW_FILE,
args: ["--#{name}"],
print_stderr: false)
.stdout
.strip
if @homebrew_constants[name] !~ /\S/
@homebrew_constants[name] = "#{none_string} #{error_string}"
end
path = Pathname.new(@homebrew_constants[name])
@homebrew_constants[name] = path if path.exist?
rescue StandardError
@homebrew_constants[name] = error_string "Not Found - Error running brew"
end
@homebrew_constants[name]
end end
def self.locale_variables def self.locale_variables
ENV.keys.grep(/^(?:LC_\S+|LANG|LANGUAGE)\Z/).collect { |v| %Q(#{v}="#{ENV[v]}") }.sort.join("\n") ENV.keys.grep(/^(?:LC_\S+|LANG|LANGUAGE)\Z/).sort
end end
def self.none_string def self.none_string
"<NONE>" "<NONE>"
end end
def self.legacy_tap_pattern
/phinze/
end
def self.notfound_string
Formatter.error("Not Found - Unknown Error")
end
def self.error_string(string = "Error") def self.error_string(string = "Error")
Formatter.error("(#{string})") Formatter.error("(#{string})")
end end
@ -121,37 +44,42 @@ module Hbc
none_string none_string
end end
def self.render_with_none_as_error(string) def self.alt_taps
return string if !string.nil? && string.respond_to?(:to_s) && !string.to_s.empty? Tap.select { |t| t.cask_dir && t != Hbc.default_tap }
"#{none_string} #{error_string}"
end end
def self.render_tap_paths(paths) def self.cask_count_for_tap(tap)
paths = [paths] unless paths.respond_to?(:each) count = tap.cask_files.count
paths.collect do |dir| "#{count} #{count == 1 ? "cask" : "casks"}"
if dir.nil? || dir.to_s.empty? rescue StandardError
"0 #{error_string "error reading #{tap.path}"}"
end
def self.render_taps(*taps)
taps.collect do |tap|
if tap.path.nil? || tap.path.to_s.empty?
none_string none_string
elsif dir.to_s.match(legacy_tap_pattern)
dir.to_s.concat(" #{error_string "Warning: legacy tap path"}")
else else
dir.to_s "#{tap.path} (#{cask_count_for_tap(tap)})"
end end
end end
end end
def self.render_env_var(var) def self.render_env_var(var)
if ENV.key?(var) return unless ENV.key?(var)
%Q(#{var}="#{ENV[var]}") var = %Q(#{var}="#{ENV[var]}")
else puts user_tilde(var)
none_string
end end
def self.user_tilde(path)
path.gsub(ENV["HOME"], "~")
end end
# This could be done by calling into Homebrew, but the situation # This could be done by calling into Homebrew, but the situation
# where "doctor" is needed is precisely the situation where such # where "doctor" is needed is precisely the situation where such
# things are less dependable. # things are less dependable.
def self.render_install_location def self.render_install_location
locations = Dir.glob(Pathname.new(homebrew_cellar).join("brew-cask", "*")).reverse locations = Dir.glob(HOMEBREW_CELLAR.join("brew-cask", "*")).reverse
if locations.empty? if locations.empty?
none_string none_string
else else
@ -162,7 +90,7 @@ module Hbc
end end
def self.render_staging_location(path) def self.render_staging_location(path)
path = Pathname.new(path) path = Pathname.new(user_tilde(path.to_s))
if !path.exist? if !path.exist?
"#{path} #{error_string "error: path does not exist"}}" "#{path} #{error_string "error: path does not exist"}}"
elsif !path.writable? elsif !path.writable?
@ -173,19 +101,18 @@ module Hbc
end end
def self.render_load_path(paths) def self.render_load_path(paths)
paths.map(&method(:user_tilde))
return "#{none_string} #{error_string}" if [*paths].empty? return "#{none_string} #{error_string}" if [*paths].empty?
paths paths
end end
def self.render_cached_downloads def self.render_cached_downloads
cleanup = CLI::Cleanup.default cleanup = CLI::Cleanup.default
files = cleanup.cache_files count = cleanup.cache_files.count
count = files.count
size = cleanup.disk_cleanup_size size = cleanup.disk_cleanup_size
size_msg = "#{number_readable(count)} files, #{disk_usage_readable(size)}" msg = user_tilde(Hbc.cache.to_s)
warn_msg = error_string('warning: run "brew cask cleanup"') msg << " (#{number_readable(count)} files, #{disk_usage_readable(size)})" unless count.zero?
size_msg << " #{warn_msg}" if count > 0 msg
[Hbc.cache, size_msg]
end end
def self.help def self.help

View File

@ -4,7 +4,7 @@ describe Hbc::CLI::Doctor do
it "displays some nice info about the environment" do it "displays some nice info about the environment" do
expect { expect {
Hbc::CLI::Doctor.run Hbc::CLI::Doctor.run
}.to output(/\A==> macOS Release:/).to_stdout }.to output(/\A==> Homebrew-Cask Version/).to_stdout
end end
it "raises an exception when arguments are given" do it "raises an exception when arguments are given" do