Ignore Gemfile `source` if source not accessible

Question

We have an internal gem server to store some organisation specific gems. We use it via a source option in Gemfile:

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

The internal gem server is only available on the internal network.

If I am off the network I can run bundle if:

  1. I comment out the source declaration (and associated end)
  2. The gems defined in the source group are already installed on the system.

That means that if I am working from home, I need to remember to comment out the source declaration, and then remember to uncomment it again before committing any change.

Is there a way to modify the Gemfile so that it will detect that the source is unavailable and ignore it? That is, can I configure Gemfile so I don't have to comment out those lines each time I work away from the local network?


Show source
| ruby   | rubygems   | bundler   | gemfile   2016-09-08 11:09 1 Answers

Answers ( 1 )

  1. 2016-09-08 11:09

    You can add arbitrary Ruby to your Gemfile, so you could do something like:

    if (some check if hostname resolves)
      source 'https://local-gems.example.com' do
        gem 'local-gem'
      end
    end
    

    So for example, you can use curl like this:

    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
    
◀ Go back