From f16c880f776450771196c35cec10b9a5860a560f Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Wed, 12 Jul 2023 16:08:32 +0900 Subject: [PATCH] [rubygems/rubygems] Introduce bundle config set version feature https://github.com/rubygems/rubygems/commit/c431a1df52 --- lib/bundler/man/bundle-config.1 | 3 +++ lib/bundler/man/bundle-config.1.ronn | 6 +++++ lib/bundler/self_manager.rb | 23 +++++++++++++++----- lib/bundler/settings.rb | 2 ++ spec/bundler/runtime/self_management_spec.rb | 19 ++++++++++++++++ 5 files changed, 48 insertions(+), 5 deletions(-) diff --git a/lib/bundler/man/bundle-config.1 b/lib/bundler/man/bundle-config.1 index 0e7046a4a2..8d5eb2de53 100644 --- a/lib/bundler/man/bundle-config.1 +++ b/lib/bundler/man/bundle-config.1 @@ -299,6 +299,9 @@ The following is a list of all configuration keys and their purpose\. You can le \fBuser_agent\fR (\fBBUNDLE_USER_AGENT\fR): The custom user agent fragment Bundler includes in API requests\. . .IP "\(bu" 4 +\fBversion\fR (\fBBUNDLE_VERSION\fR): The version of Bundler to use when running under Bundler environment\. Defaults to \fBlocal\fR\. You can also specify \fBglobal\fR or \fBx\.y\.z\fR\. \fBlocal\fR will use the Bundler version specified in the \fBGemfile\.lock\fR, \fBglobal\fR will use the system version of Bundler, and \fBx\.y\.z\fR will use the specified version of Bundler\. +. +.IP "\(bu" 4 \fBwith\fR (\fBBUNDLE_WITH\fR): A \fB:\fR\-separated list of groups whose gems bundler should install\. . .IP "\(bu" 4 diff --git a/lib/bundler/man/bundle-config.1.ronn b/lib/bundler/man/bundle-config.1.ronn index bc8b27cf89..0a3d8e3866 100644 --- a/lib/bundler/man/bundle-config.1.ronn +++ b/lib/bundler/man/bundle-config.1.ronn @@ -277,6 +277,12 @@ learn more about their operation in [bundle install(1)](bundle-install.1.html). and disallow passing no options to `bundle update`. * `user_agent` (`BUNDLE_USER_AGENT`): The custom user agent fragment Bundler includes in API requests. +* `version` (`BUNDLE_VERSION`): + The version of Bundler to use when running under Bundler environment. + Defaults to `local`. You can also specify `global` or `x.y.z`. + `local` will use the Bundler version specified in the `Gemfile.lock`, + `global` will use the system version of Bundler, and `x.y.z` will use + the specified version of Bundler. * `with` (`BUNDLE_WITH`): A `:`-separated list of groups whose gems bundler should install. * `without` (`BUNDLE_WITHOUT`): diff --git a/lib/bundler/self_manager.rb b/lib/bundler/self_manager.rb index 827f3f9222..70a5fedabd 100644 --- a/lib/bundler/self_manager.rb +++ b/lib/bundler/self_manager.rb @@ -15,11 +15,23 @@ module Bundler def install_locked_bundler_and_restart_with_it_if_needed return unless needs_switching? - Bundler.ui.info \ - "Bundler #{current_version} is running, but your lockfile was generated with #{lockfile_version}. " \ - "Installing Bundler #{lockfile_version} and restarting using that version." + begin + # BUNDLE_VERSION=x.y.z + restart_version = Gem::Version.new(Bundler.settings[:version]) - install_and_restart_with(lockfile_version) + Bundler.ui.info \ + "Bundler #{current_version} is running, but your configuration was #{restart_version}. " \ + "Installing Bundler #{restart_version} and restarting using that version." + rescue ArgumentError + # BUNDLE_VERSION=local + restart_version = lockfile_version + + Bundler.ui.info \ + "Bundler #{current_version} is running, but your lockfile was generated with #{restart_version}. " \ + "Installing Bundler #{restart_version} and restarting using that version." + end + + install_and_restart_with(restart_version) end def update_bundler_and_restart_with_it_if_needed(target) @@ -79,7 +91,8 @@ module Bundler autoswitching_applies? && released?(lockfile_version) && !running?(lockfile_version) && - !updating? + !updating? && + Bundler.settings[:version] != "global" end def autoswitching_applies? diff --git a/lib/bundler/settings.rb b/lib/bundler/settings.rb index b424bcfd52..19489e34f5 100644 --- a/lib/bundler/settings.rb +++ b/lib/bundler/settings.rb @@ -75,6 +75,7 @@ module Bundler shebang system_bindir trust-policy + version ].freeze DEFAULT_CONFIG = { @@ -84,6 +85,7 @@ module Bundler "BUNDLE_REDIRECT" => 5, "BUNDLE_RETRY" => 3, "BUNDLE_TIMEOUT" => 10, + "BUNDLE_VERSION" => "local", }.freeze def initialize(root = nil) diff --git a/spec/bundler/runtime/self_management_spec.rb b/spec/bundler/runtime/self_management_spec.rb index 700084babf..248555355c 100644 --- a/spec/bundler/runtime/self_management_spec.rb +++ b/spec/bundler/runtime/self_management_spec.rb @@ -103,6 +103,25 @@ RSpec.describe "Self management", :rubygems => ">= 3.3.0.dev", :realworld => tru expect(out).to eq(Bundler::VERSION[0] == "2" ? "Bundler version #{Bundler::VERSION}" : Bundler::VERSION) end + it "installs BUNDLE_VERSION version when using bundle config version x.y.z" do + lockfile_bundled_with(Bundler::VERSION.gsub(/\.dev/, "")) + + bundle "config set --local version #{previous_minor}" + bundle "install", :artifice => "vcr" + expect(out).to include("Bundler #{Bundler::VERSION} is running, but your configuration was #{previous_minor}. Installing Bundler #{previous_minor} and restarting using that version.") + end + + it "does not try to install when using bundle config version global" do + lockfile_bundled_with(previous_minor) + + bundle "config set version global" + bundle "install", :artifice => "vcr" + expect(out).not_to match(/restarting using that version/) + + bundle "-v" + expect(out).to eq(Bundler::VERSION[0] == "2" ? "Bundler version #{Bundler::VERSION}" : Bundler::VERSION) + end + private def lockfile_bundled_with(version)