module RippleNetworks module WarningShot class GemHandler DEFAULT_VERSION = ">0.0.0".freeze CONDITIONAL_RGX = /[^\d]*/.freeze VERSION_RGX = /[\d\.]+/.freeze =begin require "./lib/warning_shot/gem_handler.rb" gems = ["rake:>0.0.0","merb:>=0.4.0"] include RippleNetworks::WarningShot gh = GemHandler.new(gems) gh.add(["RedCloth:>2.4.0"]) gh.remove(["merb"]) gh.add("merb:>=0.5.0") gh.remove("RedCloth") =end attr_reader :gems, :missing_gems, :errors def initialize(gem_list,do_install=false,install_dir=nil) Gem.manage_gems @gri = Gem::RemoteInstaller.new @install_dir ||= Gem.dir @missing_gems = {} @gems = split(gem_list) @errors = {} check_gems install! if do_install end def split(gem_list) gems = {} gem_list.collect do |gem| gem_name, gem_ver = gem.chomp.split(":") gems[gem_name] = gem_ver || DEFAULT_VERSION #Default to newest gem end return gems end def add(gems) @gems.merge!(split(gems)) check_gems end def remove(gems) gems.collect { |g| @gems.delete(g) } check_gems end def install! failed_installs = 0 @errors = {} @missing_gems.each do |gem_name,gem_version| dep_installed, dep_message = install_dependencies(gem_name,gem_version) if dep_installed gem_install, gem_message = install_gem(gem_name,gem_version) unless gem_install failed_installs += 1 @errors[gem_name] = gem_message end else failed_installs += 1 @errors[gem_name] = gem_message end end check_gems return failed_installs == 0 end =begin rdoc *Name*:: installed? *Access*:: public *Description*:: Determine if gem is installed *Last_Edited*:: Tue Feb 19 12:40:40 PST 2008 ===Parameters * Name: gem_name * Description: rubygems gem name (case sensitive) * Datatype: String * Default: None * Required: True * Name: gem_version * Description: Gem version (ex ">= 1.0.3", "< 3.0", "3.0.1") * Datatype: String * Default: "> 0.0.0" * Required: false ===Returns *Description*:: Information queried gem *Datatype*:: Hash =end def installed?(gem_name,gem_version=DEFAULT_VERSION) gem_info = {} installed_versions = Gem::cache.search(gem_name) unless installed_versions.empty? #Gem is installed, determine version version_info = [] comparison_results = [] conditionals = gem_version.match(CONDITIONAL_RGX)[0] required_version = Gem::Version.create gem_version.match(VERSION_RGX)[0] installed_versions.each do |i_gem| comp_result = required_version <=> i_gem.version version_info.push(i_gem.version.version) comparison_results.push(comp_result) unless comparison_results.include? comp_result end gem_info[:versions] = version_info.join(",") if comparison_results.include?(0) && (conditionals =~ /=/ || conditionals.empty?) # && (conditionals =~ /<>/).nil? #Correct version installed gem_info[:installed] = true gem_info[:info] = "#{gem_name}:#{gem_version} is installed" elsif comparison_results.include?(1) && (conditionals =~ //) #Newer version installed gem_info[:installed] = true gem_info[:info] = "Newer version installed" else gem_info[:installed] = false gem_info[:info] = "Other version installed" end else #Gem is NOT installed gem_info[:installed] = false gem_info[:info] = "No versions installed" end return gem_info end private def install_dependencies(gem_name,gem_version) #Find Dependencies begin deps = @gri.find_gem_to_install gem_name, gem_version rescue Exception => e #Invalid Gem Name / Version return [false,"Bad gem name or version #{gem_name} #{gem_version}"] end #Determine what dependencies are missing missing_deps = @gri.find_dependencies_not_installed deps[0].dependencies #Install missing dependencies begin installed_deps = @gri.install_dependencies(missing_deps,true,@install_dir) rescue Errno::EACCES => e return [false, "Invalid permissions . . . try running w/ sudo"] rescue Gem::FilePermissionError => e return [false, "Invalid permissions . . . try running w/ sudo"] rescue Exception => e return [false,e.message] end return [true] #Check that the number of missing dependencies is equal to the number installed #if installed_deps.length == missing_deps.length #else #end end def install_gem(gem_name,gem_version) begin @gri.install(gem_name,gem_version,true,@install_dir) return [true] rescue Errno::EACCES => e return [false, "Invalid permissions . . . try running w/ sudo"] rescue Gem::FilePermissionError => e return [false, "Invalid permissions... try running w/ sudo"] rescue Exception => e return [false,e.message] end end =begin rdoc *Name*:: check_gems *Access*:: private *Description*:: Checks to see if the @gems are installed, results stored in @missing_gems *Last_Edited*:: Tue Feb 19 15:09:00 PST 2008 =end def check_gems @missing_gems = {} @gems.each do |gem_name,gem_version| gem_info = installed?(gem_name,gem_version) @missing_gems[gem_name] = gem_version unless gem_info[:installed] end end end end end