Class: Rabbit::GemFinder

Inherits:
Object
  • Object
show all
Includes:
GetText
Defined in:
lib/rabbit/gem-finder.rb

Constant Summary

Constants included from GetText

Rabbit::GetText::DOMAIN

Instance Method Summary collapse

Methods included from GetText

included

Constructor Details

#initialize(logger = nil) ⇒ GemFinder

Returns a new instance of GemFinder.


23
24
25
# File 'lib/rabbit/gem-finder.rb', line 23

def initialize(logger=nil)
  @logger = logger || Logger.default
end

Instance Method Details

#find(name, prefix) ⇒ Object


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
# File 'lib/rabbit/gem-finder.rb', line 27

def find(name, prefix)
  normalized_name = name.downcase
  unless normalized_name.start_with?(prefix)
    normalized_name = "#{prefix}#{normalized_name}"
  end

  retried = false
  spec = nil
  begin
    spec = Gem::Specification.find_by_name(name)
  rescue Gem::LoadError
    begin
      spec = Gem::Specification.find_by_name(normalized_name)
    rescue Gem::LoadError
      unless retried
        retried = true
        require "rubygems/dependency_installer"
        options = {}
        if File.writable?(Gem.dir)
          @logger.info(_("Installing gem: %s") % normalized_name)
        else
          options[:user_install] = true
          format = _("Installing gem in user install mode: %s")
          @logger.info(format % normalized_name)
        end
        installer = Gem::DependencyInstaller.new(options)
        installer.install(normalized_name, Gem::Requirement.default)
        retry
      end
    end
  end
  spec
end