diff options
author | Samuel Giddins <segiddins@segiddins.me> | 2017-07-19 18:58:09 -0500 |
---|---|---|
committer | Samuel Giddins <segiddins@segiddins.me> | 2017-07-21 09:33:00 -0500 |
commit | 01fe509b27b317dd4d640beb9140ab31331ea349 (patch) | |
tree | e3884864071b2b35e3a70693e9a25eb7c3184751 /lib/bundler/templates | |
parent | 365b4301baedba9b5cf4f1e47a42f65ed028c51b (diff) | |
download | bundler-01fe509b27b317dd4d640beb9140ab31331ea349.tar.gz |
Add a special bundler binstub that ensures the correct version is activated
Diffstat (limited to 'lib/bundler/templates')
-rwxr-xr-x | lib/bundler/templates/Executable | 3 | ||||
-rw-r--r-- | lib/bundler/templates/Executable.bundler | 71 |
2 files changed, 74 insertions, 0 deletions
diff --git a/lib/bundler/templates/Executable b/lib/bundler/templates/Executable index 86e73fbbc3..9289debc26 100755 --- a/lib/bundler/templates/Executable +++ b/lib/bundler/templates/Executable @@ -8,6 +8,9 @@ # this file is here to facilitate running it. # +bundle_binstub = File.expand_path("../bundle", __FILE__) +load(bundle_binstub) if File.file?(bundle_binstub) + require "pathname" ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../<%= relative_gemfile_path %>", Pathname.new(__FILE__).realpath) diff --git a/lib/bundler/templates/Executable.bundler b/lib/bundler/templates/Executable.bundler new file mode 100644 index 0000000000..351eca7427 --- /dev/null +++ b/lib/bundler/templates/Executable.bundler @@ -0,0 +1,71 @@ +#!/usr/bin/env <%= Bundler.settings[:shebang] || RbConfig::CONFIG["ruby_install_name"] %> +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application '<%= executable %>' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +require "rubygems" + +m = Module.new do + module_function + + def env_var_version + ENV['BUNDLER_VERSION'] + end + + def cli_arg_version + update = "update".start_with?(ARGV.first || " ") && ARGV.find {|a| a.start_with?("--bundler") } + update &&= update =~ /--bundler(?:=(.+))?/ && $1 || "> 0.a" + end + + def gemfile + gemfile = ENV['BUNDLE_GEMFILE'] + return gemfile if gemfile && !gemfile.empty? + + File.expand_path("../<%= relative_gemfile_path %>", __FILE__) + end + + def lockfile + File.expand_path case File.basename(gemfile) + when 'gems.rb' then gemfile.sub(/\.rb$/, gemfile) + else "#{gemfile}.lock" + end + end + + def lockfile_version + return unless File.file?(lockfile) + lockfile_contents = File.read(lockfile) + return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/ + Regexp.last_match(1) + end + + def bundler_version + @bundler_version ||= begin + env_var_version || cli_arg_version || + lockfile_version || "#{Gem::Requirement.default}.a" + end + end + + def load_bundler! + ENV["BUNDLE_GEMFILE"] ||= gemfile + + activate_bundler(bundler_version) + end + + def activate_bundler(bundler_version) + gem "bundler", bundler_version + rescue StandardError, LoadError => e + warn "Activating bundler (#{bundler_version}) failed:\n#{e.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_version}'`" + exit 42 + end +end + +m.load_bundler! + +if File.expand_path($0) == File.expand_path(__FILE__) + load Gem.bin_path("<%= spec.name %>", "<%= executable %>") +end |