Created
December 6, 2009 23:25
-
-
Save seeflanigan/250491 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
module SimpleGemDependecyChecker | |
class << self | |
def initialize | |
require_libraries | |
system_gems = get_installed_gems | |
required_gems = get_required_gems | |
verify_gems(required_gems, system_gems) | |
end | |
def require_libraries | |
begin | |
require 'rubygems' | |
require 'geminstaller' | |
rescue LoadError => e | |
if e.message.match(/rubygems/) | |
puts "Could not find RubyGems. Please confirm that it is installed." | |
else | |
raise e | |
end | |
rescue Gem::LoadError => e | |
if e.message.match(/geminstaller/) | |
puts "Could not find geminstaller. Use the following command to install:" | |
puts "gem install geminstaller" | |
else | |
raise e | |
end | |
end | |
def build_geminstaller_config_string | |
config_files = [] | |
['default', 'production', RAILS_ENV ].each do |env| | |
config_files << File.join(File.expand_path(RAILS_ROOT), 'config', '#{env}.gems.yml') | |
end | |
"--config=#{config_files.join(',')}" | |
end | |
# get gems defined in geminstaller config files | |
def get_required_gems | |
config = build_geminstaller_config_string | |
gi = GemInstaller.create_application(config) | |
gi_gems = gi.create_gems_from_config | |
gi_gems | |
end | |
# get all currently installed gems | |
def get_installed_gems | |
specs = Gem::GemPathSearcher.new | |
gems = specs.instance_variable_get("@gemspecs") | |
end | |
def verify_gems(gi_gems, system_gems) | |
gi_gems.each do |g| | |
sys_gem = system_gems.find(g.name) | |
report_missing_gem(g) unless sys_gem | |
compare_gems(g, sys_gem) | |
end | |
end | |
def compare_gems(gi_gem, sys_gem) | |
req = Gem::Requirement.create(g.version) | |
req.satisfied_by? installed_gem.version | |
end | |
def report_missing_gem(missing_gem) | |
puts "Could not find #{missing_gem.name}, version #{missing_gem.version}" | |
puts "Please use one of the following commands to ensure that it is installed:" | |
puts "geminstaller #=> This will use geminstaller to install all required gems" | |
puts "rake gems:sync #=> Wrapper for geminstaller to install gems for a given RAILS_ENV" | |
puts "gem install #{missing_gem.name} -v #{missing_gem.version} #=> Will install only this gem" | |
Process.exit(1) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment