Check for gems with local :path before deploy

2019-05-26 08:02发布

问题:

For some Rails applications, I'd like to have a safe-guard when I deploy to check if I have some gems configured to be looked up at a local path.

A little bit of context may help to understand.

When I'm in development mode, I want to have a gem in "local mode". In my Gemfile it is configured like this : gem 'my_gem', '~> 0.9', :path => './path/to/my_gem'.

In production, I want to be like this : gem 'my_gem', '~> 0.9', :git => 'git@git.example.com:my_gem.git'.

I've tried to make a shell script (or function) to read the Gemfile.lock and exit with an error if the gem is in "local mode".

My deployment scripts could use this to abort if I've forgotten to switch back to the proper mode.

Any help will be appreciated. Thanks

回答1:

Use

group :development do
  gem 'my_gem_for_development', '~> 0.9', :require => './path/to/my_gem/lib/my_gem.rb' , :path => './path/to/my_gem/lib'
end
group :production do
  gem 'my_gem', '~> 0.9', :git => 'git@git.example.com:my_gem.git'
end


回答2:

Is this a gem that you're developing? Why not just write the gem to look at the rails env and change settings based on that. Then you can one canonical version of the gem and you won't have to worry about checking to see which gem version you're using. Otherwise, bor1s' solution will work just fine.