bundle 2.7 KB

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