# File lib/rubygems/commands/pristine_command.rb, line 52
  def execute
    gem_name = nil

    specs = if options[:all] then
              Gem::SourceIndex.from_installed_gems.map do |name, spec|
                spec
              end
            else
              gem_name = get_one_gem_name
              Gem::SourceIndex.from_installed_gems.find_name(gem_name,
                                                          options[:version])
            end

    if specs.empty? then
      raise Gem::Exception,
            "Failed to find gem #{gem_name} #{options[:version]}"
    end

    install_dir = Gem.dir # TODO use installer option

    raise Gem::FilePermissionError.new(install_dir) unless
      File.writable?(install_dir)

    say "Restoring gem(s) to pristine condition..."

    specs.each do |spec|
      gem = Dir[File.join(Gem.dir, 'cache', "#{spec.full_name}.gem")].first

      if gem.nil? then
        alert_error "Cached gem for #{spec.full_name} not found, use `gem install` to restore"
        next
      end

      # TODO use installer options
      installer = Gem::Installer.new gem, :wrappers => true, :force => true
      installer.install

      say "Restored #{spec.full_name}"
    end
  end