From 4c56c73e9db5e00a47b79292248bf477da1d328f Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Wed, 9 Jan 2013 01:27:27 +0200 Subject: [PATCH] Implement `Rubygem::install` Use the code of `gem-install` command as a basis. Signed-off-by: Kyrylo Silin --- lib/pry/commands/gem_install.rb | 16 +++------------- lib/pry/rubygem.rb | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/pry/commands/gem_install.rb b/lib/pry/commands/gem_install.rb index aa524db4..7b7df022 100644 --- a/lib/pry/commands/gem_install.rb +++ b/lib/pry/commands/gem_install.rb @@ -16,19 +16,9 @@ class Pry end def process(gem) - begin - destination = File.writable?(Gem.dir) ? Gem.dir : Gem.user_dir - installer = Gem::DependencyInstaller.new :install_dir => destination - installer.install gem - rescue Errno::EACCES - raise CommandError, "Insufficient permissions to install `#{text.green gem}`." - rescue Gem::GemNotFoundException - raise CommandError, "Gem `#{text.green gem}` not found." - else - Gem.refresh - output.puts "Gem `#{text.green gem}` installed." - require gem - end + Rubygem.install(gem) + output.puts "Gem `#{ text.green(gem) }` installed." + require gem end end diff --git a/lib/pry/rubygem.rb b/lib/pry/rubygem.rb index 7d4c2dfb..ced2cd4b 100644 --- a/lib/pry/rubygem.rb +++ b/lib/pry/rubygem.rb @@ -1,9 +1,10 @@ +require 'rubygems' + class Pry module Rubygem class << self def installed?(name) - require 'rubygems' if Gem::Specification.respond_to?(:find_all_by_name) Gem::Specification.find_all_by_name(name).any? else @@ -50,6 +51,23 @@ class Pry self.list.map(&:name) end end + + # Installs a gem with all its dependencies. + # + # @param [String] name + # @return [void] + def install(name) + destination = File.writable?(Gem.dir) ? Gem.dir : Gem.user_dir + installer = Gem::DependencyInstaller.new(:install_dir => destination) + installer.install(name) + rescue Errno::EACCES + raise CommandError, + "Insufficient permissions to install `#{ text.green(name) }`." + rescue Gem::GemNotFoundException + raise CommandError, "Gem `#{ text.green(name) }` not found." + else + Gem.refresh + end end end