From 353031307e704d860826fc756ff0070be5e1b430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 29 Mar 2011 12:14:06 +0200 Subject: [PATCH] true cross-platform `command?` and `browser_launcher` methods --- lib/hub/args.rb | 2 +- lib/hub/commands.rb | 30 ------------------------------ lib/hub/context.rb | 37 +++++++++++++++++++++++++++++++++++++ test/hub_test.rb | 31 ++++++++++++++++--------------- 4 files changed, 54 insertions(+), 46 deletions(-) diff --git a/lib/hub/args.rb b/lib/hub/args.rb index 22ebaed7..7df98e0a 100644 --- a/lib/hub/args.rb +++ b/lib/hub/args.rb @@ -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 diff --git a/lib/hub/commands.rb b/lib/hub/commands.rb index 4057baab..09854ce9 100644 --- a/lib/hub/commands.rb +++ b/lib/hub/commands.rb @@ -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 diff --git a/lib/hub/context.rb b/lib/hub/context.rb index 3811dcb8..e872f9e6 100644 --- a/lib/hub/context.rb +++ b/lib/hub/context.rb @@ -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 diff --git a/test/hub_test.rb b/test/hub_test.rb index c3964776..6865414b 100644 --- a/test/hub_test.rb +++ b/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 @@ -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 -- GitLab