2013-01-08 18:27:27 -05:00
|
|
|
require 'rubygems'
|
|
|
|
|
2013-01-08 17:24:53 -05:00
|
|
|
class Pry
|
|
|
|
module Rubygem
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def installed?(name)
|
|
|
|
if Gem::Specification.respond_to?(:find_all_by_name)
|
|
|
|
Gem::Specification.find_all_by_name(name).any?
|
|
|
|
else
|
|
|
|
Gem.source_index.find_name(name).first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get the gem spec object for the given gem name.
|
|
|
|
#
|
|
|
|
# @param [String] name
|
|
|
|
# @return [Gem::Specification]
|
|
|
|
def spec(name)
|
|
|
|
specs = if Gem::Specification.respond_to?(:each)
|
|
|
|
Gem::Specification.find_all_by_name(name)
|
|
|
|
else
|
|
|
|
Gem.source_index.find_name(name)
|
|
|
|
end
|
|
|
|
|
2013-11-06 01:37:48 -05:00
|
|
|
first_spec = specs.sort_by{ |spec| Gem::Version.new(spec.version) }.last
|
2013-01-08 17:24:53 -05:00
|
|
|
|
2013-03-02 19:01:55 -05:00
|
|
|
first_spec or raise CommandError, "Gem `#{name}` not found"
|
2013-01-08 17:24:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# List gems matching a pattern.
|
|
|
|
#
|
|
|
|
# @param [Regexp] pattern
|
|
|
|
# @return [Array<Gem::Specification>]
|
|
|
|
def list(pattern = /.*/)
|
|
|
|
if Gem::Specification.respond_to?(:each)
|
|
|
|
Gem::Specification.select{|spec| spec.name =~ pattern }
|
|
|
|
else
|
|
|
|
Gem.source_index.gems.values.select{|spec| spec.name =~ pattern }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Completion function for gem-cd and gem-open.
|
|
|
|
#
|
|
|
|
# @param [String] so_far what the user's typed so far
|
|
|
|
# @return [Array<String>] completions
|
|
|
|
def complete(so_far)
|
|
|
|
if so_far =~ / ([^ ]*)\z/
|
|
|
|
self.list(%r{\A#{$2}}).map(&:name)
|
|
|
|
else
|
|
|
|
self.list.map(&:name)
|
|
|
|
end
|
|
|
|
end
|
2013-01-08 18:27:27 -05:00
|
|
|
|
|
|
|
# Installs a gem with all its dependencies.
|
|
|
|
#
|
|
|
|
# @param [String] name
|
|
|
|
# @return [void]
|
|
|
|
def install(name)
|
2013-02-20 06:02:34 -05:00
|
|
|
gemrc_opts = Gem.configuration['gem'].split(' ')
|
|
|
|
destination = if gemrc_opts.include?('--user-install')
|
|
|
|
Gem.user_dir
|
|
|
|
elsif File.writable?(Gem.dir)
|
|
|
|
Gem.dir
|
|
|
|
else
|
|
|
|
Gem.user_dir
|
|
|
|
end
|
2013-01-08 18:27:27 -05:00
|
|
|
installer = Gem::DependencyInstaller.new(:install_dir => destination)
|
|
|
|
installer.install(name)
|
|
|
|
rescue Errno::EACCES
|
|
|
|
raise CommandError,
|
2013-03-06 02:19:41 -05:00
|
|
|
"Insufficient permissions to install #{ Pry::Helpers::Text.green(name) }."
|
2013-01-08 18:27:27 -05:00
|
|
|
rescue Gem::GemNotFoundException
|
2013-02-20 06:02:34 -05:00
|
|
|
raise CommandError,
|
2013-03-06 02:19:41 -05:00
|
|
|
"Gem #{ Pry::Helpers::Text.green(name) } not found. Aborting installation."
|
2013-01-08 18:27:27 -05:00
|
|
|
else
|
|
|
|
Gem.refresh
|
|
|
|
end
|
2013-01-08 17:24:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|