Freezing gems with Gem.use_path

Edge rails now has gem dependency support built in, with tasks to freeze in vendor/gems.

I have tried a bunch of different methods, and things like setting $GEM_HOME or using gemsonrails work well. But lately I have been using the Gem.use_paths method.

If you put this in the config/environments rb:

Gem.use_paths(nil, [ "#{RAILS_ROOT}/vendor/rubygems" ])
# Gem.path is now:
# ["/Users/ghandford/Projects/my_rails_proj/vendor/rubygems", "/Library/Ruby/Gems/1.8"]

The vendor/rubygems directory is the same structure as system gems folder:

  vendor/rubygems/cache
  vendor/rubygems/doc
  vendor/rubygems/gems
  vendor/rubygems/specifications

The convention I use is any non-native gems get frozen with the project, and then all the native gems go in the system gem path, which gets you pretty close to clone/checkout and rake setup.

As far as unpacking and install gems, you just use gem install with GEM_HOME of your local project:

GEM_HOME=`pwd`/vendor/rubygems gem install capitate

A benefit of the use_paths method is that you can create scripts in your Rails project that use gems frozen in your project (and not necessarily have to require rails or environment.rb to get at them).

./script/my_non_rails_script
  require 'rubygems'
  Gem.use_paths(nil, [ File.dirname(__FILE__) + "/../vendor/rubygems" ])

Has anyone else tried this before?

Tags: , , ,

Leave a Reply