The other day, a friend of mine told me about some rubygem problems she was having. Her company’s firewall was a bit flaky and rubygems wasn’t able to auto grab dependencies for her. What she wanted was a way to determine a gem’s dependencies without yet installing it.

The gem dependency command is supposed to have an option to let you see the dependencies on a gem – but it only works on already installed gems. Then there’s the gem specification command. Great, it takes a gemfile as input, just what she needs. Unfortunately, it’s a case of false advertising. Even though the docs say it will take a gemfile, it really only works on installed gems.

% gem dependency rails-1.2.1.gem
No match found for rails-1.2.1.gem (> 0))

% gem specification rails-1.2.1.gem
ERROR:  Unknown gem rails-1.2.1.gem

So I did some investigation and ended up writing a few lines of Ruby. Save the following in a file named, gemdep.rb.

require 'rubygems/format'

Gem::Format.from_file_by_path(ARGV[0]).spec.dependencies.each do |d|
  puts "#{d.name} #{d.version_requirements.as_list}"
end

And test it as so

% ruby gemdep.rb rails-1.2.1.gem
rake >= 0.7.1
activesupport = 1.4.0
activerecord = 1.15.1
actionpack = 1.13.1
actionmailer = 1.3.1
actionwebservice = 1.2.1

The solution is always just a few lines of Ruby, isn’t it? I sometimes wonder if world hunger couldn’t be solved by a few well written lines of Ruby and

acts_as_plentiful :food