diff --git a/CHANGELOG.md b/CHANGELOG.md index 331d79e..6d5f4c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ a nice looking [Changelog](http://keepachangelog.com). ## Version [HEAD] now +* Adds ability to pristine original Gemfile.lock + ## Version [2.0.0] 2021-05-01 * **BREAKING**: Drops support for bundler below 2.0 diff --git a/lib/bundler/inject/dsl_patch.rb b/lib/bundler/inject/dsl_patch.rb index af5d27f..e67eac9 100644 --- a/lib/bundler/inject/dsl_patch.rb +++ b/lib/bundler/inject/dsl_patch.rb @@ -28,6 +28,12 @@ def ensure_gem(name, *args) end def to_definition(lockfile, unlock) + return super if ENV['BUNDLER_INJECT__DISABLE_OVERRIDE'] + + original_unlock = unlock.dup + original_lockfile = lockfile + lockfile = Pathname.new("#{lockfile}.local") if enable_pristine?(lockfile) + calling_loc = caller_locations(1, 1).first if calling_loc.path.include?("bundler/dsl.rb") && calling_loc.base_label == "evaluate" load_global_bundler_d @@ -35,13 +41,16 @@ def to_definition(lockfile, unlock) # @gemfiles doesn't exist on Bundler <= 1.15, and we can't get at @gemfile # by this point, but there's a high probability it's just "Gemfile", # or slightly more accurately, the lockfile name without the ".lock" bit. - targets = defined?(@gemfiles) ? @gemfiles : [Pathname.new(lockfile.to_s.chomp(".lock"))] + targets = defined?(@gemfiles) ? @gemfiles : [Pathname.new(original_lockfile.to_s.chomp(".lock"))] targets.reverse_each do |gemfile| load_local_bundler_d(File.dirname(gemfile)) end end - super + + super(lockfile, unlock).tap do |definition| + add_pristine_lock(definition, original_unlock) if enable_pristine?(original_lockfile) + end end private @@ -97,6 +106,24 @@ def load_bundler_d(dir) eval_gemfile(f) end end + + def add_pristine_lock(definition, original_unlock) + definition.define_singleton_method(:lock) do |file, *args| + original_disable = ENV['BUNDLER_INJECT__DISABLE_OVERRIDE'] + ENV['BUNDLER_INJECT__DISABLE_OVERRIDE'] = 'true' + + pristine_definition = Bundler::Dsl.evaluate(@gemfiles.first, Bundler.default_lockfile, original_unlock) + pristine_definition.resolve_remotely! if pristine_definition.missing_specs? + pristine_definition.lock(Bundler.default_lockfile, *args) + + ENV['BUNDLER_INJECT__DISABLE_OVERRIDE'] = original_disable + super(Pathname.new("#{file}.local"), *args) + end + end + + def enable_pristine?(lockfile) + lockfile == Bundler.default_lockfile && Bundler.settings["bundler_inject.enable_pristine"] + end end end end diff --git a/spec/support/helpers.rb b/spec/support/helpers.rb index c9ace81..90c77b1 100644 --- a/spec/support/helpers.rb +++ b/spec/support/helpers.rb @@ -111,7 +111,8 @@ def write_gemfile(contents) end def lockfile - file = app_dir.join("Gemfile.lock") + lock_name = Bundler.settings["bundler_inject.enable_pristine"] ? "Gemfile.lock.local" : "Gemfile.lock" + file = app_dir.join(lock_name) Bundler::LockfileParser.new(file.read) if file.exist? end