summaryrefslogtreecommitdiff
path: root/lib/bundler/definition.rb
diff options
context:
space:
mode:
authorDavid Rodríguez <deivid.rodriguez@riseup.net>2023-03-02 20:18:35 +0100
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2024-02-08 14:43:56 +0900
commit24d5e7176e80cecfc38daf80020fb85f1144083b (patch)
treeba0a8e927ba62f509044e6ac0b478f3d3ee5528b /lib/bundler/definition.rb
parentdebc5aaee57a25745259fd02a54701e6c184fa7c (diff)
[rubygems/rubygems] Refactor lockfile generation
https://github.com/rubygems/rubygems/commit/6a0c03c77f
Diffstat (limited to 'lib/bundler/definition.rb')
-rw-r--r--lib/bundler/definition.rb82
1 files changed, 53 insertions, 29 deletions
diff --git a/lib/bundler/definition.rb b/lib/bundler/definition.rb
index 0b0e63f77e..32a7089925 100644
--- a/lib/bundler/definition.rb
+++ b/lib/bundler/definition.rb
@@ -320,38 +320,24 @@ module Bundler
dependencies.map(&:groups).flatten.uniq
end
- def lock(file, preserve_unknown_sections = false)
- return if Definition.no_lock
-
- contents = to_lock
-
- # Convert to \r\n if the existing lock has them
- # i.e., Windows with `git config core.autocrlf=true`
- contents.gsub!(/\n/, "\r\n") if @lockfile_contents.match?("\r\n")
-
- if @locked_bundler_version
- locked_major = @locked_bundler_version.segments.first
- current_major = bundler_version_to_lock.segments.first
-
- updating_major = locked_major < current_major
- end
-
- preserve_unknown_sections ||= !updating_major && (Bundler.frozen_bundle? || !(unlocking? || @unlocking_bundler))
+ def lock(file_or_preserve_unknown_sections = false, preserve_unknown_sections_or_unused = false)
+ if [true, false, nil].include?(file_or_preserve_unknown_sections)
+ target_lockfile = lockfile || Bundler.default_lockfile
+ preserve_unknown_sections = file_or_preserve_unknown_sections
+ else
+ target_lockfile = file_or_preserve_unknown_sections
+ preserve_unknown_sections = preserve_unknown_sections_or_unused
- if file && File.exist?(file) && lockfiles_equal?(@lockfile_contents, contents, preserve_unknown_sections)
- return if Bundler.frozen_bundle?
- SharedHelpers.filesystem_access(file) { FileUtils.touch(file) }
- return
- end
+ suggestion = if target_lockfile == lockfile
+ "To fix this warning, remove it from the `Definition#lock` call."
+ else
+ "Instead, instantiate a new definition passing `#{target_lockfile}`, and call `lock` without a file argument on that definition"
+ end
- if Bundler.frozen_bundle?
- Bundler.ui.error "Cannot write a changed lockfile while frozen."
- return
+ warn "Passing a file to `Definition#lock` is deprecated. #{suggestion}"
end
- SharedHelpers.filesystem_access(file) do |p|
- File.open(p, "wb") {|f| f.puts(contents) }
- end
+ write_lock(target_lockfile, preserve_unknown_sections)
end
def locked_ruby_version
@@ -518,7 +504,45 @@ module Bundler
end
def lockfile_exists?
- lockfile && File.exist?(lockfile)
+ file_exists?(lockfile)
+ end
+
+ def file_exists?(file)
+ file && File.exist?(file)
+ end
+
+ def write_lock(file, preserve_unknown_sections)
+ return if Definition.no_lock
+
+ contents = to_lock
+
+ # Convert to \r\n if the existing lock has them
+ # i.e., Windows with `git config core.autocrlf=true`
+ contents.gsub!(/\n/, "\r\n") if @lockfile_contents.match?("\r\n")
+
+ if @locked_bundler_version
+ locked_major = @locked_bundler_version.segments.first
+ current_major = bundler_version_to_lock.segments.first
+
+ updating_major = locked_major < current_major
+ end
+
+ preserve_unknown_sections ||= !updating_major && (Bundler.frozen_bundle? || !(unlocking? || @unlocking_bundler))
+
+ if file_exists?(file) && lockfiles_equal?(@lockfile_contents, contents, preserve_unknown_sections)
+ return if Bundler.frozen_bundle?
+ SharedHelpers.filesystem_access(file) { FileUtils.touch(file) }
+ return
+ end
+
+ if Bundler.frozen_bundle?
+ Bundler.ui.error "Cannot write a changed lockfile while frozen."
+ return
+ end
+
+ SharedHelpers.filesystem_access(file) do |p|
+ File.open(p, "wb") {|f| f.puts(contents) }
+ end
end
def resolver