Skip to content

Commit e712345

Browse files
authored
Merge pull request #6817 from rubygems/allow-settings-prelease-bundler
Introduce bundle config set version feature
2 parents 8e10600 + 46cd9be commit e712345

5 files changed

Lines changed: 60 additions & 7 deletions

File tree

bundler/lib/bundler/man/bundle-config.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ The following is a list of all configuration keys and their purpose\. You can le
296296
\fBuser_agent\fR (\fBBUNDLE_USER_AGENT\fR): The custom user agent fragment Bundler includes in API requests\.
297297
.
298298
.IP "\(bu" 4
299+
\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\.
300+
.
301+
.IP "\(bu" 4
299302
\fBwith\fR (\fBBUNDLE_WITH\fR): A \fB:\fR\-separated list of groups whose gems bundler should install\.
300303
.
301304
.IP "\(bu" 4

bundler/lib/bundler/man/bundle-config.1.ronn

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ learn more about their operation in [bundle install(1)](bundle-install.1.html).
274274
and disallow passing no options to `bundle update`.
275275
* `user_agent` (`BUNDLE_USER_AGENT`):
276276
The custom user agent fragment Bundler includes in API requests.
277+
* `version` (`BUNDLE_VERSION`):
278+
The version of Bundler to use when running under Bundler environment.
279+
Defaults to `local`. You can also specify `global` or `x.y.z`.
280+
`local` will use the Bundler version specified in the `Gemfile.lock`,
281+
`global` will use the system version of Bundler, and `x.y.z` will use
282+
the specified version of Bundler.
277283
* `with` (`BUNDLE_WITH`):
278284
A `:`-separated list of groups whose gems bundler should install.
279285
* `without` (`BUNDLE_WITHOUT`):

bundler/lib/bundler/self_manager.rb

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,23 @@ class SelfManager
99
def restart_with_locked_bundler_if_needed
1010
return unless needs_switching? && installed?
1111

12-
restart_with(lockfile_version)
12+
restart_with(restart_version)
1313
end
1414

1515
def install_locked_bundler_and_restart_with_it_if_needed
1616
return unless needs_switching?
1717

18-
Bundler.ui.info \
19-
"Bundler #{current_version} is running, but your lockfile was generated with #{lockfile_version}. " \
20-
"Installing Bundler #{lockfile_version} and restarting using that version."
18+
if restart_version == lockfile_version
19+
Bundler.ui.info \
20+
"Bundler #{current_version} is running, but your lockfile was generated with #{lockfile_version}. " \
21+
"Installing Bundler #{lockfile_version} and restarting using that version."
22+
else
23+
Bundler.ui.info \
24+
"Bundler #{current_version} is running, but your configuration was #{restart_version}. " \
25+
"Installing Bundler #{restart_version} and restarting using that version."
26+
end
2127

22-
install_and_restart_with(lockfile_version)
28+
install_and_restart_with(restart_version)
2329
end
2430

2531
def update_bundler_and_restart_with_it_if_needed(target)
@@ -79,7 +85,8 @@ def needs_switching?
7985
autoswitching_applies? &&
8086
released?(lockfile_version) &&
8187
!running?(lockfile_version) &&
82-
!updating?
88+
!updating? &&
89+
Bundler.settings[:version] != "global"
8390
end
8491

8592
def autoswitching_applies?
@@ -151,7 +158,7 @@ def updating?
151158
def installed?
152159
Bundler.configure
153160

154-
Bundler.rubygems.find_bundler(lockfile_version.to_s)
161+
Bundler.rubygems.find_bundler(restart_version.to_s)
155162
end
156163

157164
def current_version
@@ -164,5 +171,14 @@ def lockfile_version
164171
parsed_version = Bundler::LockfileParser.bundled_with
165172
@lockfile_version = parsed_version ? Gem::Version.new(parsed_version) : nil
166173
end
174+
175+
def restart_version
176+
return @restart_version if defined?(@restart_version)
177+
# BUNDLE_VERSION=x.y.z
178+
@restart_version = Gem::Version.new(Bundler.settings[:version])
179+
rescue ArgumentError
180+
# BUNDLE_VERSION=local
181+
@restart_version = lockfile_version
182+
end
167183
end
168184
end

bundler/lib/bundler/settings.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class Settings
7474
shebang
7575
system_bindir
7676
trust-policy
77+
version
7778
].freeze
7879

7980
DEFAULT_CONFIG = {
@@ -83,6 +84,7 @@ class Settings
8384
"BUNDLE_REDIRECT" => 5,
8485
"BUNDLE_RETRY" => 3,
8586
"BUNDLE_TIMEOUT" => 10,
87+
"BUNDLE_VERSION" => "local",
8688
}.freeze
8789

8890
def initialize(root = nil)

bundler/spec/runtime/self_management_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
"2.3.0"
77
end
88

9+
let(:current_version) do
10+
"2.4.0"
11+
end
12+
913
before do
1014
build_repo2
1115

@@ -103,6 +107,28 @@
103107
expect(out).to eq(Bundler::VERSION[0] == "2" ? "Bundler version #{Bundler::VERSION}" : Bundler::VERSION)
104108
end
105109

110+
it "installs BUNDLE_VERSION version when using bundle config version x.y.z" do
111+
lockfile_bundled_with(current_version)
112+
113+
bundle "config set --local version #{previous_minor}"
114+
bundle "install", :artifice => "vcr"
115+
expect(out).to include("Bundler #{Bundler::VERSION} is running, but your configuration was #{previous_minor}. Installing Bundler #{previous_minor} and restarting using that version.")
116+
117+
bundle "-v"
118+
expect(out).to eq(previous_minor[0] == "2" ? "Bundler version #{previous_minor}" : previous_minor)
119+
end
120+
121+
it "does not try to install when using bundle config version global" do
122+
lockfile_bundled_with(previous_minor)
123+
124+
bundle "config set version global"
125+
bundle "install", :artifice => "vcr"
126+
expect(out).not_to match(/restarting using that version/)
127+
128+
bundle "-v"
129+
expect(out).to eq(Bundler::VERSION[0] == "2" ? "Bundler version #{Bundler::VERSION}" : Bundler::VERSION)
130+
end
131+
106132
private
107133

108134
def lockfile_bundled_with(version)

0 commit comments

Comments
 (0)