summaryrefslogtreecommitdiff
path: root/lib/pry/rubygem.rb
blob: 072e42a074287ac9fb77549b07dc2775576bd323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'rubygems'

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

        first_spec = specs.sort_by { |spec| Gem::Version.new(spec.version) }.last

        first_spec || 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

      # Installs a gem with all its dependencies.
      #
      # @param [String] name
      # @return [void]
      def install(name)
        require 'rubygems/dependency_installer'
        gem_config = Gem.configuration['gem']
        gemrc_opts = (gem_config.nil? ? "" : gem_config.split(' '))
        destination = if gemrc_opts.include?('--user-install')
                        Gem.user_dir
                      elsif File.writable?(Gem.dir)
                        Gem.dir
                      else
                        Gem.user_dir
                      end
        installer = Gem::DependencyInstaller.new(install_dir: destination)
        installer.install(name)
      rescue Errno::EACCES
        raise CommandError,
          "Insufficient permissions to install #{green(name)}."
      rescue Gem::GemNotFoundException
        raise CommandError,
          "Gem #{green(name)} not found. Aborting installation."
      else
        Gem.refresh
      end
    end

  end
end