-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce a cache object to share things between syncing and check
- Loading branch information
Showing
4 changed files
with
237 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# frozen_string_literal: true | ||
|
||
module Bundler | ||
module Multilock | ||
# caches lockfiles across multiple lockfile checks or sync runs | ||
class Cache | ||
def initialize | ||
@contents = {} | ||
@parsers = {} | ||
@specs = {} | ||
@reverse_dependencies = {} | ||
@base_checks = {} | ||
@deep_checks = {} | ||
@missing_specs = Set.new | ||
@logged_missing = false | ||
end | ||
|
||
# Removes a given lockfile's associated cached data | ||
# | ||
# Should be called if the lockfile is modified | ||
# @param lockfile_name [Pathname] | ||
# @return [void] | ||
def invalidate_lockfile(lockfile_name) | ||
@contents.delete(lockfile_name) | ||
@parsers.delete(lockfile_name) | ||
@specs.delete(lockfile_name) | ||
@reverse_dependencies.delete(lockfile_name) | ||
@base_checks.delete(lockfile_name) | ||
@deep_checks.delete(lockfile_name) | ||
end | ||
|
||
# @param lockfile_name [Pathname] | ||
# @return [String] the raw contents of the lockfile | ||
def contents(lockfile_name) | ||
@contents[lockfile_name] ||= lockfile_name.read.freeze | ||
end | ||
|
||
# @param lockfile_name [Pathname] | ||
# @return [LockfileParser] | ||
def parser(lockfile_name) | ||
@parsers[lockfile_name] ||= LockfileParser.new(contents(lockfile_name)) | ||
end | ||
|
||
def specs(lockfile_name) | ||
@specs[lockfile_name] ||= parser(lockfile_name).specs.to_h do |spec| | ||
[[spec.name, spec.platform], spec] | ||
end | ||
end | ||
|
||
# @param lockfile_name [Pathname] | ||
# @return [Hash<String, Gem::Requirement>] hash of gem name to requirement for that gem | ||
def reverse_dependencies(lockfile_name) | ||
@reverse_dependencies[lockfile_name] ||= begin | ||
# can use Gem::Requirement.default_prelease when Ruby 2.6 support is dropped | ||
reverse_dependencies = Hash.new { |h, k| h[k] = Gem::Requirement.new(">= 0.a") } | ||
|
||
lockfile = parser(lockfile_name) | ||
|
||
lockfile.dependencies.each_value do |spec| | ||
reverse_dependencies[spec.name].requirements.concat(spec.requirement.requirements) | ||
end | ||
lockfile.specs.each do |spec| | ||
spec.dependencies.each do |dependency| | ||
reverse_dependencies[dependency.name].requirements.concat(dependency.requirement.requirements) | ||
end | ||
end | ||
|
||
reverse_dependencies | ||
end | ||
end | ||
|
||
def log_missing_spec(spec) | ||
return if @missing_specs.include?(spec) | ||
|
||
Bundler.ui.error "The following gems are missing" if @missing_specs.empty? | ||
@missing_specs << spec | ||
Bundler.ui.error(" * #{spec.name} (#{spec.version})") | ||
end | ||
|
||
def base_check(lockfile_name) | ||
@base_checks.fetch(lockfile_name) { @base_checks[lockfile_name] = yield } | ||
end | ||
|
||
def deep_check(lockfile_name) | ||
@deep_checks.fetch(lockfile_name) { @deep_checks[lockfile_name] = yield } | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.