class RBS::Collection::Config::LockfileGenerator

def self.generate(config:, definition:, with_lockfile: true)

def self.generate(config:, definition:, with_lockfile: true)
  generator = new(config: config, definition: definition, with_lockfile: with_lockfile)
  generator.generate
  generator.lockfile
end

def assign_gem(name:, version:, skip: false)

def assign_gem(name:, version:, skip: false)
= gem_entries[name]
ta = entry&.fetch("source", nil)
d = entry&.fetch("ignore", false)
 if ignored
 if lockfile.gems.key?(name)
 skip
ype var locked: Lockfile::library?
xisting_lockfile
cked = existing_lockfile.gems[name]
 rbs_collection.lock.yaml contain the gem, use it.
se find the gem from gem_collection.
ss locked
urce =
if src_data
  Sources.from_config_entry(src_data, base_directory: config.config_path.dirname)
else
  find_source(name: name)
end
 source
installed_version = version
best_version = find_best_version(version: installed_version, versions: source.versions(name))
locked = {
  name: name,
  version: best_version.to_s,
  source: source,
}
d
ocked
ckfile.gems[name] = locked
gin
locked[:source].dependencies_of(locked[:name], locked[:version])&.each do |dep|
  assign_stdlib(name: dep["name"], from_gem: name)
end
scue
RBS.logger.warn "Cannot find `#{locked[:name]}-#{locked[:version]}` gem. Using incorrect Bundler context? (#{definition.lockfile})"
d
c = gem_hash.fetch(name, nil)
.dependencies.each do |dep|
 dep_spec = gem_hash[dep.name]
assign_gem(name: dep.name, version: dep_spec.version)
d
logger.warn "Cannot find `#{name}` gem. Using incorrect Bundler context? (#{definition.lockfile})"

def assign_stdlib(name:, from_gem:)

def assign_stdlib(name:, from_gem:)
 if lockfile.gems.key?(name)
ame
rubygems', 'set'
= "`#{name}` has been moved to core library, so it is always loaded. Remove explicit loading `#{name}`"
<< " from `#{from_gem}`" if from_gem
logger.warn msg
rn
 = Sources::Stdlib.instance
le.gems[name] = { name: name, version: "0", source: source }
 source.has?(name, nil)
e "Cannot find `#{name}` from standard libraries"
s = source.dependencies_of(name, "0")
.each do |dep|
sign_stdlib(name: dep["name"], from_gem: name)

def find_best_version(version:, versions:)

def find_best_version(version:, versions:)
ates = versions.map { |v| Gem::Version.create(v) or raise }
 candidates.max || raise unless version
m::Version.create(version) or raise
tory.find_best_version(v, candidates)

def find_source(name:)

def find_source(name:)
s = config.sources
s.find { |c| c.has?(name, nil) }

def generate

def generate
  config.gems.each do |gem|
    if Sources::Stdlib.instance.has?(gem["name"], nil) || gem.dig("source", "type") == "stdlib"
      unless gem.fetch("ignore", false)
        assign_stdlib(name: gem["name"], from_gem: nil)
      end
    else
      assign_gem(name: gem["name"], version: gem["version"])
    end
  end
  definition.dependencies.each do |dep|
    if dep.autorequire && dep.autorequire.empty?
      next
    end
    if spec = gem_hash[dep.name]
      assign_gem(name: dep.name, version: spec.version, skip: dep.source.is_a?(Bundler::Source::Gemspec))
    end
  end
  lockfile.lockfile_path.write(YAML.dump(lockfile.to_lockfile))
end

def initialize(config:, definition:, with_lockfile:)

def initialize(config:, definition:, with_lockfile:)
  @config = config
  @gem_entries = config.gems.each.with_object({}) do |entry, hash| #$ Hash[String, gem_entry?]
    name = entry["name"]
    hash[name] = entry
  end
  lockfile_path = Config.to_lockfile_path(config.config_path)
  lockfile_dir = lockfile_path.parent
  @lockfile = Lockfile.new(
    lockfile_path: lockfile_path,
    path: config.repo_path_data,
    gemfile_lock_path: definition.lockfile.relative_path_from(lockfile_dir)
  )
  if with_lockfile && lockfile_path.file?
    @existing_lockfile = Lockfile.from_lockfile(lockfile_path: lockfile_path, data: YAML.load_file(lockfile_path.to_s))
    validate_gemfile_lock_path!(lock: @existing_lockfile, gemfile_lock_path: definition.lockfile)
  end
  @definition = definition
  @gem_hash = definition.locked_gems.specs.each.with_object({}) do |spec, hash|  #$ Hash[String, Bundler::LazySpecification]
    hash[spec.name] = spec
  end
end

def validate_gemfile_lock_path!(lock:, gemfile_lock_path:)

def validate_gemfile_lock_path!(lock:, gemfile_lock_path:)
 unless lock
 unless lock.gemfile_lock_fullpath
 File.realpath(lock.gemfile_lock_fullpath) == File.realpath(gemfile_lock_path)
e GemfileLockMismatchError.new(expected: lock.gemfile_lock_fullpath, actual: gemfile_lock_path)