Generate gem.yml and .gems for Rails

A rake task which will generate gem configuration files based on your config.gem specifications in Rails. This comes in handy when deploying to Engine Yard, or Heroku. Just run it before deploying to update the files.

namespace :gems do
  desc "Spit out gems.yml and .gems in root of app (for Heroku + EY etc.)"
  task :specify => :environment do
    gems = Rails.configuration.gems
    
    # output gems.yml
    yaml = File.join(RAILS_ROOT, "gems.yml")
    File.open(yaml, "w") do |f|
      output = []
      gems.each do |gem|
        spec = { "name" => gem.name, "version" => gem.version_requirements.to_s }
        spec["install_options"] = "--source #{gem.source}" if gem.source
        output << spec
      end
      f.write output.to_yaml
      puts output.to_yaml
    end
    
    # output .gems
    dot_gems = File.join(RAILS_ROOT, ".gems")
    File.open(dot_gems, "w") do |f|
      output = []
      gems.each do |gem|
        spec = "#{gem.name} --version '#{gem.version_requirements.to_s}'"
        spec << " --source #{gem.source}" if gem.source
        output << spec
      end
      f.write output.join("\n")
      puts output.join("\n")
    end
  end
end

A mix of design and code, by Mark Dodwell, web designer and Ruby on Rails developer.