【问题标题】:Ignore Gemfile `source` if source not accessible如果源不可访问,则忽略 Gemfile `source`
【发布时间】:2016-09-08 09:04:14
【问题描述】:

我们有一个内部 gem 服务器来存储一些组织特定的 gem。我们通过Gemfile 中的源选项使用它:

source 'https://local-gems.example.com' do
  gem 'local-gem'
end

内部 gem 服务器仅在内部网络上可用。

如果我不在网络上,我可以运行bundle 如果:

  1. 我注释掉了source 声明(以及相关的end
  2. 源组中定义的 gem 已安装在系统上。

这意味着如果我在家工作,我需要记住注释掉 source 声明,然后记得在提交任何更改之前再次取消注释。

有没有办法修改Gemfile,让它检测到源不可用并忽略它?也就是说,我可以配置Gemfile,这样我每次离开本地网络时就不必注释掉那些行了吗?

【问题讨论】:

    标签: ruby rubygems bundler gemfile


    【解决方案1】:

    您可以将任意 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
    

    【讨论】:

    • 你说得对,使用system 是更好的选择。感谢您对我的编辑进行编辑。
    • 我已经修改了答案,以便仅在系统确定本地 gem 服务器不可用时才回退到默认行为。如果curl不可用,系统会尝试连接本地gem服务器。如果它不存在,错误消息将反映这一点。
    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    相关资源
    最近更新 更多