Skip to content

Commit

Permalink
Extract out gem related code to Pry::Rubygem
Browse files Browse the repository at this point in the history
And fix some typos, and prettify some lines.

Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
  • Loading branch information
kyrylo committed Jan 8, 2013
1 parent a4e4ef0 commit 3c60ee8
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 50 deletions.
1 change: 1 addition & 0 deletions lib/pry.rb
Expand Up @@ -249,3 +249,4 @@ module ExtendCommandBundle
require 'pry/pager'
require 'pry/terminal_info'
require 'pry/editor'
require 'pry/rubygem'
2 changes: 1 addition & 1 deletion lib/pry/command.rb
Expand Up @@ -412,7 +412,7 @@ def pass_block(arg_string)
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."
Expand Down
4 changes: 2 additions & 2 deletions lib/pry/commands/gem_cd.rb
Expand Up @@ -12,12 +12,12 @@ class Command::GemCd < Pry::ClassCommand
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

Expand Down
2 changes: 1 addition & 1 deletion lib/pry/commands/gem_list.rb
Expand Up @@ -13,7 +13,7 @@ class Command::GemList < Pry::ClassCommand

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|
Expand Down
4 changes: 2 additions & 2 deletions lib/pry/commands/gem_open.rb
Expand Up @@ -13,13 +13,13 @@ class Command::GemOpen < Pry::ClassCommand
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

Expand Down
2 changes: 1 addition & 1 deletion lib/pry/commands/install_command.rb
Expand Up @@ -24,7 +24,7 @@ def process(name)
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
Expand Down
7 changes: 1 addition & 6 deletions lib/pry/helpers/base_helpers.rb
Expand Up @@ -31,19 +31,14 @@ def find_command(name, set = Pry::Commands)
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

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

Expand Down
37 changes: 0 additions & 37 deletions lib/pry/helpers/command_helpers.rb
Expand Up @@ -147,43 +147,6 @@ def absolute_index_range(range_or_number, array_length)

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 changes: 56 additions & 0 deletions lib/pry/rubygem.rb
@@ -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

0 comments on commit 3c60ee8

Please sign in to comment.