2

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?

ReggieB
  • 8,100
  • 3
  • 38
  • 46

1 Answers1

3

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
ReggieB
  • 8,100
  • 3
  • 38
  • 46
Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
  • 1
    You're right, using `system` is a better option. Thank you for that edit to my edit. – ReggieB Sep 08 '16 at 09:56
  • I've modified the answer so that it only falls back to the default behaviour if the system is sure that the local gem server is not available. If `curl` is not available, the system will try to connect to the local gem server. If it then isn't present, the error message will reflect that. – ReggieB Sep 08 '16 at 10:42
  • See http://unix.stackexchange.com/questions/196549/hide-curl-output for purpose of `> /dev/null` – ReggieB Sep 09 '16 at 10:21