Unify 'which' and which_s' utility methods

'which' only returns a Pathname or nil, and doesn't care about anything
sent to stderr, so just silence it by default and combine the two
methods.

Closes Homebrew/homebrew#12115.

Signed-off-by: Jack Nagel <jacknagel@gmail.com>
This commit is contained in:
Jack Nagel 2012-05-07 20:32:04 -05:00
parent fa1edd6846
commit 68124d481f
6 changed files with 19 additions and 24 deletions

View File

@ -633,7 +633,7 @@ def check_for_multiple_volumes
end end
def check_for_git def check_for_git
unless which_s "git" then <<-EOS.undent unless which "git" then <<-EOS.undent
Git could not be found in your PATH. Git could not be found in your PATH.
Homebrew uses Git for several internal functions, and some formulae use Git Homebrew uses Git for several internal functions, and some formulae use Git
checkouts instead of stable tarballs. You may want to install Git: checkouts instead of stable tarballs. You may want to install Git:
@ -643,7 +643,7 @@ def check_for_git
end end
def check_git_newline_settings def check_git_newline_settings
return unless which_s "git" return unless which "git"
autocrlf = `git config --get core.autocrlf`.chomp autocrlf = `git config --get core.autocrlf`.chomp
safecrlf = `git config --get core.safecrlf`.chomp safecrlf = `git config --get core.safecrlf`.chomp
@ -774,7 +774,7 @@ def check_missing_deps
end end
def check_git_status def check_git_status
return unless which_s "git" return unless which "git"
HOMEBREW_REPOSITORY.cd do HOMEBREW_REPOSITORY.cd do
unless `git status -s -- Library/Homebrew/ 2>/dev/null`.chomp.empty? then <<-EOS.undent unless `git status -s -- Library/Homebrew/ 2>/dev/null`.chomp.empty? then <<-EOS.undent
You have uncommitted modifications to Homebrew's core. You have uncommitted modifications to Homebrew's core.
@ -803,7 +803,7 @@ end
def check_git_version def check_git_version
# see https://github.com/blog/642-smart-http-support # see https://github.com/blog/642-smart-http-support
return unless which_s "git" return unless which "git"
`git --version`.chomp =~ /git version (\d)\.(\d)\.(\d)/ `git --version`.chomp =~ /git version (\d)\.(\d)\.(\d)/
if $2.to_i < 6 or $2.to_i == 6 and $3.to_i < 6 then <<-EOS.undent if $2.to_i < 6 or $2.to_i == 6 and $3.to_i < 6 then <<-EOS.undent
@ -815,7 +815,7 @@ def check_git_version
end end
def check_for_enthought_python def check_for_enthought_python
if which_s "enpkg" then <<-EOS.undent if which "enpkg" then <<-EOS.undent
Enthought Python was found in your PATH. Enthought Python was found in your PATH.
This can cause build problems, as this software installs its own This can cause build problems, as this software installs its own
copies of iconv and libxml2 into directories that are picked up by copies of iconv and libxml2 into directories that are picked up by
@ -825,7 +825,7 @@ def check_for_enthought_python
end end
def check_for_bad_python_symlink def check_for_bad_python_symlink
return unless which_s "python" return unless which "python"
# Indeed Python --version outputs to stderr (WTF?) # Indeed Python --version outputs to stderr (WTF?)
`python --version 2>&1` =~ /Python (\d+)\./ `python --version 2>&1` =~ /Python (\d+)\./
unless $1 == "2" then <<-EOS.undent unless $1 == "2" then <<-EOS.undent

View File

@ -4,7 +4,7 @@ require 'cmd/untap'
module Homebrew extend self module Homebrew extend self
def update def update
abort "Please `brew install git' first." unless which_s "git" abort "Please `brew install git' first." unless which "git"
# ensure GIT_CONFIG is unset as we need to operate on .git/config # ensure GIT_CONFIG is unset as we need to operate on .git/config
ENV.delete('GIT_CONFIG') ENV.delete('GIT_CONFIG')

View File

@ -2,7 +2,7 @@ require 'formula'
module Homebrew extend self module Homebrew extend self
def versions def versions
raise "Please `brew install git` first" unless which_s "git" raise "Please `brew install git` first" unless which "git"
raise "Please `brew update' first" unless (HOMEBREW_REPOSITORY/".git").directory? raise "Please `brew update' first" unless (HOMEBREW_REPOSITORY/".git").directory?
raise FormulaUnspecifiedError if ARGV.named.empty? raise FormulaUnspecifiedError if ARGV.named.empty?

View File

@ -80,7 +80,7 @@ class CurlDownloadStrategy < AbstractDownloadStrategy
safe_system '/usr/bin/tar', 'xf', @tarball_path safe_system '/usr/bin/tar', 'xf', @tarball_path
chdir chdir
when :xz when :xz
raise "You must install XZutils: brew install xz" unless which_s "xz" raise "You must install XZutils: brew install xz" unless which "xz"
safe_system "xz -dc \"#{@tarball_path}\" | /usr/bin/tar xf -" safe_system "xz -dc \"#{@tarball_path}\" | /usr/bin/tar xf -"
chdir chdir
when :pkg when :pkg
@ -322,7 +322,7 @@ class GitDownloadStrategy < AbstractDownloadStrategy
end end
def fetch def fetch
raise "You must install Git: brew install git" unless which_s "git" raise "You must install Git: brew install git" unless which "git"
ohai "Cloning #{@url}" ohai "Cloning #{@url}"
@ -455,7 +455,7 @@ class MercurialDownloadStrategy < AbstractDownloadStrategy
def cached_location; @clone; end def cached_location; @clone; end
def fetch def fetch
raise "You must install Mercurial: brew install mercurial" unless which_s "hg" raise "You must install Mercurial: brew install mercurial" unless which "hg"
ohai "Cloning #{@url}" ohai "Cloning #{@url}"
@ -496,7 +496,7 @@ class BazaarDownloadStrategy < AbstractDownloadStrategy
def cached_location; @clone; end def cached_location; @clone; end
def fetch def fetch
raise "You must install bazaar first" unless which_s "bzr" raise "You must install bazaar first" unless which "bzr"
ohai "Cloning #{@url}" ohai "Cloning #{@url}"
unless @clone.exist? unless @clone.exist?
@ -539,7 +539,7 @@ class FossilDownloadStrategy < AbstractDownloadStrategy
def cached_location; @clone; end def cached_location; @clone; end
def fetch def fetch
raise "You must install fossil first" unless which_s "fossil" raise "You must install fossil first" unless which "fossil"
ohai "Cloning #{@url}" ohai "Cloning #{@url}"
unless @clone.exist? unless @clone.exist?

View File

@ -147,9 +147,8 @@ def puts_columns items, star_items=[]
end end
end end
def which cmd, silent=false def which cmd
cmd += " 2>/dev/null" if silent path = `/usr/bin/which #{cmd} 2>/dev/null`.chomp
path = `/usr/bin/which #{cmd}`.chomp
if path.empty? if path.empty?
nil nil
else else
@ -157,19 +156,15 @@ def which cmd, silent=false
end end
end end
def which_s cmd
which cmd, true
end
def which_editor def which_editor
editor = ENV['HOMEBREW_EDITOR'] || ENV['EDITOR'] editor = ENV['HOMEBREW_EDITOR'] || ENV['EDITOR']
# If an editor wasn't set, try to pick a sane default # If an editor wasn't set, try to pick a sane default
return editor unless editor.nil? return editor unless editor.nil?
# Find Textmate # Find Textmate
return 'mate' if which_s "mate" return 'mate' if which "mate"
# Find # BBEdit / TextWrangler # Find # BBEdit / TextWrangler
return 'edit' if which_s "edit" return 'edit' if which "edit"
# Default to vim # Default to vim
return '/usr/bin/vim' return '/usr/bin/vim'
end end
@ -405,7 +400,7 @@ module MacOS extend self
# Xcode 4.3 xc* tools hang indefinately if xcode-select path is set thus # Xcode 4.3 xc* tools hang indefinately if xcode-select path is set thus
raise if `xcode-select -print-path 2>/dev/null`.chomp == "/" raise if `xcode-select -print-path 2>/dev/null`.chomp == "/"
raise unless which_s "xcodebuild" raise unless which "xcodebuild"
`xcodebuild -version 2>/dev/null` =~ /Xcode (\d(\.\d)*)/ `xcodebuild -version 2>/dev/null` =~ /Xcode (\d(\.\d)*)/
raise if $1.nil? or not $?.success? raise if $1.nil? or not $?.success?
$1 $1

View File

@ -72,7 +72,7 @@ begin
# Add example external commands to PATH before checking. # Add example external commands to PATH before checking.
ENV['PATH'] += ":#{HOMEBREW_REPOSITORY}/Library/Contributions/cmds" ENV['PATH'] += ":#{HOMEBREW_REPOSITORY}/Library/Contributions/cmds"
if which_s "brew-#{cmd}" if which "brew-#{cmd}"
%w[CACHE CELLAR LIBRARY_PATH PREFIX REPOSITORY].each do |e| %w[CACHE CELLAR LIBRARY_PATH PREFIX REPOSITORY].each do |e|
ENV["HOMEBREW_#{e}"] = Object.const_get "HOMEBREW_#{e}" ENV["HOMEBREW_#{e}"] = Object.const_get "HOMEBREW_#{e}"
end end