Merge pull request #4719 from rolandwalker/doctor_refactor

refactor `brew cask doctor`
This commit is contained in:
Roland Walker
2014-06-06 20:42:45 -04:00
+108 -37
View File
@@ -1,32 +1,64 @@
class Cask::CLI::Doctor
def self.run
notfound_msg = "#{Tty.red}Not Found - Unknown Error#{Tty.reset}"
fq_default_tap = notfound_msg
alt_taps = notfound_msg
default_cask_count = notfound_msg
privileged_uid = notfound_msg
homebrew_origin = notfound_msg
ohai 'OS X Version:', 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( RUBY_PATH )
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( HOMEBREW_CASK_VERSION )
ohai 'Homebrew-cask Default Tap Path:', render_tap_paths( fq_default_tap )
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_with_none_as_error( $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 'Running As Privileged User:', render_with_none_as_error( privileged_uid )
end
def self.fq_default_tap
return @fq_default_tap if @fq_default_tap
@fq_default_tap = notfound_string
begin
privileged_uid = Process.euid == 0 ? "Yes #{Tty.red}(warning: not recommended)#{Tty.reset}" : 'No'
rescue StandardError; end
begin
fq_default_tap = HOMEBREW_REPOSITORY.join 'Library', 'Taps', Cask.default_tap
@fq_default_tap = HOMEBREW_REPOSITORY.join 'Library', 'Taps', Cask.default_tap
rescue StandardError; end
@fq_default_tap
end
def self.alt_taps
alt_taps = notfound_string
begin
alt_taps = Pathname.glob(HOMEBREW_REPOSITORY.join 'Library', 'Taps', '*', '*', 'Casks').map(&:dirname) -
[fq_default_tap]
alt_taps = nil unless alt_taps.length > 0
rescue StandardError; end
alt_taps
end
def self.default_cask_count
default_cask_count = notfound_string
begin
default_cask_count = HOMEBREW_REPOSITORY.join(fq_default_tap, 'Casks').children.count(&:file?)
rescue StandardError
default_cask_count = "0 #{Tty.red}(Error reading #{fq_default_tap})#{Tty.reset}"
default_cask_count = "0 #{error_string %Q{Error reading #{fq_default_tap}}}"
end
default_cask_count
end
def self.homebrew_origin
homebrew_origin = notfound_string
begin
HOMEBREW_REPOSITORY.cd do
homebrew_origin = Cask::SystemCommand.run('git',
@@ -34,36 +66,75 @@ class Cask::CLI::Doctor
:stderr => :silence).strip
end
if homebrew_origin !~ %r{\S}
homebrew_origin = "<NONE> #{Tty.red}(Error)#{Tty.reset}"
homebrew_origin = "#{none_string} #{error_string}"
elsif homebrew_origin !~ %r{(mxcl|Homebrew)/homebrew(\.git)?\Z}
homebrew_origin.concat " #{Tty.red}(warning: nonstandard origin)#{Tty.reset}"
homebrew_origin.concat " #{error_string 'warning: nonstandard origin'}"
end
rescue StandardError
homebrew_origin = "#{Tty.red}Not Found - Error running git#{Tty.reset}"
homebrew_origin = error_string 'Not Found - Error running git'
end
homebrew_origin
end
ohai 'OS X Version:', MACOS_FULL_VERSION
ohai "Hardware Architecture:", "#{Hardware::CPU.type}-#{Hardware::CPU.bits}"
ohai 'Ruby Version:', "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
ohai 'Ruby Path:', RUBY_PATH
ohai 'Homebrew Version:', HOMEBREW_VERSION
ohai 'Homebrew Executable Path:', HOMEBREW_BREW_FILE
ohai 'Homebrew Cellar Path:', HOMEBREW_CELLAR
ohai 'Homebrew Repository Path:', HOMEBREW_REPOSITORY
ohai 'Homebrew Origin:', homebrew_origin
ohai 'Homebrew-cask Version:', HOMEBREW_CASK_VERSION
ohai 'Homebrew-cask Default Tap Path:', fq_default_tap
ohai 'Homebrew-cask Alternate Cask Taps:', alt_taps
ohai 'Homebrew-cask Default Tap Cask Count:', default_cask_count
ohai 'Contents of $LOAD_PATH:', $LOAD_PATH
ohai 'Contents of $RUBYLIB Environment Variable:', ENV['RUBYLIB']
ohai 'Contents of $RUBYOPT Environment Variable:', ENV['RUBYOPT']
ohai 'Contents of $RUBYPATH Environment Variable:', ENV['RUBYPATH']
ohai 'Contents of $RBENV_VERSION Environment Variable:', ENV['RBENV_VERSION']
ohai 'Contents of $GEM_HOME Environment Variable:', ENV['GEM_HOME']
ohai 'Contents of $GEM_PATH Environment Variable:', ENV['GEM_PATH']
ohai 'Contents of $BUNDLE_PATH Environment Variable:', ENV['BUNDLE_PATH']
ohai 'Running As Privileged User:', privileged_uid
def self.locale_variables
ENV.keys.grep(/^(?:LC_\S+|LANG|LANGUAGE)\Z/).collect_concat { |v| %Q{#{v}="#{ENV[v]}"} }.sort
end
def self.privileged_uid
privileged_uid = notfound_string
begin
privileged_uid = Process.euid == 0 ? "Yes #{error_string 'warning: not recommended'}" : 'No'
rescue StandardError; end
privileged_uid
end
def self.none_string
'<NONE>'
end
def self.legacy_tap_pattern
%r{phinze}
end
def self.notfound_string
"#{Tty.red}Not Found - Unknown Error#{Tty.reset}"
end
def self.error_string(string='Error')
"#{Tty.red}(#{string})#{Tty.reset}"
end
def self.render_with_none(string)
(string.nil? or not string.respond_to?(:to_s) or string.to_s.length == 0) ?
none_string :
string
end
def self.render_with_none_as_error(string)
(string.nil? or not string.respond_to?(:to_s) or string.to_s.length == 0) ?
"#{none_string} #{error_string}" :
string
end
def self.render_tap_paths(paths)
paths = [ paths ] unless paths.respond_to?(:each)
paths.collect do |dir|
if (dir.nil? or dir.to_s.length == 0) then
none_string
elsif dir.to_s.match(legacy_tap_pattern)
dir.to_s.concat(" #{error_string 'Warning: legacy tap path'}")
else
dir.to_s
end
end
end
def self.render_env_var(var)
if ENV.key?(var)
%Q{#{var}="#{ENV[var]}"}
else
none_string
end
end
def self.help