提交 35303130 编写于 作者: M Mislav Marohnić

true cross-platform `command?` and `browser_launcher` methods

上级 7d3ff72b
......@@ -54,7 +54,7 @@ module Hub
# Array of `executable` followed by all args suitable as arguments
# for `exec` or `system` calls.
def to_exec(args = self)
[executable].concat args
[*executable].concat args
end
# All the words (as opposed to flags) contained in this argument
......
......@@ -556,35 +556,6 @@ help
# from the command line.
#
# Checks whether a command exists on this system in the $PATH.
#
# name - The String name of the command to check for.
#
# Returns a Boolean.
def command?(name)
`which #{name} 2>/dev/null`
$?.success?
end
# Detects commands to launch the user's browser, checking $BROWSER
# first then falling back to a few common launchers. Aborts with
# an error if it can't find anything appropriate.
#
# Returns a launch command.
def browser_launcher
if ENV['BROWSER']
ENV['BROWSER']
elsif RUBY_PLATFORM.include?('darwin')
"open"
elsif command?("xdg-open")
"xdg-open"
elsif command?("cygstart")
"cygstart"
else
abort "Please set $BROWSER to a web launcher to use this command."
end
end
# Handles common functionality of browser commands like `browse`
# and `compare`. Yields a block that returns params for `github_url`.
def browse_command(args)
......@@ -596,7 +567,6 @@ help
args.push github_url({:web => true, :private => true}.update(params))
end
# Returns the terminal-formatted manpage, ready to be printed to
# the screen.
def hub_manpage
......
......@@ -155,5 +155,42 @@ module Hub
def current_dirname
DIRNAME
end
# Cross-platform web browser command; respects the value set in $BROWSER.
#
# Returns an array, e.g.: ['open']
def browser_launcher
require 'rbconfig'
browser = ENV['BROWSER'] ||
(RbConfig::CONFIG['host_os'].include?('darwin') && 'open') ||
(RbConfig::CONFIG['host_os'] =~ /msdos|mswin|djgpp|mingw|windows/ && 'start') ||
%w[xdg-open cygstart x-www-browser firefox opera mozilla netscape].find { |comm| which comm }
abort "Please set $BROWSER to a web launcher to use this command." unless browser
Array(browser)
end
# Cross-platform way of finding an executable in the $PATH.
#
# which('ruby') #=> /usr/bin/ruby
def which(cmd)
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each { |ext|
exe = "#{path}/#{cmd}#{ext}"
return exe if File.executable? exe
}
end
return nil
end
# Checks whether a command exists on this system in the $PATH.
#
# name - The String name of the command to check for.
#
# Returns a Boolean.
def command?(name)
!which(name).nil?
end
end
end
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'helper'
require 'webmock/test_unit'
require 'rbconfig'
WebMock::BodyPattern.class_eval do
undef normalize_hash
......@@ -17,10 +18,10 @@ class HubTest < Test::Unit::TestCase
COMMANDS = []
Hub::Commands.class_eval do
remove_method :command?
define_method :command? do |name|
COMMANDS.include?(name)
Hub::Context.class_eval do
remove_method :which
define_method :which do |name|
COMMANDS.include?(name) ? "/usr/bin/#{name}" : nil
end
end
......@@ -720,7 +721,7 @@ config
def test_linux_browser
stub_available_commands "open", "xdg-open", "cygstart"
with_browser_env(nil) do
with_ruby_platform("i686-linux") do
with_host_os("i686-linux") do
assert_browser("xdg-open")
end
end
......@@ -729,7 +730,7 @@ config
def test_cygwin_browser
stub_available_commands "open", "cygstart"
with_browser_env(nil) do
with_ruby_platform("i686-linux") do
with_host_os("i686-linux") do
assert_browser("cygstart")
end
end
......@@ -739,7 +740,7 @@ config
stub_available_commands()
expected = "Please set $BROWSER to a web launcher to use this command.\n"
with_browser_env(nil) do
with_ruby_platform("i686-linux") do
with_host_os("i686-linux") do
assert_equal expected, hub("browse")
end
end
......@@ -834,14 +835,14 @@ config
assert_command "browse", "#{browser} https://github.com/defunkt/hub"
end
def with_ruby_platform(value)
platform = RUBY_PLATFORM
Object.send(:remove_const, :RUBY_PLATFORM)
Object.const_set(:RUBY_PLATFORM, value)
yield
ensure
Object.send(:remove_const, :RUBY_PLATFORM)
Object.const_set(:RUBY_PLATFORM, platform)
def with_host_os(value)
host_os = RbConfig::CONFIG['host_os']
RbConfig::CONFIG['host_os'] = value
begin
yield
ensure
RbConfig::CONFIG['host_os'] = host_os
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册