Skip to content

Commit

Permalink
use a hash keyed by lockfile for the definition list
Browse files Browse the repository at this point in the history
  • Loading branch information
ccutrer committed Mar 29, 2024
1 parent 336cccc commit d218f9e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
21 changes: 8 additions & 13 deletions lib/bundler/multilock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,24 @@ def add_lockfile(lockfile = nil,
# allow short-form lockfile names
lockfile = expand_lockfile(lockfile)

if lockfile_definitions.find { |definition| definition[:lockfile] == lockfile }
raise ArgumentError, "Lockfile #{lockfile} is already defined"
end
raise ArgumentError, "Lockfile #{lockfile} is already defined" if lockfile_definitions.key?(lockfile)

env_lockfile = lockfile if active && ENV["BUNDLE_LOCKFILE"] == "active"
env_lockfile ||= ENV["BUNDLE_LOCKFILE"]&.then { |l| expand_lockfile(l) }
active = env_lockfile == lockfile if env_lockfile

if active && (old_active = lockfile_definitions.find { |definition| definition[:active] })
if active && (old_active = lockfile_definitions.each_value.find { |definition| definition[:active] })
raise ArgumentError, "Only one lockfile (#{old_active[:lockfile]}) can be flagged as active"
end

parent = expand_lockfile(parent)
if parent != Bundler.default_lockfile(force_original: true) &&
!lockfile_definitions.find { |definition| definition[:lockfile] == parent } &&
!lockfile_definitions.key?(parent) &&
!parent.exist?
raise ArgumentError, "Parent lockfile #{parent} is not defined"
end

lockfile_definitions << (lockfile_def = {
lockfile_definitions[lockfile] = (lockfile_def = {
gemfile: (gemfile && Bundler.root.join(gemfile).expand_path) || Bundler.default_gemfile,
lockfile: lockfile,
active: active,
Expand Down Expand Up @@ -158,8 +156,7 @@ def after_install_all(install: true)
synced_any = false
local_parser_cache = {}
Bundler.settings.temporary(cache_all_platforms: true, suppress_install_using_messages: true) do
lockfile_definitions.each do |lockfile_definition|
lockfile_name = lockfile_definition[:lockfile]
lockfile_definitions.each do |lockfile_name, lockfile_definition|
# we already wrote the default lockfile
next if lockfile_name == Bundler.default_lockfile(force_original: true)

Expand Down Expand Up @@ -334,7 +331,7 @@ def loaded!
@loaded = true
return if lockfile_definitions.empty?

return unless lockfile_definitions.none? { |definition| definition[:active] }
return unless lockfile_definitions.each_value.none? { |definition| definition[:active] }

if ENV["BUNDLE_LOCKFILE"]&.then { |l| expand_lockfile(l) } ==
Bundler.default_lockfile(force_original: true)
Expand All @@ -344,9 +341,7 @@ def loaded!
raise GemfileNotFound, "Could not locate lockfile #{ENV["BUNDLE_LOCKFILE"].inspect}" if ENV["BUNDLE_LOCKFILE"]

# Gemfile.lock isn't explicitly specified, otherwise it would be active
default_lockfile_definition = lockfile_definitions.find do |definition|
definition[:lockfile] == Bundler.default_lockfile(force_original: true)
end
default_lockfile_definition = lockfile_definitions[Bundler.default_lockfile(force_original: true)]
return unless default_lockfile_definition && default_lockfile_definition[:active] == false

raise GemfileEvalError, "No lockfiles marked as active"
Expand Down Expand Up @@ -411,7 +406,7 @@ def inject_preamble

# @!visibility private
def reset!
@lockfile_definitions = []
@lockfile_definitions = {}
@loaded = false
end

Expand Down
8 changes: 4 additions & 4 deletions lib/bundler/multilock/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ def run(skip_base_checks: false)
base_check({ gemfile: Bundler.default_gemfile,
lockfile: Bundler.default_lockfile(force_original: true) })
end
Multilock.lockfile_definitions.each do |lockfile_definition|
next if lockfile_definition[:lockfile] == Bundler.default_lockfile(force_original: true)
Multilock.lockfile_definitions.each do |lockfile_name, lockfile_definition|
next if lockfile_name == Bundler.default_lockfile(force_original: true)

unless lockfile_definition[:lockfile].exist?
Bundler.ui.error("Lockfile #{lockfile_definition[:lockfile]} does not exist.")
unless lockfile_name.exist?
Bundler.ui.error("Lockfile #{lockfile_name} does not exist.")
success = false
next
end
Expand Down

0 comments on commit d218f9e

Please sign in to comment.