mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Extract out gem related code to Pry::Rubygem
And fix some typos, and prettify some lines. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
This commit is contained in:
parent
a4e4ef023a
commit
3c60ee88e5
9 changed files with 65 additions and 50 deletions
|
@ -249,3 +249,4 @@ require 'pry/cli'
|
|||
require 'pry/pager'
|
||||
require 'pry/terminal_info'
|
||||
require 'pry/editor'
|
||||
require 'pry/rubygem'
|
||||
|
|
|
@ -412,7 +412,7 @@ class Pry
|
|||
def call_safely(*args)
|
||||
unless dependencies_met?
|
||||
gems_needed = Array(command_options[:requires_gem])
|
||||
gems_not_installed = gems_needed.select { |g| !gem_installed?(g) }
|
||||
gems_not_installed = gems_needed.select { |g| !Rubygem.installed?(g) }
|
||||
output.puts "\nThe command '#{command_name}' is #{text.bold("unavailable")} because it requires the following gems to be installed: #{(gems_not_installed.join(", "))}"
|
||||
output.puts "-"
|
||||
output.puts "Type `install-command #{command_name}` to install the required gems and activate this command."
|
||||
|
|
|
@ -12,12 +12,12 @@ class Pry
|
|||
BANNER
|
||||
|
||||
def process(gem)
|
||||
Dir.chdir(gem_spec(gem).full_gem_path)
|
||||
Dir.chdir(Rubygem.spec(gem).full_gem_path)
|
||||
output.puts(Dir.pwd)
|
||||
end
|
||||
|
||||
def complete(str)
|
||||
gem_complete(str)
|
||||
Rubygem.complete(str)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ class Pry
|
|||
|
||||
def process(pattern = nil)
|
||||
pattern = Regexp.compile(pattern || '')
|
||||
gems = gem_list(pattern).group_by(&:name)
|
||||
gems = Rubygem.list(pattern).group_by(&:name)
|
||||
|
||||
gems.each do |gem, specs|
|
||||
specs.sort! do |a,b|
|
||||
|
|
|
@ -13,13 +13,13 @@ class Pry
|
|||
BANNER
|
||||
|
||||
def process(gem)
|
||||
Dir.chdir(gem_spec(gem).full_gem_path) do
|
||||
Dir.chdir(Rubygem.spec(gem).full_gem_path) do
|
||||
Pry::Editor.invoke_editor(".", 0, false)
|
||||
end
|
||||
end
|
||||
|
||||
def complete(str)
|
||||
gem_complete(str)
|
||||
Rubygem.complete(str)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ class Pry
|
|||
gems_to_install = Array(command.options[:requires_gem])
|
||||
|
||||
gems_to_install.each do |g|
|
||||
next if gem_installed?(g)
|
||||
next if Rubygem.installed?(g)
|
||||
output.puts "Installing `#{g}` gem..."
|
||||
|
||||
begin
|
||||
|
|
|
@ -31,11 +31,6 @@ class Pry
|
|||
command_match.last if command_match
|
||||
end
|
||||
|
||||
def gem_installed?(gem_name)
|
||||
require 'rubygems'
|
||||
Gem::Specification.respond_to?(:find_all_by_name) ? !Gem::Specification.find_all_by_name(gem_name).empty? : Gem.source_index.find_name(gem_name).first
|
||||
end
|
||||
|
||||
def not_a_real_file?(file)
|
||||
file =~ /(\(.*\))|<.*>/ || file =~ /__unknown__/ || file == "" || file == "-e"
|
||||
end
|
||||
|
@ -43,7 +38,7 @@ class Pry
|
|||
def command_dependencies_met?(options)
|
||||
return true if !options[:requires_gem]
|
||||
Array(options[:requires_gem]).all? do |g|
|
||||
gem_installed?(g)
|
||||
Rubygem.installed?(g)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -147,43 +147,6 @@ class Pry
|
|||
|
||||
Range.new(a, b)
|
||||
end
|
||||
|
||||
# Get the gem spec object for the given gem
|
||||
# @param [String] gem name
|
||||
# @return [Gem::Specification]
|
||||
def gem_spec(gem)
|
||||
specs = if Gem::Specification.respond_to?(:each)
|
||||
Gem::Specification.find_all_by_name(gem)
|
||||
else
|
||||
Gem.source_index.find_name(gem)
|
||||
end
|
||||
|
||||
spec = specs.sort_by{ |spec| Gem::Version.new(spec.version) }.first
|
||||
|
||||
spec or raise CommandError, "Gem `#{gem}` not found"
|
||||
end
|
||||
|
||||
# List gems matching a pattern
|
||||
# @param [Regexp] pattern
|
||||
# @return [Array<Gem::Specification>]
|
||||
def gem_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 gem_complete(so_far)
|
||||
if so_far =~ / ([^ ]*)\z/
|
||||
gem_list(%r{\A#{$2}}).map(&:name)
|
||||
else
|
||||
gem_list.map(&:name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
56
lib/pry/rubygem.rb
Normal file
56
lib/pry/rubygem.rb
Normal file
|
@ -0,0 +1,56 @@
|
|||
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
|
||||
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
|
||||
|
||||
spec = specs.sort_by{ |spec| Gem::Version.new(spec.version) }.first
|
||||
|
||||
spec or raise CommandError, "Gem `#{name}` not found"
|
||||
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
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue