@@ -320,38 +320,26 @@ def groups
320320 dependencies . map ( &:groups ) . flatten . uniq
321321 end
322322
323- def lock ( file , preserve_unknown_sections = false )
324- return if Definition . no_lock
325-
326- contents = to_lock
327-
328- # Convert to \r\n if the existing lock has them
329- # i.e., Windows with `git config core.autocrlf=true`
330- contents . gsub! ( /\n / , "\r \n " ) if @lockfile_contents . match? ( "\r \n " )
331-
332- if @locked_bundler_version
333- locked_major = @locked_bundler_version . segments . first
334- current_major = bundler_version_to_lock . segments . first
335-
336- updating_major = locked_major < current_major
337- end
323+ def lock ( file_or_preserve_unknown_sections = false , preserve_unknown_sections_or_unused = false )
324+ if [ true , false , nil ] . include? ( file_or_preserve_unknown_sections )
325+ target_lockfile = lockfile || Bundler . default_lockfile
326+ preserve_unknown_sections = file_or_preserve_unknown_sections
327+ else
328+ target_lockfile = file_or_preserve_unknown_sections
329+ preserve_unknown_sections = preserve_unknown_sections_or_unused
338330
339- preserve_unknown_sections ||= !updating_major && ( Bundler . frozen_bundle? || !( unlocking? || @unlocking_bundler ) )
331+ suggestion = if target_lockfile == lockfile
332+ "To fix this warning, remove it from the `Definition#lock` call."
333+ else
334+ "Instead, instantiate a new definition passing `#{ target_lockfile } `, and call `lock` without a file argument on that definition"
335+ end
340336
341- if file && File . exist? ( file ) && lockfiles_equal? ( @lockfile_contents , contents , preserve_unknown_sections )
342- return if Bundler . frozen_bundle?
343- SharedHelpers . filesystem_access ( file ) { FileUtils . touch ( file ) }
344- return
345- end
337+ msg = "`Definition#lock` was passed a target file argument. #{ suggestion } "
346338
347- if Bundler . frozen_bundle?
348- Bundler . ui . error "Cannot write a changed lockfile while frozen."
349- return
339+ Bundler ::SharedHelpers . major_deprecation 2 , msg
350340 end
351341
352- SharedHelpers . filesystem_access ( file ) do |p |
353- File . open ( p , "wb" ) { |f | f . puts ( contents ) }
354- end
342+ write_lock ( target_lockfile , preserve_unknown_sections )
355343 end
356344
357345 def locked_ruby_version
@@ -518,7 +506,45 @@ def should_add_extra_platforms?
518506 end
519507
520508 def lockfile_exists?
521- lockfile && File . exist? ( lockfile )
509+ file_exists? ( lockfile )
510+ end
511+
512+ def file_exists? ( file )
513+ file && File . exist? ( file )
514+ end
515+
516+ def write_lock ( file , preserve_unknown_sections )
517+ return if Definition . no_lock
518+
519+ contents = to_lock
520+
521+ # Convert to \r\n if the existing lock has them
522+ # i.e., Windows with `git config core.autocrlf=true`
523+ contents . gsub! ( /\n / , "\r \n " ) if @lockfile_contents . match? ( "\r \n " )
524+
525+ if @locked_bundler_version
526+ locked_major = @locked_bundler_version . segments . first
527+ current_major = bundler_version_to_lock . segments . first
528+
529+ updating_major = locked_major < current_major
530+ end
531+
532+ preserve_unknown_sections ||= !updating_major && ( Bundler . frozen_bundle? || !( unlocking? || @unlocking_bundler ) )
533+
534+ if file_exists? ( file ) && lockfiles_equal? ( @lockfile_contents , contents , preserve_unknown_sections )
535+ return if Bundler . frozen_bundle?
536+ SharedHelpers . filesystem_access ( file ) { FileUtils . touch ( file ) }
537+ return
538+ end
539+
540+ if Bundler . frozen_bundle?
541+ Bundler . ui . error "Cannot write a changed lockfile while frozen."
542+ return
543+ end
544+
545+ SharedHelpers . filesystem_access ( file ) do |p |
546+ File . open ( p , "wb" ) { |f | f . puts ( contents ) }
547+ end
522548 end
523549
524550 def resolver
0 commit comments