-
-
Save jtimberman/100678 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2009 Damian Janowski. MIT License. | |
# http://gist.github.com/100655 | |
# | |
# Very simple script to handle Gem dependencies. | |
# It supports a basic vendoring system | |
# (just gem unpack to ./vendor) | |
# | |
# List your dependencies below and require this | |
# file at the very top of your program (think | |
# of it as a replacement for your require "rubygems"). | |
# | |
# Also try: | |
# | |
# $ ruby dependencies.rb | |
# | |
# to get a list of the required gems and to see | |
# which of them are vendored. | |
require 'rubygems' | |
RACK_ENV = ENV['RACK_ENV'] || 'development' unless defined? RACK_ENV | |
dependencies = <<-EOS | |
# Things you should know about version requirements: | |
# http://rubygems.org/read/chapter/16 | |
# | |
# List your dependencies here. | |
# | |
# rack ~> 0.9 | |
# haml ~> 2.0 | |
# hpricot 0.8 (test) | |
# ruby-debug (development, test) | |
EOS | |
missing = [] | |
dependencies.each_line do |line| | |
next unless line =~ /^([\w\-_]+) ?([<~=> \d\.]+)?( \(([\w,]+)\))?$/ | |
name, version, env = $1, $2, $4 | |
next if env && !env.split(/\, ?/).include?(RACK_ENV.to_s) | |
version = nil if version.empty? | |
vendor_name = name.dup | |
vendor_name << "-#{version[/([\d\.]+)$/, 1]}" if version | |
vendor_path = File.join(File.dirname(__FILE__), 'vendor', "#{vendor_name}*", 'lib') | |
if vendor_path = Dir[vendor_path].first | |
# Vendored gem | |
$:.unshift(vendor_path) | |
else | |
# RubyGems | |
begin | |
gem(*[name, version].compact) | |
rescue Gem::LoadError => e | |
$stderr.puts "=> #{e.message}" | |
missing << [name, version] | |
end | |
end | |
if $0 == __FILE__ | |
puts [name, version, ("(in #{vendor_path})" if vendor_path)].compact.join(" ") | |
end | |
end | |
if missing.any? | |
$stderr.puts if $0 == __FILE__ | |
missing.each do |spec| | |
$stderr.puts "sudo gem install #{spec[0]} #{('-v %s ' % spec[1][/([0-9\.]+)$/] if spec[1])}--no-rdoc --no-ri" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment