Skip to content

Commit

Permalink
true cross-platform command? and browser_launcher methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Mar 29, 2011
1 parent 7d3ff72 commit 3530313
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 46 deletions.
2 changes: 1 addition & 1 deletion lib/hub/args.rb
Expand Up @@ -54,7 +54,7 @@ def skip?
# 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
Expand Down
30 changes: 0 additions & 30 deletions lib/hub/commands.rb
Expand Up @@ -556,35 +556,6 @@ def improved_help_text
# 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)
Expand All @@ -596,7 +567,6 @@ def browse_command(args)
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
Expand Down
37 changes: 37 additions & 0 deletions lib/hub/context.rb
Expand Up @@ -155,5 +155,42 @@ def github_url(options = {})
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
31 changes: 16 additions & 15 deletions test/hub_test.rb
@@ -1,6 +1,7 @@
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'helper'
require 'webmock/test_unit'
require 'rbconfig'

WebMock::BodyPattern.class_eval do
undef normalize_hash
Expand All @@ -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

Expand Down Expand Up @@ -720,7 +721,7 @@ def test_custom_browser
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
Expand All @@ -729,7 +730,7 @@ def test_linux_browser
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
Expand All @@ -739,7 +740,7 @@ def test_no_browser
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
Expand Down Expand Up @@ -834,14 +835,14 @@ def assert_browser(browser)
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

0 comments on commit 3530313

Please sign in to comment.