summaryrefslogtreecommitdiff
path: root/lib/rubygems/spec_fetcher.rb
diff options
context:
space:
mode:
authorryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-01-19 00:08:49 +0000
committerryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-01-19 00:08:49 +0000
commite798ccbacf489a3af2201ae30058ff0ae7f79045 (patch)
tree4147a7834eb88323057fd2120a2ddc96c1eb32ab /lib/rubygems/spec_fetcher.rb
parentd26fb035cae8d351dc238376722c980230dc5fbd (diff)
Import rubygems 1.5.0 (release candidate)
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30599 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rubygems/spec_fetcher.rb')
-rw-r--r--lib/rubygems/spec_fetcher.rb107
1 files changed, 47 insertions, 60 deletions
diff --git a/lib/rubygems/spec_fetcher.rb b/lib/rubygems/spec_fetcher.rb
index dac35d85d0..c999a65cd9 100644
--- a/lib/rubygems/spec_fetcher.rb
+++ b/lib/rubygems/spec_fetcher.rb
@@ -1,9 +1,13 @@
-require 'zlib'
-require 'fileutils'
+######################################################################
+# This file is imported from the rubygems project.
+# DO NOT make modifications in this repo. They _will_ be reverted!
+# File a patch instead and assign it to Ryan Davis or Eric Hodel.
+######################################################################
require 'rubygems/remote_fetcher'
require 'rubygems/user_interaction'
require 'rubygems/errors'
+require 'rubygems/text'
##
# SpecFetcher handles metadata updates from remote gem repositories.
@@ -11,6 +15,13 @@ require 'rubygems/errors'
class Gem::SpecFetcher
include Gem::UserInteraction
+ include Gem::Text
+
+ FILES = {
+ :all => 'specs',
+ :latest => 'latest_specs',
+ :prerelease => 'prerelease_specs',
+ }
##
# The SpecFetcher cache dir.
@@ -43,6 +54,8 @@ class Gem::SpecFetcher
end
def initialize
+ require 'fileutils'
+
@dir = File.join Gem.user_home, '.gem', 'specs'
@update_cache = File.stat(Gem.user_home).uid == Process.uid
@@ -50,6 +63,12 @@ class Gem::SpecFetcher
@latest_specs = {}
@prerelease_specs = {}
+ @caches = {
+ :latest => @latest_specs,
+ :prerelease => @prerelease_specs,
+ :all => @specs
+ }
+
@fetcher = Gem::RemoteFetcher.fetcher
end
@@ -74,14 +93,6 @@ class Gem::SpecFetcher
end
return [ss, errors]
-
- rescue Gem::RemoteFetcher::FetchError => e
- raise unless warn_legacy e do
- require 'rubygems/source_info_cache'
-
- return [Gem::SourceInfoCache.search_with_source(dependency,
- matching_platform, all), nil]
- end
end
def fetch(*args)
@@ -160,26 +171,31 @@ class Gem::SpecFetcher
end
##
- # Returns Array of gem repositories that were generated with RubyGems less
- # than 1.2.
+ # Suggests a gem based on the supplied +gem_name+. Returns a string
+ # of the gem name if an approximate match can be found or nil
+ # otherwise. NOTE: for performance reasons only gems which exactly
+ # match the first character of +gem_name+ are considered.
- def legacy_repos
- Gem.sources.reject do |source_uri|
- source_uri = URI.parse source_uri
- spec_path = source_uri + "specs.#{Gem.marshal_version}.gz"
+ def suggest_gems_from_name gem_name
+ gem_name = gem_name.downcase
+ max = gem_name.size / 2
+ specs = list.values.flatten(1) # flatten(1) is 1.8.7 and up
- begin
- @fetcher.fetch_size spec_path
- rescue Gem::RemoteFetcher::FetchError
- begin
- @fetcher.fetch_size(source_uri + 'yaml') # re-raise if non-repo
- rescue Gem::RemoteFetcher::FetchError
- alert_error "#{source_uri} does not appear to be a repository"
- raise
- end
- false
- end
- end
+ matches = specs.map { |name, version, platform|
+ next unless Gem::Platform.match platform
+
+ distance = levenshtein_distance gem_name, name.downcase
+
+ next if distance >= max
+
+ return [name] if distance == 0
+
+ [name, distance]
+ }.compact
+
+ matches = matches.uniq.sort_by { |name, dist| dist }
+
+ matches.first(5).map { |name, dist| name }
end
##
@@ -197,15 +213,9 @@ class Gem::SpecFetcher
:latest
end
- list = {}
-
- file = { :latest => 'latest_specs',
- :prerelease => 'prerelease_specs',
- :all => 'specs' }[type]
-
- cache = { :latest => @latest_specs,
- :prerelease => @prerelease_specs,
- :all => @specs }[type]
+ list = {}
+ file = FILES[type]
+ cache = @caches[type]
Gem.sources.each do |source_uri|
source_uri = URI.parse source_uri
@@ -273,28 +283,5 @@ class Gem::SpecFetcher
specs
end
- ##
- # Warn about legacy repositories if +exception+ indicates only legacy
- # repositories are available, and yield to the block. Returns false if the
- # exception indicates some other FetchError.
-
- def warn_legacy(exception)
- uri = exception.uri.to_s
- if uri =~ /specs\.#{Regexp.escape Gem.marshal_version}\.gz$/ then
- alert_warning <<-EOF
-RubyGems 1.2+ index not found for:
-\t#{legacy_repos.join "\n\t"}
-
-RubyGems will revert to legacy indexes degrading performance.
- EOF
-
- yield
-
- return true
- end
-
- false
- end
-
end