|
1 | 1 | #!/usr/bin/env ruby |
2 | 2 | # frozen_string_literal: true |
3 | 3 |
|
| 4 | +# |
| 5 | +# This file was generated by Bundler. |
| 6 | +# |
| 7 | +# The application 'bundle' is installed as part of a gem, and |
| 8 | +# this file is here to facilitate running it. |
| 9 | +# |
| 10 | + |
4 | 11 | require "rubygems" |
5 | 12 |
|
6 | 13 | m = Module.new do |
7 | | - extend self |
| 14 | + module_function |
| 15 | + |
| 16 | + def invoked_as_script? |
| 17 | + File.expand_path($0) == File.expand_path(__FILE__) |
| 18 | + end |
| 19 | + |
| 20 | + def env_var_version |
| 21 | + ENV["BUNDLER_VERSION"] |
| 22 | + end |
| 23 | + |
| 24 | + def cli_arg_version |
| 25 | + return unless invoked_as_script? # don't want to hijack other binstubs |
| 26 | + return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update` |
| 27 | + bundler_version = nil |
| 28 | + update_index = nil |
| 29 | + ARGV.each_with_index do |a, i| |
| 30 | + if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN |
| 31 | + bundler_version = a |
| 32 | + end |
| 33 | + next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/ |
| 34 | + bundler_version = $1 |
| 35 | + update_index = i |
| 36 | + end |
| 37 | + bundler_version |
| 38 | + end |
8 | 39 |
|
9 | 40 | def gemfile |
| 41 | + gemfile = ENV["BUNDLE_GEMFILE"] |
| 42 | + return gemfile if gemfile && !gemfile.empty? |
| 43 | + |
10 | 44 | File.expand_path("../Gemfile", __dir__) |
11 | 45 | end |
12 | 46 |
|
13 | 47 | def lockfile |
14 | | - "#{gemfile}.lock" |
| 48 | + lockfile = |
| 49 | + case File.basename(gemfile) |
| 50 | + when "gems.rb" then gemfile.sub(/\.rb$/, gemfile) |
| 51 | + else "#{gemfile}.lock" |
| 52 | + end |
| 53 | + File.expand_path(lockfile) |
15 | 54 | end |
16 | 55 |
|
17 | 56 | def lockfile_version |
18 | 57 | return unless File.file?(lockfile) |
19 | | - |
20 | 58 | lockfile_contents = File.read(lockfile) |
| 59 | + return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/ |
| 60 | + Regexp.last_match(1) |
| 61 | + end |
21 | 62 |
|
22 | | - regexp = /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/ |
23 | | - |
24 | | - regexp.match(lockfile_contents)[1] |
| 63 | + def bundler_requirement |
| 64 | + @bundler_requirement ||= |
| 65 | + env_var_version || cli_arg_version || |
| 66 | + bundler_requirement_for(lockfile_version) |
25 | 67 | end |
26 | 68 |
|
27 | | - def bundler_version |
28 | | - @bundler_version ||= lockfile_version |
| 69 | + def bundler_requirement_for(version) |
| 70 | + return "#{Gem::Requirement.default}.a" unless version |
| 71 | + |
| 72 | + bundler_gem_version = Gem::Version.new(version) |
| 73 | + |
| 74 | + requirement = bundler_gem_version.approximate_recommendation |
| 75 | + |
| 76 | + return requirement unless Gem.rubygems_version < Gem::Version.new("2.7.0") |
| 77 | + |
| 78 | + requirement += ".a" if bundler_gem_version.prerelease? |
| 79 | + |
| 80 | + requirement |
29 | 81 | end |
30 | 82 |
|
31 | 83 | def load_bundler! |
32 | 84 | ENV["BUNDLE_GEMFILE"] ||= gemfile |
33 | 85 |
|
34 | | - activate_bundler(bundler_version) |
| 86 | + activate_bundler |
| 87 | + end |
| 88 | + |
| 89 | + def activate_bundler |
| 90 | + gem_error = activation_error_handling do |
| 91 | + gem "bundler", bundler_requirement |
| 92 | + end |
| 93 | + return if gem_error.nil? |
| 94 | + require_error = activation_error_handling do |
| 95 | + require "bundler/version" |
| 96 | + end |
| 97 | + return if require_error.nil? && Gem::Requirement.new(bundler_requirement).satisfied_by?(Gem::Version.new(Bundler::VERSION)) |
| 98 | + warn "Activating bundler (#{bundler_requirement}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_requirement}'`" |
| 99 | + exit 42 |
35 | 100 | end |
36 | 101 |
|
37 | | - def activate_bundler(bundler_version) |
38 | | - gem "bundler", bundler_version |
| 102 | + def activation_error_handling |
| 103 | + yield |
| 104 | + nil |
| 105 | + rescue StandardError, LoadError => e |
| 106 | + e |
39 | 107 | end |
40 | 108 | end |
41 | 109 |
|
42 | 110 | m.load_bundler! |
| 111 | + |
| 112 | +if m.invoked_as_script? |
| 113 | + load Gem.bin_path("bundler", "bundle") |
| 114 | +end |
0 commit comments