Class: Chelsea::Deps

Inherits:
Object
  • Object
show all
Defined in:
lib/chelsea/deps.rb

Overview

Project dependencies

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, verbose: false) ⇒ Deps

Returns a new instance of Deps.



29
30
31
32
33
# File 'lib/chelsea/deps.rb', line 29

def initialize(path:, verbose: false)
  @verbose = verbose
  ENV['BUNDLE_GEMFILE'] = File.expand_path(path).chomp('.lock')
  @lockfile = Bundler::LockfileParser.new(File.read(path))
end

Class Method Details

.to_purl(name, version) ⇒ Object



39
40
41
# File 'lib/chelsea/deps.rb', line 39

def self.to_purl(name, version)
  "pkg:gem/#{name}@#{version}"
end

Instance Method Details

#coordinatesObject

Iterates over all dependencies and stores them in dependencies_versions and coordinates instance vars



72
73
74
75
76
# File 'lib/chelsea/deps.rb', line 72

def coordinates
  dependencies.each_with_object({ 'coordinates' => [] }) do |(name, v), coords|
    coords['coordinates'] << self.class.to_purl(name, v[1])
  end
end

#dependenciesObject

Parses specs from lockfile instanct var and inserts into dependenices instance var



45
46
47
48
49
# File 'lib/chelsea/deps.rb', line 45

def dependencies
  @lockfile.specs.each_with_object({}) do |gem, h|
    h[gem.name] = [gem.name, gem.version]
  end
end

#nil?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/chelsea/deps.rb', line 35

def nil?
  @dependencies.empty?
end

#reverse_dependenciesObject

Collects all reverse dependencies in reverse_dependencies instance var this rescue block honks



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/chelsea/deps.rb', line 53

def reverse_dependencies # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  reverse = Gem::Commands::DependencyCommand.new
  reverse.options[:reverse_dependencies] = true
  # We want to filter the reverses dependencies by specs in lockfile
  spec_names = @lockfile.specs.map { |i| i.to_s.split }.map do |n, _v|
    n.to_s
  end
  reverse
    .reverse_dependencies(@lockfile.specs)
    .to_h
    .transform_values! do |reverse_dep|
      reverse_dep.select do |name, _dep, _req, _|
        spec_names.include?(name.split('-')[0])
      end
    end
end