Skip to content

Commit 82d8d24

Browse files
jeremyevanscolby-swandale
authored andcommitted
[ruby/rubygems] Add support for lockfile in Gemfile
This allows you to specify the lockfile to use. This is useful if you want to use different lockfiles for different ruby versions or platforms. You can also skip writing the lockfile by using a false value. ruby/rubygems@2896aa3fc2 Co-authored-by: Colby Swandale <[email protected]>
1 parent fb28d47 commit 82d8d24

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

lib/bundler/definition.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class << self
1010
attr_accessor :no_lock
1111
end
1212

13+
attr_writer :lockfile
14+
1315
attr_reader(
1416
:dependencies,
1517
:locked_checksums,
@@ -380,7 +382,7 @@ def lock(file_or_preserve_unknown_sections = false, preserve_unknown_sections_or
380382
end
381383

382384
def write_lock(file, preserve_unknown_sections)
383-
return if Definition.no_lock || file.nil?
385+
return if Definition.no_lock || !lockfile || file.nil?
384386

385387
contents = to_lock
386388

lib/bundler/dsl.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ class Dsl
99

1010
def self.evaluate(gemfile, lockfile, unlock)
1111
builder = new
12+
builder.lockfile(lockfile)
1213
builder.eval_gemfile(gemfile)
13-
builder.to_definition(lockfile, unlock)
14+
builder.to_definition(builder.lockfile_path, unlock)
1415
end
1516

1617
VALID_PLATFORMS = Bundler::CurrentRuby::PLATFORM_MAP.keys.freeze
@@ -38,6 +39,7 @@ def initialize
3839
@gemspecs = []
3940
@gemfile = nil
4041
@gemfiles = []
42+
@lockfile = nil
4143
add_git_sources
4244
end
4345

@@ -101,6 +103,15 @@ def gem(name, *args)
101103
add_dependency(name, version, options)
102104
end
103105

106+
# For usage in Dsl.evaluate, since lockfile is used as part of the Gemfile.
107+
def lockfile_path
108+
@lockfile
109+
end
110+
111+
def lockfile(file)
112+
@lockfile = file
113+
end
114+
104115
def source(source, *args, &blk)
105116
options = args.last.is_a?(Hash) ? args.pop.dup : {}
106117
options = normalize_hash(options)
@@ -175,6 +186,7 @@ def github(repo, options = {})
175186

176187
def to_definition(lockfile, unlock)
177188
check_primary_source_safety
189+
lockfile = @lockfile unless @lockfile.nil?
178190
Definition.new(lockfile, @dependencies, @sources, unlock, @ruby_version, @optional_groups, @gemfiles)
179191
end
180192

lib/bundler/man/gemfile.5

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,4 +469,21 @@ For implicit gems (dependencies of explicit gems), any source, git, or path repo
469469
.IP "3." 4
470470
If neither of the above conditions are met, the global source will be used\. If multiple global sources are specified, they will be prioritized from last to first, but this is deprecated since Bundler 1\.13, so Bundler prints a warning and will abort with an error in the future\.
471471
.IP "" 0
472-
472+
.SH "LOCKFILE"
473+
By default, Bundler will create a lockfile by adding \fB\.lock\fR to the end of the Gemfile name\. To change this, use the \fBlockfile\fR method:
474+
.IP "" 4
475+
.nf
476+
lockfile "/path/to/lockfile\.lock"
477+
.fi
478+
.IP "" 0
479+
.P
480+
This is useful when you want to use different lockfiles per ruby version or platform\.
481+
.P
482+
To avoid writing a lock file, use \fBfalse\fR as the argument:
483+
.IP "" 4
484+
.nf
485+
lockfile false
486+
.fi
487+
.IP "" 0
488+
.P
489+
This is useful for library development and other situations where the code is expected to work with a range of dependency versions\.

lib/bundler/man/gemfile.5.ronn

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,3 +556,20 @@ bundler uses the following priority order:
556556
If multiple global sources are specified, they will be prioritized from
557557
last to first, but this is deprecated since Bundler 1.13, so Bundler prints
558558
a warning and will abort with an error in the future.
559+
560+
## LOCKFILE
561+
562+
By default, Bundler will create a lockfile by adding `.lock` to the end of the
563+
Gemfile name. To change this, use the `lockfile` method:
564+
565+
lockfile "/path/to/lockfile.lock"
566+
567+
This is useful when you want to use different lockfiles per ruby version or
568+
platform.
569+
570+
To avoid writing a lock file, use `false` as the argument:
571+
572+
lockfile false
573+
574+
This is useful for library development and other situations where the code is
575+
expected to work with a range of dependency versions.

spec/bundler/commands/install_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@
2929
expect(bundled_app_lock).to exist
3030
end
3131

32+
it "creates lockfile based on the lockfile method in Gemfile" do
33+
install_gemfile <<-G
34+
lockfile "OmgFile.lock"
35+
source "https://gem.repo1"
36+
gem "myrack", "1.0"
37+
G
38+
39+
bundle "install"
40+
41+
expect(bundled_app("OmgFile.lock")).to exist
42+
end
43+
44+
it "does not make a lockfile if lockfile false is used in Gemfile" do
45+
install_gemfile <<-G
46+
lockfile false
47+
source "https://gem.repo1"
48+
gem 'myrack'
49+
G
50+
51+
expect(bundled_app_lock).not_to exist
52+
end
53+
3254
it "does not create ./.bundle by default" do
3355
install_gemfile <<-G
3456
source "https://gem.repo1"

0 commit comments

Comments
 (0)