我们有一个内部 gem 服务器来存储一些组织特定的 gem 。我们通过 Gemfile
中的源选项使用它:
source 'https://local-gems.example.com' do
gem 'local-gem'
end
内部gem服务器仅在内网可用。
如果我断网,我可以运行 bundle
如果:
- 我注释掉了
source
声明(以及相关的end
) - 源组中定义的 gem 已安装在系统上。
这意味着,如果我在家工作,我需要记住注释掉 source
声明,然后记住在提交任何更改之前再次取消注释。
有没有办法修改Gemfile
,以便它检测到源不可用并忽略它?也就是说,我可以配置 Gemfile
以便每次远离本地网络时都不必注释掉这些行吗?
最佳答案
您可以将任意 Ruby 添加到 Gemfile 中,这样您就可以执行以下操作:
if (some check if hostname resolves)
source 'https://local-gems.example.com' do
gem 'local-gem'
end
end
例如,您可以像这样使用 curl
:
local_source = if system('curl -s https://local-gems.example.com > /dev/null') != false
# `curl` not available (`nil` returned)
# or local gem server accessible (`true` returned)
# try accessing:
'https://local-gems.example.com'
else
# Fall back on default behaviour
'https://rubygems.org'
end
source local_source do
gem 'local-gem'
end
关于ruby - 如果源无法访问,请忽略 Gemfile `source`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39386806/