Class: RuboCop::Lockfile Private
Do not use. This class is for internal use only.
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/rubocop/lockfile.rb |
Overview
Encapsulation of a lockfile for use when checking for gems. Does not actually resolve gems, just parses the lockfile.
Class Method Summary
- .new(lockfile_path = nil) ⇒ Lockfile constructor Internal use only
Instance Attribute Summary
- #use_bundler_lock_parser? ⇒ Boolean readonly private Internal use only
Instance Method Summary
-
#dependencies ⇒ Array<Bundler::Dependency>?
Internal use only
Gems that the bundle directly depends on.
-
#gem_versions(include_transitive_dependencies: true)
Internal use only
Returns the locked versions of gems from this lockfile.
-
#gems ⇒ Array<Bundler::Dependency>?
Internal use only
All activated gems, including transitive dependencies.
-
#includes_gem?(name) ⇒ Boolean
Internal use only
Whether this lockfile includes the named gem, directly or indirectly.
- #parser ⇒ Bundler::LockfileParser? private Internal use only
Instance Attribute Details
#use_bundler_lock_parser? ⇒ Boolean
(readonly, private)
# File 'lib/rubocop/lockfile.rb', line 85
def use_bundler_lock_parser? return false unless Object.const_defined?(:Bundler) Bundler.const_defined?(:LockfileParser) && Bundler::VERSION >= '2.0' end
Instance Method Details
#dependencies ⇒ Array
<Bundler::Dependency
>?
Gems that the bundle directly depends on.
#gem_versions(include_transitive_dependencies: true)
Returns the locked versions of gems from this lockfile.
# File 'lib/rubocop/lockfile.rb', line 49
def gem_versions(include_transitive_dependencies: true) return {} unless parser all_gem_versions = parser.specs.to_h { |spec| [spec.name, spec.version] } if include_transitive_dependencies all_gem_versions else direct_dep_names = parser.dependencies.keys all_gem_versions.slice(*direct_dep_names) end end
#gems ⇒ Array
<Bundler::Dependency
>?
All activated gems, including transitive dependencies.
# File 'lib/rubocop/lockfile.rb', line 37
def gems return [] unless parser # `Bundler::LockfileParser` returns `Bundler::LazySpecification` objects # which are not resolved, so extract the dependencies from them parser.dependencies.values.concat(parser.specs.flat_map(&:dependencies)) end
#includes_gem?(name) ⇒ Boolean
Whether this lockfile includes the named gem, directly or indirectly.
# File 'lib/rubocop/lockfile.rb', line 65
def includes_gem?(name) gems.any? { |gem| gem.name == name } end
#parser ⇒ Bundler::LockfileParser
? (private)
# File 'lib/rubocop/lockfile.rb', line 72
def parser return @parser if defined?(@parser) @parser = if @lockfile_path && File.exist?(@lockfile_path) && use_bundler_lock_parser? begin lockfile = ::Bundler.read_file(@lockfile_path) ::Bundler::LockfileParser.new(lockfile) if lockfile rescue ::Bundler::BundlerError nil end end end