lib/rbs/collection/config/lockfile_generator.rb in rbs-2.7.0 vs lib/rbs/collection/config/lockfile_generator.rb in rbs-2.8.0.pre.1
- old
+ new
@@ -1,13 +1,27 @@
# frozen_string_literal: true
module RBS
module Collection
-
- # This class represent the configration file.
class Config
class LockfileGenerator
+ class GemfileLockMismatchError < StandardError
+ def initialize(expected:, actual:)
+ @expected = expected
+ @actual = actual
+ end
+
+ def message
+ <<~MESSAGE
+ RBS Collection loads a different Gemfile.lock from before.
+ The Gemfile.lock must be the same as that is recorded in rbs_collection.lock.yaml.
+ Expected Gemfile.lock: #{@expected}
+ Actual Gemfile.lock: #{@actual}
+ MESSAGE
+ end
+ end
+
attr_reader :config, :lock, :gemfile_lock, :lock_path
def self.generate(config_path:, gemfile_lock_path:, with_lockfile: true)
new(config_path: config_path, gemfile_lock_path: gemfile_lock_path, with_lockfile: with_lockfile).generate
end
@@ -16,10 +30,14 @@
@config = Config.from_path config_path
@lock_path = Config.to_lockfile_path(config_path)
@lock = Config.from_path(lock_path) if lock_path.exist? && with_lockfile
@gemfile_lock = Bundler::LockfileParser.new(gemfile_lock_path.read)
@gem_queue = []
+
+ validate_gemfile_lock_path!(lock: lock, gemfile_lock_path: gemfile_lock_path)
+
+ config.gemfile_lock_path = gemfile_lock_path
end
def generate
config.gems.each do |gem|
@gem_queue.push({ name: gem['name'], version: gem['version'] })
@@ -34,9 +52,17 @@
end
remove_ignored_gems!
config.dump_to(lock_path)
config
+ end
+
+ private def validate_gemfile_lock_path!(lock:, gemfile_lock_path:)
+ return unless lock
+ return unless lock.gemfile_lock_path
+ return if lock.gemfile_lock_path == gemfile_lock_path
+
+ raise GemfileLockMismatchError.new(expected: lock.gemfile_lock_path, actual: gemfile_lock_path)
end
private def assign_gem(name:, version:)
# @type var locked: gem_entry?
locked = lock&.gem(name)