From 324988e7a5d7d4735cf5f96ea17ae1491d0f801e Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Wed, 25 Jan 2017 00:36:38 +0100 Subject: [PATCH 1/7] Simplify `brew cask doctor`. --- Library/Homebrew/cask/lib/hbc/cli/doctor.rb | 112 ++++---------------- 1 file changed, 20 insertions(+), 92 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb index 84d8e1ebcf..3618f0734c 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb @@ -2,23 +2,13 @@ module Hbc class CLI class Doctor < Base def self.run - ohai "macOS Release:", render_with_none_as_error(MacOS.full_version) - ohai "Hardware Architecture:", render_with_none_as_error("#{Hardware::CPU.type}-#{Hardware::CPU.bits}") - ohai "Ruby Version:", render_with_none_as_error("#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}") - ohai "Ruby Path:", render_with_none_as_error(RbConfig.ruby) - # TODO: consider removing most Homebrew constants from doctor output - ohai "Homebrew Version:", render_with_none_as_error(homebrew_version) - ohai "Homebrew Executable Path:", render_with_none_as_error(HOMEBREW_BREW_FILE) - ohai "Homebrew Cellar Path:", render_with_none_as_error(homebrew_cellar) - 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 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 - 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 "Homebrew-Cask Taps:" + puts render_taps(Hbc.default_tap) + puts render_taps(*alt_taps) 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") @@ -33,69 +23,6 @@ module Hbc ohai "Contents of Locale Environment Variables:", render_with_none(locale_variables) end - def self.alt_taps - Tap.select { |t| t.cask_dir && t != Hbc.default_tap } - .map(&:path) - end - - def self.default_cask_count - 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 - def self.locale_variables ENV.keys.grep(/^(?:LC_\S+|LANG|LANGUAGE)\Z/).collect { |v| %Q(#{v}="#{ENV[v]}") }.sort.join("\n") end @@ -108,10 +35,6 @@ module Hbc /phinze/ end - def self.notfound_string - Formatter.error("Not Found - Unknown Error") - end - def self.error_string(string = "Error") Formatter.error("(#{string})") end @@ -121,20 +44,25 @@ module Hbc none_string end - def self.render_with_none_as_error(string) - return string if !string.nil? && string.respond_to?(:to_s) && !string.to_s.empty? - "#{none_string} #{error_string}" + def self.alt_taps + Tap.select { |t| t.cask_dir && t != Hbc.default_tap } end - def self.render_tap_paths(paths) - paths = [paths] unless paths.respond_to?(:each) - paths.collect do |dir| - if dir.nil? || dir.to_s.empty? + 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}"}" + end + + def self.render_taps(*taps) + taps.collect do |tap| + if tap.path.nil? || tap.path.to_s.empty? none_string - elsif dir.to_s.match(legacy_tap_pattern) - dir.to_s.concat(" #{error_string "Warning: legacy tap path"}") + elsif tap.path.to_s.match(legacy_tap_pattern) + tap.path.to_s.concat(" #{error_string "Warning: legacy tap path"}") else - dir.to_s + "#{tap.path} (#{cask_count_for_tap(tap)})" end end end @@ -151,7 +79,7 @@ module Hbc # where "doctor" is needed is precisely the situation where such # things are less dependable. 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? none_string else From 7d312e4ccadd1852b0ae956e19248eea7ec13696 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Thu, 26 Jan 2017 08:25:17 +0100 Subject: [PATCH 2/7] Remove legacy tap pattern. --- Library/Homebrew/cask/lib/hbc/cli/doctor.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb index 3618f0734c..cba3ba0e69 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb @@ -31,10 +31,6 @@ module Hbc "" end - def self.legacy_tap_pattern - /phinze/ - end - def self.error_string(string = "Error") Formatter.error("(#{string})") end @@ -59,8 +55,6 @@ module Hbc taps.collect do |tap| if tap.path.nil? || tap.path.to_s.empty? none_string - elsif tap.path.to_s.match(legacy_tap_pattern) - tap.path.to_s.concat(" #{error_string "Warning: legacy tap path"}") else "#{tap.path} (#{cask_count_for_tap(tap)})" end From f0337a318320bebe56de1b07b406cd8fbca9c8a5 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Thu, 26 Jan 2017 08:54:52 +0100 Subject: [PATCH 3/7] Simplify output of environment variables. --- Library/Homebrew/cask/lib/hbc/cli/doctor.rb | 30 ++++++++++----------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb index cba3ba0e69..0fa08be8a7 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb @@ -10,21 +10,22 @@ module Hbc puts render_taps(Hbc.default_tap) puts render_taps(*alt_taps) 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) + ohai "Environment Variables:" + render_env_var("RUBYLIB") + render_env_var("RUBYOPT") + render_env_var("RUBYPATH") + render_env_var("RBENV_VERSION") + render_env_var("CHRUBY_VERSION") + render_env_var("GEM_HOME") + render_env_var("GEM_PATH") + render_env_var("BUNDLE_PATH") + render_env_var("PATH") + render_env_var("SHELL") + locale_variables end 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.each(&method(:render_env_var)) end def self.none_string @@ -63,9 +64,8 @@ module Hbc def self.render_env_var(var) if ENV.key?(var) - %Q(#{var}="#{ENV[var]}") - else - none_string + var = %Q(#{var}="#{ENV[var]}") + puts var.gsub(ENV["HOME"], "~") end end From b54c3d618e8c2704eb9930776c883f0052ecc9db Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 4 Feb 2017 21:52:04 +0100 Subject: [PATCH 4/7] Add `user_tilde` method to shorten/anonymize output. --- Library/Homebrew/cask/lib/hbc/cli/doctor.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb index 0fa08be8a7..7b777abbbb 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb @@ -65,10 +65,14 @@ module Hbc def self.render_env_var(var) if ENV.key?(var) var = %Q(#{var}="#{ENV[var]}") - puts var.gsub(ENV["HOME"], "~") + puts user_tilde(var) end end + def self.user_tilde(path) + path.gsub(ENV["HOME"], "~") + end + # 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. @@ -84,7 +88,7 @@ module Hbc end def self.render_staging_location(path) - path = Pathname.new(path) + path = Pathname.new(user_tilde(path.to_s)) if !path.exist? "#{path} #{error_string "error: path does not exist"}}" elsif !path.writable? @@ -95,6 +99,7 @@ module Hbc end def self.render_load_path(paths) + paths.map(&method(:user_tilde)) return "#{none_string} #{error_string}" if [*paths].empty? paths end From 46e051b1c2ed44ce4db151da9a09a1870c485334 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 4 Feb 2017 21:53:02 +0100 Subject: [PATCH 5/7] Simplify cache output. --- Library/Homebrew/cask/lib/hbc/cli/doctor.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb index 7b777abbbb..acf584bfd2 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb @@ -106,13 +106,11 @@ module Hbc def self.render_cached_downloads cleanup = CLI::Cleanup.default - files = cleanup.cache_files - count = files.count + count = cleanup.cache_files.count size = cleanup.disk_cleanup_size - size_msg = "#{number_readable(count)} files, #{disk_usage_readable(size)}" - warn_msg = error_string('warning: run "brew cask cleanup"') - size_msg << " #{warn_msg}" if count > 0 - [Hbc.cache, size_msg] + msg = user_tilde(Hbc.cache.to_s) + msg << " (#{number_readable(count)} files, #{disk_usage_readable(size)})" unless count.zero? + msg end def self.help From d8eab8c2114f8f89decb4293283ec747f82f9d08 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 4 Feb 2017 21:53:35 +0100 Subject: [PATCH 6/7] Simplify rendering of environment variables. --- Library/Homebrew/cask/lib/hbc/cli/doctor.rb | 38 +++++++++++---------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb index acf584bfd2..598c9eee18 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb @@ -7,25 +7,28 @@ module Hbc ohai "Homebrew-Cask Staging Location:", render_staging_location(Hbc.caskroom) ohai "Homebrew-Cask Cached Downloads:", render_cached_downloads ohai "Homebrew-Cask Taps:" - puts render_taps(Hbc.default_tap) - puts render_taps(*alt_taps) + puts render_taps(Hbc.default_tap, *alt_taps) ohai "Contents of $LOAD_PATH:", render_load_path($LOAD_PATH) ohai "Environment Variables:" - render_env_var("RUBYLIB") - render_env_var("RUBYOPT") - render_env_var("RUBYPATH") - render_env_var("RBENV_VERSION") - render_env_var("CHRUBY_VERSION") - render_env_var("GEM_HOME") - render_env_var("GEM_PATH") - render_env_var("BUNDLE_PATH") - render_env_var("PATH") - render_env_var("SHELL") - locale_variables + + 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)) end def self.locale_variables - ENV.keys.grep(/^(?:LC_\S+|LANG|LANGUAGE)\Z/).sort.each(&method(:render_env_var)) + ENV.keys.grep(/^(?:LC_\S+|LANG|LANGUAGE)\Z/).sort end def self.none_string @@ -63,10 +66,9 @@ module Hbc end def self.render_env_var(var) - if ENV.key?(var) - var = %Q(#{var}="#{ENV[var]}") - puts user_tilde(var) - end + return unless ENV.key?(var) + var = %Q(#{var}="#{ENV[var]}") + puts user_tilde(var) end def self.user_tilde(path) From d02a4d9e51e69280532533b2cfa7e1e5513edc4c Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sun, 5 Feb 2017 06:48:08 +0100 Subject: [PATCH 7/7] Fix test for `brew cask doctor`. --- Library/Homebrew/cask/lib/hbc/cli/doctor.rb | 12 ++++++------ Library/Homebrew/cask/spec/cask/cli/doctor_spec.rb | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb index 598c9eee18..6b2f4caab5 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb @@ -2,14 +2,14 @@ module Hbc class CLI class Doctor < Base def self.run - 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 + 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 ohai "Homebrew-Cask Taps:" puts render_taps(Hbc.default_tap, *alt_taps) - ohai "Contents of $LOAD_PATH:", render_load_path($LOAD_PATH) - ohai "Environment Variables:" + ohai "Contents of $LOAD_PATH", render_load_path($LOAD_PATH) + ohai "Environment Variables" environment_variables = [ "RUBYLIB", diff --git a/Library/Homebrew/cask/spec/cask/cli/doctor_spec.rb b/Library/Homebrew/cask/spec/cask/cli/doctor_spec.rb index 38f8b50ac2..ff1cf5706e 100644 --- a/Library/Homebrew/cask/spec/cask/cli/doctor_spec.rb +++ b/Library/Homebrew/cask/spec/cask/cli/doctor_spec.rb @@ -4,7 +4,7 @@ describe Hbc::CLI::Doctor do it "displays some nice info about the environment" do expect { Hbc::CLI::Doctor.run - }.to output(/\A==> macOS Release:/).to_stdout + }.to output(/\A==> Homebrew-Cask Version/).to_stdout end it "raises an exception when arguments are given" do