2018-05-18 04:39:13 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-08 04:32:18 +03:00
|
|
|
module Gem::BundlerVersionFinder
|
|
|
|
def self.bundler_version
|
|
|
|
version, _ = bundler_version_with_reason
|
|
|
|
|
|
|
|
return unless version
|
|
|
|
|
|
|
|
Gem::Version.new(version)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.bundler_version_with_reason
|
|
|
|
if v = ENV["BUNDLER_VERSION"]
|
|
|
|
return [v, "`$BUNDLER_VERSION`"]
|
|
|
|
end
|
|
|
|
if v = bundle_update_bundler_version
|
|
|
|
return if v == true
|
|
|
|
return [v, "`bundle update --bundler`"]
|
|
|
|
end
|
|
|
|
v, lockfile = lockfile_version
|
|
|
|
if v
|
|
|
|
return [v, "your #{lockfile}"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.missing_version_message
|
|
|
|
return unless vr = bundler_version_with_reason
|
|
|
|
<<-EOS
|
|
|
|
Could not find 'bundler' (#{vr.first}) required by #{vr.last}.
|
2018-11-21 13:20:47 +03:00
|
|
|
To update to the latest version installed on your system, run `bundle update --bundler`.
|
2017-10-08 04:32:18 +03:00
|
|
|
To install the missing version, run `gem install bundler:#{vr.first}`
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.compatible?(spec)
|
|
|
|
return true unless spec.name == "bundler".freeze
|
|
|
|
return true unless bundler_version = self.bundler_version
|
2018-12-12 08:07:50 +03:00
|
|
|
|
|
|
|
spec.version.segments.first == bundler_version.segments.first
|
2017-10-08 04:32:18 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.filter!(specs)
|
|
|
|
return unless bundler_version = self.bundler_version
|
2018-12-12 08:07:50 +03:00
|
|
|
|
2020-06-10 20:46:05 +03:00
|
|
|
specs.reject! {|spec| spec.version.segments.first != bundler_version.segments.first }
|
2019-01-07 14:00:35 +03:00
|
|
|
|
2020-06-10 20:46:05 +03:00
|
|
|
exact_match_index = specs.find_index {|spec| spec.version == bundler_version }
|
2019-01-10 19:08:59 +03:00
|
|
|
return unless exact_match_index
|
2019-01-07 14:00:35 +03:00
|
|
|
|
|
|
|
specs.unshift(specs.delete_at(exact_match_index))
|
2017-10-08 04:32:18 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.bundle_update_bundler_version
|
|
|
|
return unless File.basename($0) == "bundle".freeze
|
|
|
|
return unless "update".start_with?(ARGV.first || " ")
|
|
|
|
bundler_version = nil
|
|
|
|
update_index = nil
|
|
|
|
ARGV.each_with_index do |a, i|
|
|
|
|
if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN
|
|
|
|
bundler_version = a
|
|
|
|
end
|
|
|
|
next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/
|
|
|
|
bundler_version = $1 || true
|
|
|
|
update_index = i
|
|
|
|
end
|
|
|
|
bundler_version
|
|
|
|
end
|
|
|
|
private_class_method :bundle_update_bundler_version
|
|
|
|
|
|
|
|
def self.lockfile_version
|
|
|
|
return unless lockfile = lockfile_contents
|
|
|
|
lockfile, contents = lockfile
|
|
|
|
lockfile ||= "lockfile"
|
|
|
|
regexp = /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/
|
|
|
|
return unless contents =~ regexp
|
|
|
|
[$1, lockfile]
|
|
|
|
end
|
|
|
|
private_class_method :lockfile_version
|
|
|
|
|
|
|
|
def self.lockfile_contents
|
|
|
|
gemfile = ENV["BUNDLE_GEMFILE"]
|
|
|
|
gemfile = nil if gemfile && gemfile.empty?
|
|
|
|
|
2020-03-24 09:39:24 +03:00
|
|
|
unless gemfile
|
|
|
|
begin
|
|
|
|
Gem::Util.traverse_parents(Dir.pwd) do |directory|
|
2020-06-10 20:46:05 +03:00
|
|
|
next unless gemfile = Gem::GEM_DEP_FILES.find {|f| File.file?(f.tap(&Gem::UNTAINT)) }
|
2020-03-24 09:39:24 +03:00
|
|
|
|
|
|
|
gemfile = File.join directory, gemfile
|
|
|
|
break
|
|
|
|
end
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2017-10-08 04:32:18 +03:00
|
|
|
|
|
|
|
return unless gemfile
|
|
|
|
|
|
|
|
lockfile = case gemfile
|
2018-10-22 03:27:02 +03:00
|
|
|
when "gems.rb" then "gems.locked"
|
|
|
|
else "#{gemfile}.lock"
|
2019-11-11 09:03:57 +03:00
|
|
|
end.dup.tap(&Gem::UNTAINT)
|
2017-10-08 04:32:18 +03:00
|
|
|
|
|
|
|
return unless File.file?(lockfile)
|
|
|
|
|
|
|
|
[lockfile, File.read(lockfile)]
|
|
|
|
end
|
|
|
|
private_class_method :lockfile_contents
|
|
|
|
end
|