class Gem::Source
ASource knows how to list and fetch gems from a RubyGems marshal index.
There are otherSource subclasses for installed gems, local gems, the bundler dependency API and so-forth.
Attributes
Public Class Methods
Public Instance Methods
Source
# File lib/rubygems/source.rb, line 37def<=>(other)caseotherwhenGem::Source::Installed,Gem::Source::Local,Gem::Source::Lock,Gem::Source::SpecificFile,Gem::Source::Git,Gem::Source::Vendorthen-1whenGem::Sourcethenunless@urireturn0unlessother.urireturn1endreturn-1unlessother.uri# Returning 1 here ensures that when sorting a list of sources, the# original ordering of sources supplied by the user is preserved.return1unless@uri.to_s==other.uri.to_s0endend
Sources are ordered by installation preference.
Source
# File lib/rubygems/source.rb, line 84defcache_dir(uri)# Correct for windows pathsescaped_path =uri.path.sub(%r{^/([a-z]):/}i,'/\\1-/')File.joinGem.spec_cache_dir,"#{uri.host}%#{uri.port}",File.dirname(escaped_path)end
Returns the local directory to writeuri to.
Source
# File lib/rubygems/source.rb, line 73defdependency_resolver_set(prerelease =false)new_dependency_resolver_set.tap {|set|set.prerelease =prerelease }end
Returns a Set that can fetch specifications from this source.
The set will optionally fetch prereleases if requested.
Source
# File lib/rubygems/source.rb, line 193defdownload(spec,dir =Dir.pwd)fetcher =Gem::RemoteFetcher.fetcherfetcher.downloadspec,uri.to_s,dirend
Downloadsspec and writes it todir. See alsoGem::RemoteFetcher#download.
Source
# File lib/rubygems/source.rb, line 107deffetch_spec(name_tuple)fetcher =Gem::RemoteFetcher.fetcherspec_file_name =name_tuple.spec_namesource_uri =enforce_trailing_slash(uri)+"#{Gem::MARSHAL_SPEC_DIR}#{spec_file_name}"cache_dir =cache_dirsource_urilocal_spec =File.joincache_dir,spec_file_nameifFile.exist?local_specspec =Gem.read_binarylocal_specGem.load_safe_marshalspec =beginGem::SafeMarshal.safe_load(spec)rescueStandardErrornilendreturnspecifspecendsource_uri.path<<".rz"spec =fetcher.fetch_pathsource_urispec =Gem::Util.inflatespecifupdate_cache?require"fileutils"FileUtils.mkdir_pcache_dirFile.openlocal_spec,"wb"do|io|io.writespecendendGem.load_safe_marshal# TODO: Investigate setting Gem::Specification#loaded_from to a URIGem::SafeMarshal.safe_loadspecend
Fetches a specification for the givenname_tuple.
Source
# File lib/rubygems/source.rb, line 159defload_specs(type)file =FILES[type]fetcher =Gem::RemoteFetcher.fetcherfile_name ="#{file}.#{Gem.marshal_version}"spec_path =enforce_trailing_slash(uri)+"#{file_name}.gz"cache_dir =cache_dirspec_pathlocal_file =File.join(cache_dir,file_name)retried =falseifupdate_cache?require"fileutils"FileUtils.mkdir_pcache_direndspec_dump =fetcher.cache_update_pathspec_path,local_file,update_cache?Gem.load_safe_marshalbeginGem::NameTuple.from_listGem::SafeMarshal.safe_load(spec_dump)rescueArgumentErrorifupdate_cache?&&!retriedFileUtils.rmlocal_fileretried =trueretryelseraiseGem::Exception.new("Invalid spec cache file in #{local_file}")endendend
Loadstype kind of specs fetching from +@uri+ if the on-disk cache is out of date.
type is one of the following:
:released => Return the list of all released specs :latest => Return the list of only the highest version of each gem :prerelease => Return the list of all prerelease only specs
Source
# File lib/rubygems/source.rb, line 213deftypo_squatting?(host,distance_threshold =4)returnif@uri.host.nil?levenshtein_distance(@uri.host,host).between?1,distance_thresholdend
Source
# File lib/rubygems/source.rb, line 94defupdate_cache?return@update_cacheunless@update_cache.nil?@update_cache =beginFile.stat(Gem.user_home).uid==Process.uidrescueErrno::ENOENTfalseendend
Returns true when it is possible and safe to update the cache directory.
Private Instance Methods
Source
# File lib/rubygems/source.rb, line 243defenforce_trailing_slash(uri)uri.merge(uri.path.gsub(%r{/+$},"")+"/")end
Source
# File lib/rubygems/source.rb, line 220defnew_dependency_resolver_setreturnGem::Resolver::IndexSet.newselfifuri.scheme=="file"fetch_uri =ifuri.host=="rubygems.org"index_uri =uri.dupindex_uri.host ="index.rubygems.org"index_urielseuriendbundler_api_uri =enforce_trailing_slash(fetch_uri)+"versions"beginfetcher =Gem::RemoteFetcher.fetcherresponse =fetcher.fetch_pathbundler_api_uri,nil,truerescueGem::RemoteFetcher::FetchErrorGem::Resolver::IndexSet.newselfelseGem::Resolver::APISet.newresponse.uri+"./info/"endend