2018-05-17 21:39:13 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
module Gem::BundlerVersionFinder
|
|
|
|
def self.bundler_version
|
2021-12-22 19:21:36 -05:00
|
|
|
v = ENV["BUNDLER_VERSION"]
|
2017-10-07 21:32:18 -04:00
|
|
|
|
2021-12-22 19:21:36 -05:00
|
|
|
v ||= bundle_update_bundler_version
|
|
|
|
return if v == true
|
2017-10-07 21:32:18 -04:00
|
|
|
|
2021-12-22 19:21:36 -05:00
|
|
|
v ||= lockfile_version
|
|
|
|
return unless v
|
2017-10-07 21:32:18 -04:00
|
|
|
|
2021-12-22 19:21:36 -05:00
|
|
|
Gem::Version.new(v)
|
2017-10-07 21:32:18 -04:00
|
|
|
end
|
|
|
|
|
2021-12-22 19:21:36 -05:00
|
|
|
def self.prioritize!(specs)
|
2020-06-10 13:46:05 -04:00
|
|
|
exact_match_index = specs.find_index {|spec| spec.version == bundler_version }
|
2019-01-10 11:08:59 -05:00
|
|
|
return unless exact_match_index
|
2019-01-07 06:00:35 -05:00
|
|
|
|
|
|
|
specs.unshift(specs.delete_at(exact_match_index))
|
2017-10-07 21:32:18 -04: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
|
2021-12-22 19:21:36 -05:00
|
|
|
return unless contents = lockfile_contents
|
2017-10-07 21:32:18 -04:00
|
|
|
regexp = /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/
|
|
|
|
return unless contents =~ regexp
|
2021-12-22 19:21:36 -05:00
|
|
|
$1
|
2017-10-07 21:32:18 -04:00
|
|
|
end
|
|
|
|
private_class_method :lockfile_version
|
|
|
|
|
|
|
|
def self.lockfile_contents
|
|
|
|
gemfile = ENV["BUNDLE_GEMFILE"]
|
|
|
|
gemfile = nil if gemfile && gemfile.empty?
|
|
|
|
|
2020-03-24 02:39:24 -04:00
|
|
|
unless gemfile
|
|
|
|
begin
|
|
|
|
Gem::Util.traverse_parents(Dir.pwd) do |directory|
|
2020-06-10 13:46:05 -04:00
|
|
|
next unless gemfile = Gem::GEM_DEP_FILES.find {|f| File.file?(f.tap(&Gem::UNTAINT)) }
|
2020-03-24 02:39:24 -04:00
|
|
|
|
|
|
|
gemfile = File.join directory, gemfile
|
|
|
|
break
|
|
|
|
end
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2017-10-07 21:32:18 -04:00
|
|
|
|
|
|
|
return unless gemfile
|
|
|
|
|
|
|
|
lockfile = case gemfile
|
2022-01-17 09:16:34 -05:00
|
|
|
when "gems.rb" then "gems.locked"
|
|
|
|
else "#{gemfile}.lock"
|
|
|
|
end.dup.tap(&Gem::UNTAINT)
|
2017-10-07 21:32:18 -04:00
|
|
|
|
|
|
|
return unless File.file?(lockfile)
|
|
|
|
|
2021-12-22 19:21:36 -05:00
|
|
|
File.read(lockfile)
|
2017-10-07 21:32:18 -04:00
|
|
|
end
|
|
|
|
private_class_method :lockfile_contents
|
|
|
|
end
|