123456789_123456789_123456789_123456789_123456789_

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

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#bundler_lock_parser_defined?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/lockfile.rb', line 85

def bundler_lock_parser_defined?
  Object.const_defined?(:Bundler) && Bundler.const_defined?(:LockfileParser)
end

Instance Method Details

#dependenciesArray<Bundler::Dependency>?

Gems that the bundle directly depends on.

[ GitHub ]

  
# File 'lib/rubocop/lockfile.rb', line 29

def dependencies
  return [] unless parser

  parser.dependencies.values
end

#gem_versions(include_transitive_dependencies: true)

Returns the locked versions of gems from this lockfile.

Parameters:

  • include_transitive_dependencies: (Boolean) (defaults to: true)

    When false, only direct dependencies are returned, i.e. those listed explicitly in the Gemfile.

[ GitHub ]

  
# 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

#gemsArray<Bundler::Dependency>?

All activated gems, including transitive dependencies.

[ GitHub ]

  
# 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.

Parameters:

[ GitHub ]

  
# File 'lib/rubocop/lockfile.rb', line 65

def includes_gem?(name)
  gems.any? { |gem| gem.name == name }
end

#parserBundler::LockfileParser? (private)

[ GitHub ]

  
# File 'lib/rubocop/lockfile.rb', line 72

def parser
  return @parser if defined?(@parser)

  @parser = if @lockfile_path && bundler_lock_parser_defined?
              begin
                lockfile = ::Bundler.read_file(@lockfile_path)
                ::Bundler::LockfileParser.new(lockfile) if lockfile
              rescue ::Bundler::BundlerError
                nil
              end
            end
end