Class: RuboCop::Lockfile Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/lockfile.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Encapsulation of a lockfile for use when checking for gems. Does not actually resolve gems, just parses the lockfile.

Instance Method Summary collapse

Constructor Details

#initialize(lockfile_path = nil) ⇒ Lockfile

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Lockfile.

Parameters:

  • lockfile_path (String, Pathname, nil) (defaults to: nil)


17
18
19
20
21
22
23
24
25
# File 'lib/rubocop/lockfile.rb', line 17

def initialize(lockfile_path = nil)
  lockfile_path ||= begin
    ::Bundler.default_lockfile if bundler_lock_parser_defined?
  rescue ::Bundler::GemfileNotFound
    nil # We might not be a folder with a Gemfile, but that's okay.
  end

  @lockfile_path = lockfile_path
end

Instance Method Details

#dependenciesArray<Bundler::Dependency>?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Gems that the bundle directly depends on.

Returns:

  • (Array<Bundler::Dependency>, nil)


29
30
31
32
33
# File 'lib/rubocop/lockfile.rb', line 29

def dependencies
  return [] unless parser

  parser.dependencies.values
end

#gem_versions(include_transitive_dependencies: true) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



49
50
51
52
53
54
55
56
57
58
59
60
# 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>?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

All activated gems, including transitive dependencies.

Returns:

  • (Array<Bundler::Dependency>, nil)


37
38
39
40
41
42
43
# 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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether this lockfile includes the named gem, directly or indirectly.

Parameters:

Returns:

  • (Boolean)


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

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