diff options
Diffstat (limited to 'lib/rubygems/source')
| -rw-r--r-- | lib/rubygems/source/git.rb | 244 | ||||
| -rw-r--r-- | lib/rubygems/source/installed.rb | 39 | ||||
| -rw-r--r-- | lib/rubygems/source/local.rb | 135 | ||||
| -rw-r--r-- | lib/rubygems/source/lock.rb | 49 | ||||
| -rw-r--r-- | lib/rubygems/source/specific_file.rb | 73 | ||||
| -rw-r--r-- | lib/rubygems/source/vendor.rb | 24 |
6 files changed, 564 insertions, 0 deletions
diff --git a/lib/rubygems/source/git.rb b/lib/rubygems/source/git.rb new file mode 100644 index 0000000000..baf2f9dd4c --- /dev/null +++ b/lib/rubygems/source/git.rb @@ -0,0 +1,244 @@ +# frozen_string_literal: true + +## +# A git gem for use in a gem dependencies file. +# +# Example: +# +# source = +# Gem::Source::Git.new 'rake', 'git@example:rake.git', 'rake-10.1.0', false +# +# source.specs + +class Gem::Source::Git < Gem::Source + ## + # The name of the gem created by this git gem. + + attr_reader :name + + ## + # The commit reference used for checking out this git gem. + + attr_reader :reference + + ## + # When false the cache for this repository will not be updated. + + attr_accessor :remote + + ## + # The git repository this gem is sourced from. + + attr_reader :repository + + ## + # The directory for cache and git gem installation + + attr_accessor :root_dir + + ## + # Does this repository need submodules checked out too? + + attr_reader :need_submodules + + ## + # Creates a new git gem source for a gems from loaded from +repository+ at + # the given +reference+. The +name+ is only used to track the repository + # back to a gem dependencies file, it has no real significance as a git + # repository may contain multiple gems. If +submodules+ is true, submodules + # will be checked out when the gem is installed. + + def initialize(name, repository, reference, submodules = false) + require_relative "../uri" + @uri = Gem::Uri.parse(repository) + @name = name + @repository = repository + @reference = reference || "HEAD" + @need_submodules = submodules + + @remote = true + @root_dir = Gem.dir + end + + def <=>(other) + case other + when Gem::Source::Git then + 0 + when Gem::Source::Vendor, + Gem::Source::Lock then + -1 + when Gem::Source then + 1 + end + end + + def ==(other) # :nodoc: + super && + @name == other.name && + @repository == other.repository && + @reference == other.reference && + @need_submodules == other.need_submodules + end + + def git_command + ENV.fetch("git", "git") + end + + ## + # Checks out the files for the repository into the install_dir. + + def checkout # :nodoc: + cache + + return false unless File.exist? repo_cache_dir + + unless File.exist? install_dir + system git_command, "clone", "--quiet", "--no-checkout", + repo_cache_dir, install_dir + end + + Dir.chdir install_dir do + system git_command, "fetch", "--quiet", "--force", "--tags", install_dir + + success = system git_command, "reset", "--quiet", "--hard", rev_parse + + if @need_submodules + require "open3" + _, status = Open3.capture2e(git_command, "submodule", "update", "--quiet", "--init", "--recursive") + + success &&= status.success? + end + + success + end + end + + ## + # Creates a local cache repository for the git gem. + + def cache # :nodoc: + return unless @remote + + if File.exist? repo_cache_dir + Dir.chdir repo_cache_dir do + system git_command, "fetch", "--quiet", "--force", "--tags", + @repository, "refs/heads/*:refs/heads/*" + end + else + system git_command, "clone", "--quiet", "--bare", "--no-hardlinks", + @repository, repo_cache_dir + end + end + + ## + # Directory where git gems get unpacked and so-forth. + + def base_dir # :nodoc: + File.join @root_dir, "bundler" + end + + ## + # A short reference for use in git gem directories + + def dir_shortref # :nodoc: + rev_parse[0..11] + end + + ## + # Nothing to download for git gems + + def download(full_spec, path) # :nodoc: + end + + ## + # The directory where the git gem will be installed. + + def install_dir # :nodoc: + return unless File.exist? repo_cache_dir + + File.join base_dir, "gems", "#{@name}-#{dir_shortref}" + end + + def pretty_print(q) # :nodoc: + q.object_group(self) do + q.group 2, "[Git: ", "]" do + q.breakable + q.text @repository + + q.breakable + q.text @reference + end + end + end + + ## + # The directory where the git gem's repository will be cached. + + def repo_cache_dir # :nodoc: + File.join @root_dir, "cache", "bundler", "git", "#{@name}-#{uri_hash}" + end + + ## + # Converts the git reference for the repository into a commit hash. + + def rev_parse # :nodoc: + hash = nil + + Dir.chdir repo_cache_dir do + hash = Gem::Util.popen(git_command, "rev-parse", @reference).strip + end + + raise Gem::Exception, + "unable to find reference #{@reference} in #{@repository}" unless + $?.success? + + hash + end + + ## + # Loads all gemspecs in the repository + + def specs + checkout + + return [] unless install_dir + + Dir.chdir install_dir do + Dir["{,*,*/*}.gemspec"].filter_map do |spec_file| + directory = File.dirname spec_file + file = File.basename spec_file + + Dir.chdir directory do + spec = Gem::Specification.load file + if spec + spec.base_dir = base_dir + + spec.extension_dir = + File.join base_dir, "extensions", Gem::Platform.local.to_s, + Gem.extension_api_version, "#{name}-#{dir_shortref}" + + spec.full_gem_path = File.dirname spec.loaded_from if spec + end + spec + end + end + end + end + + ## + # A hash for the git gem based on the git repository Gem::URI. + + def uri_hash # :nodoc: + require_relative "../openssl" + + normalized = + if @repository.match?(%r{^\w+://(\w+@)?}) + uri = Gem::URI(@repository).normalize.to_s.sub %r{/$},"" + uri.sub(/\A(\w+)/) { $1.downcase } + else + @repository + end + + OpenSSL::Digest::SHA1.hexdigest normalized + end +end diff --git a/lib/rubygems/source/installed.rb b/lib/rubygems/source/installed.rb new file mode 100644 index 0000000000..f5c96fee51 --- /dev/null +++ b/lib/rubygems/source/installed.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +## +# Represents an installed gem. This is used for dependency resolution. + +class Gem::Source::Installed < Gem::Source + def initialize # :nodoc: + @uri = nil + end + + ## + # Installed sources sort before all other sources + + def <=>(other) + case other + when Gem::Source::Git, + Gem::Source::Lock, + Gem::Source::Vendor then + -1 + when Gem::Source::Installed then + 0 + when Gem::Source then + 1 + end + end + + ## + # We don't need to download an installed gem + + def download(spec, path) + nil + end + + def pretty_print(q) # :nodoc: + q.object_group(self) do + q.text "[Installed]" + end + end +end diff --git a/lib/rubygems/source/local.rb b/lib/rubygems/source/local.rb new file mode 100644 index 0000000000..4bef31a265 --- /dev/null +++ b/lib/rubygems/source/local.rb @@ -0,0 +1,135 @@ +# frozen_string_literal: true + +## +# The local source finds gems in the current directory for fulfilling +# dependencies. + +class Gem::Source::Local < Gem::Source + def initialize # :nodoc: + @specs = nil + @api_uri = nil + @uri = nil + @load_specs_names = {} + end + + ## + # Local sorts before Gem::Source and after Gem::Source::Installed + + def <=>(other) + case other + when Gem::Source::Installed, + Gem::Source::Lock then + -1 + when Gem::Source::Local then + 0 + when Gem::Source then + 1 + end + end + + def inspect # :nodoc: + keys = @specs ? @specs.keys.sort : "NOT LOADED" + format("#<%s specs: %p>", self.class, keys) + end + + def load_specs(type) # :nodoc: + @load_specs_names[type] ||= begin + names = [] + + @specs = {} + + Dir["*.gem"].each do |file| + pkg = Gem::Package.new(file) + spec = pkg.spec + rescue SystemCallError, Gem::Package::FormatError + # ignore + else + tup = spec.name_tuple + @specs[tup] = [File.expand_path(file), pkg] + + case type + when :released + unless pkg.spec.version.prerelease? + names << pkg.spec.name_tuple + end + when :prerelease + if pkg.spec.version.prerelease? + names << pkg.spec.name_tuple + end + when :latest + tup = pkg.spec.name_tuple + + cur = names.find {|x| x.name == tup.name } + if !cur + names << tup + elsif cur.version < tup.version + names.delete cur + names << tup + end + else + names << pkg.spec.name_tuple + end + end + + names + end + end + + def find_gem(gem_name, version = Gem::Requirement.default, prerelease = false) # :nodoc: + find_all_gems(gem_name, version, prerelease).max_by(&:version) + end + + def find_all_gems(gem_name, version = Gem::Requirement.default, prerelease = false) # :nodoc: + load_specs :complete + + found = [] + + @specs.each do |n, data| + next unless n.name == gem_name + s = data[1].spec + + if version.satisfied_by?(s.version) + if prerelease + found << s + elsif !s.version.prerelease? || version.prerelease? + found << s + end + end + end + + found + end + + def fetch_spec(name) # :nodoc: + load_specs :complete + + if data = @specs[name] + data.last.spec + else + raise Gem::Exception, "Unable to find spec for #{name.inspect}" + end + end + + def download(spec, cache_dir = nil) # :nodoc: + load_specs :complete + + @specs.each do |_name, data| + return data[0] if data[1].spec == spec + end + + raise Gem::Exception, "Unable to find file for '#{spec.full_name}'" + end + + def pretty_print(q) # :nodoc: + q.object_group(self) do + q.group 2, "[Local gems:", "]" do + q.breakable + if @specs + q.seplist @specs.keys do |v| + q.text v.full_name + end + end + end + end + end +end diff --git a/lib/rubygems/source/lock.rb b/lib/rubygems/source/lock.rb new file mode 100644 index 0000000000..70849210bd --- /dev/null +++ b/lib/rubygems/source/lock.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +## +# A Lock source wraps an installed gem's source and sorts before other sources +# during dependency resolution. This allows RubyGems to prefer gems from +# dependency lock files. + +class Gem::Source::Lock < Gem::Source + ## + # The wrapped Gem::Source + + attr_reader :wrapped + + ## + # Creates a new Lock source that wraps +source+ and moves it earlier in the + # sort list. + + def initialize(source) + @wrapped = source + end + + def <=>(other) # :nodoc: + case other + when Gem::Source::Lock then + @wrapped <=> other.wrapped + when Gem::Source then + 1 + end + end + + def ==(other) # :nodoc: + (self <=> other) == 0 + end + + def hash # :nodoc: + @wrapped.hash ^ 3 + end + + ## + # Delegates to the wrapped source's fetch_spec method. + + def fetch_spec(name_tuple) + @wrapped.fetch_spec name_tuple + end + + def uri # :nodoc: + @wrapped.uri + end +end diff --git a/lib/rubygems/source/specific_file.rb b/lib/rubygems/source/specific_file.rb new file mode 100644 index 0000000000..dde1d48a21 --- /dev/null +++ b/lib/rubygems/source/specific_file.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +## +# A source representing a single .gem file. This is used for installation of +# local gems. + +class Gem::Source::SpecificFile < Gem::Source + ## + # The path to the gem for this specific file. + + attr_reader :path + + ## + # Creates a new SpecificFile for the gem in +file+ + + def initialize(file) + @uri = nil + @path = ::File.expand_path(file) + + @package = Gem::Package.new @path + @spec = @package.spec + @name = @spec.name_tuple + end + + ## + # The Gem::Specification extracted from this .gem. + + attr_reader :spec + + def load_specs(*a) # :nodoc: + [@name] + end + + def fetch_spec(name) # :nodoc: + return @spec if name == @name + raise Gem::Exception, "Unable to find '#{name}'" + end + + def download(spec, dir = nil) # :nodoc: + return @path if spec == @spec + raise Gem::Exception, "Unable to download '#{spec.full_name}'" + end + + def pretty_print(q) # :nodoc: + q.object_group(self) do + q.group 2, "[SpecificFile:", "]" do + q.breakable + q.text @path + end + end + end + + ## + # Orders this source against +other+. + # + # If +other+ is a SpecificFile from a different gem name +nil+ is returned. + # + # If +other+ is a SpecificFile from the same gem name the versions are + # compared using Gem::Version#<=> + # + # Otherwise Gem::Source#<=> is used. + + def <=>(other) + case other + when Gem::Source::SpecificFile then + return nil if @spec.name != other.spec.name + + @spec.version <=> other.spec.version + else + super + end + end +end diff --git a/lib/rubygems/source/vendor.rb b/lib/rubygems/source/vendor.rb new file mode 100644 index 0000000000..44ef614441 --- /dev/null +++ b/lib/rubygems/source/vendor.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +## +# This represents a vendored source that is similar to an installed gem. + +class Gem::Source::Vendor < Gem::Source::Installed + ## + # Creates a new Vendor source for a gem that was unpacked at +path+. + + def initialize(path) + @uri = path + end + + def <=>(other) + case other + when Gem::Source::Lock then + -1 + when Gem::Source::Vendor then + 0 + when Gem::Source then + 1 + end + end +end |
