Skip to content

Instantly share code, notes, and snippets.

@craic
Created April 24, 2009 15:42
Show Gist options
  • Save craic/101154 to your computer and use it in GitHub Desktop.
Save craic/101154 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# install_gems.rb
# Copyright 2009 Robert Jones jones@craic.com
# Use at your own risk, no warranty, etc, etc.
# Given a file of gems from another machine, recreate the same environment
# For more information see this post:
# http://craiccomputing.blogspot.com/2009/04/installing-ruby-19-and-gems-on-machine.html
# 1: Generate the list of gems with 'gem list' or 'gem list --no-versions'
# 2: Delete lines with any gems you don't want to mirror, or comment them out with
# a '#' in the first column
# 3: Transfer the file to the target machine
# 4: Run this script with the filename as the only argument (run as root)
# If you want to install documentation then add the 'docs' option
# Capture the stderr output in a file for debugging
def already_installed(gem)
result = `gem list --local --no-versions #{gem}`
installed = false
result.each_line do |str|
if str =~ /^#{gem}\s/
installed = true
break
end
end
installed
end
if ARGV.length < 1 or ARGV.length > 2
STDERR.puts "Usage: #{$0} <list of gems> [docs]"
exit
end
doc_options = ARGV.length == 2 ? '' : ' --no-ri --no-rdoc'
filename = ARGV[0]
open(filename, 'r').each_line do |line|
line.chomp!
if line =~ /^\s*$/ or line =~ /^\*\*\*/ or line =~ /^#/
next
elsif line =~ /^(\S+)/
gem = $1
# Does the system already have this gem? - This won't pay attention to versions..
installed = already_installed(gem)
if not installed
puts "Installing #{gem}\n"
`gem install #{doc_options} #{gem} &> tmp_error_log`
installed = already_installed(gem)
if not installed # something went wrong
puts "----------------------------------------------------------\nFAILED"
open('tmp_error_log', 'r') { |f| puts f.read }
File.delete('tmp_error_log')
puts "----------------------------------------------------------\n"
end
else
puts "Skipping #{gem}\n"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment