pry--pry/lib/pry/default_commands/gems.rb

85 lines
2.9 KiB
Ruby
Raw Normal View History

class Pry
module DefaultCommands
2011-05-07 05:32:05 +00:00
Gems = Pry::CommandSet.new do
2012-01-15 21:43:18 +00:00
create_command "gem-install", "Install a gem and refresh the gem cache.", :argument_required => true do |gem|
banner <<-BANNER
Usage: gem-install GEM_NAME
Installs the given gem and refreshes the gem cache so that you can immediately 'require GEM_FILE'
BANNER
def setup
require 'rubygems/dependency_installer' unless defined? Gem::DependencyInstaller
end
2012-01-15 21:43:18 +00:00
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."
end
end
end
2012-01-15 21:43:18 +00:00
create_command "gem-cd", "Change working directory to specified gem's directory.", :argument_required => true do |gem|
banner <<-BANNER
Usage: gem-cd GEM_NAME
2012-01-15 21:43:18 +00:00
Change the current working directory to that in which the given gem is installed.
BANNER
2012-01-15 21:43:18 +00:00
def process(gem)
specs = Gem::Specification.respond_to?(:each) ? Gem::Specification.find_all_by_name(gem) : Gem.source_index.find_name(gem)
spec = specs.sort { |a,b| Gem::Version.new(b.version) <=> Gem::Version.new(a.version) }.first
if spec
Dir.chdir(spec.full_gem_path)
output.puts(Dir.pwd)
else
raise CommandError, "Gem `#{gem}` not found."
end
end
end
2012-01-15 21:43:18 +00:00
create_command "gem-list", "List and search installed gems." do |pattern|
banner <<-BANNER
Usage: gem-list [REGEX]
List all installed gems, when a regex is provided, limit the output to those that
match the regex.
BANNER
def process(pattern=nil)
2012-01-29 03:50:38 +00:00
pattern = Regexp.compile(pattern || '')
2012-01-15 21:43:18 +00:00
gems = if Gem::Specification.respond_to?(:each)
Gem::Specification.select{|spec| spec.name =~ pattern }.group_by(&:name)
else
Gem.source_index.gems.values.group_by(&:name).select { |gemname, specs| gemname =~ pattern }
end
gems.each do |gem, specs|
specs.sort! do |a,b|
Gem::Version.new(b.version) <=> Gem::Version.new(a.version)
end
versions = specs.each_with_index.map do |spec, index|
index == 0 ? text.bright_green(spec.version.to_s) : text.green(spec.version.to_s)
end
output.puts "#{text.default gem} (#{versions.join ', '})"
end
end
end
end
end
end