Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
hub
提交
35303130
H
hub
项目概览
github
/
hub
11 个月 前同步成功
通知
3
Star
22523
Fork
2406
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
35303130
编写于
3月 29, 2011
作者:
M
Mislav Marohnić
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
true cross-platform `command?` and `browser_launcher` methods
上级
7d3ff72b
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
54 addition
and
46 deletion
+54
-46
lib/hub/args.rb
lib/hub/args.rb
+1
-1
lib/hub/commands.rb
lib/hub/commands.rb
+0
-30
lib/hub/context.rb
lib/hub/context.rb
+37
-0
test/hub_test.rb
test/hub_test.rb
+16
-15
未找到文件。
lib/hub/args.rb
浏览文件 @
35303130
...
...
@@ -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
...
...
lib/hub/commands.rb
浏览文件 @
35303130
...
...
@@ -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
...
...
lib/hub/context.rb
浏览文件 @
35303130
...
...
@@ -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
test/hub_test.rb
浏览文件 @
35303130
$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
::
Co
mmands
.
class_eval
do
remove_method
:
command?
define_method
:
command?
do
|
name
|
COMMANDS
.
include?
(
name
)
Hub
::
Co
ntext
.
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录