|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 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 | + |
| 11 | +require "rubygems" |
| 12 | + |
| 13 | +m = Module.new do |
| 14 | + module_function |
| 15 | + |
| 16 | + def invoked_as_script? |
| 17 | + File.expand_path($PROGRAM_NAME) == File.expand_path(__FILE__) |
| 18 | + end |
| 19 | + |
| 20 | + def env_var_version |
| 21 | + ENV.fetch("BUNDLER_VERSION", nil) |
| 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 | + |
| 28 | + bundler_version = nil |
| 29 | + update_index = nil |
| 30 | + ARGV.each_with_index do |a, i| |
| 31 | + if update_index && update_index.succ == i && |
| 32 | + a =~ Gem::Version::ANCHORED_VERSION_PATTERN |
| 33 | + bundler_version = a |
| 34 | + end |
| 35 | + next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/o |
| 36 | + |
| 37 | + bundler_version = Regexp.last_match(1) |
| 38 | + update_index = i |
| 39 | + end |
| 40 | + bundler_version |
| 41 | + end |
| 42 | + |
| 43 | + def gemfile |
| 44 | + gemfile = ENV.fetch("BUNDLE_GEMFILE", nil) |
| 45 | + return gemfile if gemfile && !gemfile.empty? |
| 46 | + |
| 47 | + File.expand_path("../Gemfile", __dir__) |
| 48 | + end |
| 49 | + |
| 50 | + def lockfile |
| 51 | + lockfile = |
| 52 | + case File.basename(gemfile) |
| 53 | + when "gems.rb" then gemfile.sub(/\.rb$/, ".locked") |
| 54 | + else "#{gemfile}.lock" |
| 55 | + end |
| 56 | + File.expand_path(lockfile) |
| 57 | + end |
| 58 | + |
| 59 | + def lockfile_version |
| 60 | + return unless File.file?(lockfile) |
| 61 | + |
| 62 | + lockfile_contents = File.read(lockfile) |
| 63 | + unless lockfile_contents =~ \ |
| 64 | + /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/o |
| 65 | + return |
| 66 | + end |
| 67 | + |
| 68 | + Regexp.last_match(1) |
| 69 | + end |
| 70 | + |
| 71 | + def bundler_requirement |
| 72 | + @bundler_requirement ||= |
| 73 | + env_var_version || |
| 74 | + cli_arg_version || |
| 75 | + bundler_requirement_for(lockfile_version) |
| 76 | + end |
| 77 | + |
| 78 | + def bundler_requirement_for(version) |
| 79 | + return "#{Gem::Requirement.default}.a" unless version |
| 80 | + |
| 81 | + bundler_gem_version = Gem::Version.new(version) |
| 82 | + |
| 83 | + bundler_gem_version.approximate_recommendation |
| 84 | + end |
| 85 | + |
| 86 | + def load_bundler! |
| 87 | + ENV["BUNDLE_GEMFILE"] ||= gemfile |
| 88 | + |
| 89 | + activate_bundler |
| 90 | + end |
| 91 | + |
| 92 | + def activate_bundler |
| 93 | + gem_error = activation_error_handling do |
| 94 | + gem "bundler", bundler_requirement |
| 95 | + end |
| 96 | + return if gem_error.nil? |
| 97 | + |
| 98 | + require_error = activation_error_handling do |
| 99 | + require "bundler/version" |
| 100 | + end |
| 101 | + if require_error.nil? && |
| 102 | + Gem::Requirement.new(bundler_requirement) |
| 103 | + .satisfied_by?(Gem::Version.new(Bundler::VERSION)) |
| 104 | + return |
| 105 | + end |
| 106 | + |
| 107 | + warn "Activating bundler (#{bundler_requirement}) failed:\n#{gem_error.message}\n\n" \ |
| 108 | + "To install the version of bundler this project requires," \ |
| 109 | + " run `gem install bundler -v '#{bundler_requirement}'`" |
| 110 | + exit 42 |
| 111 | + end |
| 112 | + |
| 113 | + def activation_error_handling |
| 114 | + yield |
| 115 | + nil |
| 116 | + rescue StandardError, LoadError => e |
| 117 | + e |
| 118 | + end |
| 119 | +end |
| 120 | + |
| 121 | +m.load_bundler! |
| 122 | + |
| 123 | +load Gem.bin_path("bundler", "bundle") if m.invoked_as_script? |
0 commit comments