Import a gemset without bundler

Here’s the scenario…

I’m trying to get a Rails 2.1.1 application up-and-running on my Mac (Lion). The application uses Ruby 1.8.7-p334. Bundler is not used so there’s no chance of doing a bundle install… drat!

I have a gemset export from another developer’s machine, which was created by doing this:

rvm gemset export my_app

That gives me a file which looks like this (truncated for brevity)…

# my_app.gems generated gem export file. Note…
actionmailer -v2.1.1
actionpack -v2.1.1
activerecord -v2.1.1
activeresource -v2.1.1
activesupport -v2.1.1

So, in theory it should be as easy as pie to import this gemset like this:

rvm gemset import my_app

Wrong!

The main problem arises when you install these gems, because the dependencies are not managed properly (this is what bundler was created for). So you end up with multiple versions of gems installed.

Pain.

The trick now is I need to be able to do a diff between my currently installed list of gems and the desired list given to me by the other developer. You could do this manually, but I’m far too lazy for that…

So, I created a little ruby script which will accept two files to be used for the diff operation, together with a third optional argument depending on whether I want to install or remove the gems.

Here’s the script…

#!/usr/bin/env ruby

unless ARGV.count >= 2
  puts "Please provide two files to perform diff..."
  exit
end

def load_file file
  arr = File.readlines(file).map(&:chomp).compact
  arr.shift if arr.first =~ /^#/
  arr
end

f1, f2 = load_file(ARGV[0]), load_file(ARGV[1])
command = ARGV[2] || 'install'

((f1 - f2) + (f2 - f1)).uniq.each do |gem|
  puts "gem #{command} #{gem}"
end

Save this into a file called file_diff.rb and make the file executable:

chmod +x file_diff.rb

Now, it’s easy to get the exact gems installed by following these simple steps…

./file_diff.rb file1 file2 uninstall > uninstall.sh
chmod x+ uninstall.sh
./uninstall.sh

And that’s it! You should now have the same gems in your gemset that your friend has in his gemset, as per his exported list.

One thought on “Import a gemset without bundler

Leave a comment