Skip to content

Commit 17acdd4

Browse files
committed
Support bundle install --lockfile option
This allows for specifying the lockfile to read and write. It mirrors the --gemfile option, and has higher priority than the lockfile method in the Gemfile. It also mirrors the bundle lock --lockfile option. When the --lockfile option is used, it is applied twice. First, before the Gemfile is read, to specify the lockfile to operate on, and again after the Gemfile is read, so that if the Gemfile has a lockfile method that overrides the defintion's lockfile, the --lockfile option still has higher precedence.
1 parent 2494239 commit 17acdd4

File tree

7 files changed

+56
-8
lines changed

7 files changed

+56
-8
lines changed

bundler/lib/bundler/cli.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,29 @@ def self.aliases_for(command_name)
5959
def initialize(*args)
6060
super
6161

62+
current_cmd = args.last[:current_command].name
63+
6264
custom_gemfile = options[:gemfile] || Bundler.settings[:gemfile]
6365
if custom_gemfile && !custom_gemfile.empty?
6466
Bundler::SharedHelpers.set_env "BUNDLE_GEMFILE", File.expand_path(custom_gemfile)
65-
Bundler.reset_settings_and_root!
67+
reset_settings = true
6668
end
6769

70+
# lock --lockfile works differently than install --lockfile
71+
unless current_cmd == "lock"
72+
custom_lockfile = options[:lockfile] || Bundler.settings[:lockfile]
73+
if custom_lockfile && !custom_lockfile.empty?
74+
Bundler::SharedHelpers.set_env "BUNDLE_LOCKFILE", File.expand_path(custom_lockfile)
75+
reset_settings = true
76+
end
77+
end
78+
79+
Bundler.reset_settings_and_root! if reset_settings
80+
6881
Bundler.auto_switch
6982

7083
Bundler.settings.set_command_option_if_given :retry, options[:retry]
7184

72-
current_cmd = args.last[:current_command].name
7385
Bundler.auto_install if AUTO_INSTALL_CMDS.include?(current_cmd)
7486
rescue UnknownArgumentError => e
7587
raise InvalidOption, e.message
@@ -232,6 +244,7 @@ def remove(*gems)
232244
method_option "gemfile", type: :string, banner: "Use the specified gemfile instead of Gemfile"
233245
method_option "jobs", aliases: "-j", type: :numeric, banner: "Specify the number of jobs to run in parallel"
234246
method_option "local", type: :boolean, banner: "Do not attempt to fetch gems remotely and use the gem cache instead"
247+
method_option "lockfile", type: :string, banner: "Use the specified lockfile instead of the default."
235248
method_option "prefer-local", type: :boolean, banner: "Only attempt to fetch gems remotely if not present locally, even if newer versions are available remotely"
236249
method_option "no-cache", type: :boolean, banner: "Don't update the existing gem cache."
237250
method_option "no-lock", type: :boolean, banner: "Don't create a lockfile."

bundler/lib/bundler/cli/install.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def run
4444
# (rather than some optimizations we perform at app runtime).
4545
definition = Bundler.definition(strict: true)
4646
definition.validate_runtime!
47+
definition.lockfile = options["lockfile"] if options["lockfile"]
4748
definition.lockfile = false if options["no-lock"]
4849

4950
installer = Installer.install(Bundler.root, definition, options)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
.SH "NAME"
55
\fBbundle\-install\fR \- Install the dependencies specified in your Gemfile
66
.SH "SYNOPSIS"
7-
\fBbundle install\fR [\-\-force] [\-\-full\-index] [\-\-gemfile=GEMFILE] [\-\-jobs=NUMBER] [\-\-local] [\-\-no\-cache] [\-\-no\-lock] [\-\-prefer\-local] [\-\-quiet] [\-\-retry=NUMBER] [\-\-standalone[=GROUP[ GROUP\|\.\|\.\|\.]]] [\-\-trust\-policy=TRUST\-POLICY] [\-\-target\-rbconfig=TARGET\-RBCONFIG]
7+
\fBbundle install\fR [\-\-force] [\-\-full\-index] [\-\-gemfile=GEMFILE] [\-\-jobs=NUMBER] [\-\-local] [\-\-lockfile=LOCKFILE] [\-\-no\-cache] [\-\-no\-lock] [\-\-prefer\-local] [\-\-quiet] [\-\-retry=NUMBER] [\-\-standalone[=GROUP[ GROUP\|\.\|\.\|\.]]] [\-\-trust\-policy=TRUST\-POLICY] [\-\-target\-rbconfig=TARGET\-RBCONFIG]
88
.SH "DESCRIPTION"
99
Install the gems specified in your Gemfile(5)\. If this is the first time you run bundle install (and a \fBGemfile\.lock\fR does not exist), Bundler will fetch all remote sources, resolve dependencies and install all needed gems\.
1010
.P
@@ -28,6 +28,9 @@ The maximum number of parallel download and install jobs\. The default is the nu
2828
\fB\-\-local\fR
2929
Do not attempt to connect to \fBrubygems\.org\fR\. Instead, Bundler will use the gems already present in Rubygems' cache or in \fBvendor/cache\fR\. Note that if an appropriate platform\-specific gem exists on \fBrubygems\.org\fR it will not be found\.
3030
.TP
31+
\fB\-\-lockfile=LOCKFILE\fR
32+
The location of the lockfile which Bundler should use\. This defaults to the Gemfile location with \fB\.lock\fR appended\.
33+
.TP
3134
\fB\-\-prefer\-local\fR
3235
Force using locally installed gems, or gems already present in Rubygems' cache or in \fBvendor/cache\fR, when resolving, even if newer versions are available remotely\. Only attempt to connect to \fBrubygems\.org\fR for gems that are not present locally\.
3336
.TP

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ bundle-install(1) -- Install the dependencies specified in your Gemfile
88
[--gemfile=GEMFILE]
99
[--jobs=NUMBER]
1010
[--local]
11+
[--lockfile=LOCKFILE]
1112
[--no-cache]
1213
[--no-lock]
1314
[--prefer-local]
@@ -61,6 +62,10 @@ update process below under [CONSERVATIVE UPDATING][].
6162
appropriate platform-specific gem exists on `rubygems.org` it will not be
6263
found.
6364

65+
* `--lockfile=LOCKFILE`:
66+
The location of the lockfile which Bundler should use. This defaults
67+
to the Gemfile location with `.lock` appended.
68+
6469
* `--prefer-local`:
6570
Force using locally installed gems, or gems already present in Rubygems' cache
6671
or in `vendor/cache`, when resolving, even if newer versions are available

bundler/lib/bundler/man/gemfile.5

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,12 @@ When determining path to the lockfile or whether to create a lockfile, the follo
492492
.IP "1." 4
493493
The \fBbundle install\fR \fB\-\-no\-lock\fR option (which disables lockfile creation)\.
494494
.IP "2." 4
495-
The \fBlockfile\fR method in the Gemfile\.
495+
The \fBbundle install\fR \fB\-\-lockfile\fR option\.
496496
.IP "3." 4
497-
The \fBBUNDLE_LOCKFILE\fR environment variable\.
497+
The \fBlockfile\fR method in the Gemfile\.
498498
.IP "4." 4
499+
The \fBBUNDLE_LOCKFILE\fR environment variable\.
500+
.IP "5." 4
499501
The default behavior of adding \fB\.lock\fR to the end of the Gemfile name\.
500502
.IP "" 0
501503

bundler/lib/bundler/man/gemfile.5.ronn

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ When determining path to the lockfile or whether to create a lockfile, the
580580
following precedence is used:
581581

582582
1. The `bundle install` `--no-lock` option (which disables lockfile creation).
583-
2. The `lockfile` method in the Gemfile.
584-
3. The `BUNDLE_LOCKFILE` environment variable.
585-
4. The default behavior of adding `.lock` to the end of the Gemfile name.
583+
1. The `bundle install` `--lockfile` option.
584+
1. The `lockfile` method in the Gemfile.
585+
1. The `BUNDLE_LOCKFILE` environment variable.
586+
1. The default behavior of adding `.lock` to the end of the Gemfile name.

bundler/spec/commands/install_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@
4141
expect(bundled_app("OmgFile.lock")).to exist
4242
end
4343

44+
it "creates lockfile based on --lockfile option is given" do
45+
gemfile bundled_app("OmgFile"), <<-G
46+
source "https://gem.repo1"
47+
gem "myrack", "1.0"
48+
G
49+
50+
bundle "install --gemfile OmgFile --lockfile ReallyOmgFile.lock"
51+
52+
expect(bundled_app("ReallyOmgFile.lock")).to exist
53+
end
54+
4455
it "does not make a lockfile if lockfile false is used in Gemfile" do
4556
install_gemfile <<-G
4657
lockfile false
@@ -100,6 +111,18 @@
100111
expect(bundled_app("OmgFile.lock")).not_to exist
101112
end
102113

114+
it "doesn't create a lockfile if --no-lock and --lockfile options are given" do
115+
gemfile bundled_app("OmgFile"), <<-G
116+
source "https://gem.repo1"
117+
gem "myrack", "1.0"
118+
G
119+
120+
bundle "install --gemfile OmgFile --no-lock --lockfile ReallyOmgFile.lock"
121+
122+
expect(bundled_app("OmgFile.lock")).not_to exist
123+
expect(bundled_app("ReallyOmgFile.lock")).not_to exist
124+
end
125+
103126
it "doesn't delete the lockfile if one already exists" do
104127
install_gemfile <<-G
105128
source "https://gem.repo1"

0 commit comments

Comments
 (0)