Class: BundleUpdateInteractive::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/bundle_update_interactive/report.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gemfile:, current_lockfile:, updated_lockfile:) ⇒ Report

Returns a new instance of Report.



22
23
24
25
26
27
28
29
30
31
# File 'lib/bundle_update_interactive/report.rb', line 22

def initialize(gemfile:, current_lockfile:, updated_lockfile:)
  @current_lockfile = current_lockfile
  @outdated_gems ||= current_lockfile.entries.each_with_object({}) do |current_lockfile_entry, hash|
    name = current_lockfile_entry.name
    updated_lockfile_entry = updated_lockfile[name]
    next unless current_lockfile_entry.older_than?(updated_lockfile_entry)

    hash[name] = build_outdated_gem(current_lockfile_entry, updated_lockfile_entry, gemfile[name]&.groups)
  end.freeze
end

Instance Attribute Details

#outdated_gemsObject (readonly)

Returns the value of attribute outdated_gems.



20
21
22
# File 'lib/bundle_update_interactive/report.rb', line 20

def outdated_gems
  @outdated_gems
end

Class Method Details

.generateObject



11
12
13
14
15
16
17
# File 'lib/bundle_update_interactive/report.rb', line 11

def generate
  gemfile = Gemfile.parse
  current_lockfile = Lockfile.parse
  updated_lockfile = Lockfile.parse(BundlerCommands.read_updated_lockfile)

  new(gemfile: gemfile, current_lockfile: current_lockfile, updated_lockfile: updated_lockfile)
end

Instance Method Details

#[](gem_name) ⇒ Object



33
34
35
# File 'lib/bundle_update_interactive/report.rb', line 33

def [](gem_name)
  outdated_gems[gem_name]
end

#bundle_update!(*gem_names) ⇒ Object



61
62
63
64
# File 'lib/bundle_update_interactive/report.rb', line 61

def bundle_update!(*gem_names)
  expanded_names = expand_gems_with_exact_dependencies(*gem_names)
  BundlerCommands.update_gems_conservatively(*expanded_names)
end

#expand_gems_with_exact_dependencies(*gem_names) ⇒ Object



43
44
45
46
# File 'lib/bundle_update_interactive/report.rb', line 43

def expand_gems_with_exact_dependencies(*gem_names)
  gem_names.flatten!
  gem_names.flat_map { |name| [name, *current_lockfile[name].exact_dependencies] }.uniq
end

#scan_for_vulnerabilities!Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bundle_update_interactive/report.rb', line 48

def scan_for_vulnerabilities!
  return false if outdated_gems.empty?

  Bundler::Audit::Database.update!(quiet: true)
  audit_report = Bundler::Audit::Scanner.new.report
  vulnerable_gem_names = Set.new(audit_report.vulnerable_gems.map(&:name))

  outdated_gems.each do |name, gem|
    gem.vulnerable = (vulnerable_gem_names & [name, *current_lockfile[name].exact_dependencies]).any?
  end
  true
end

#updateable_gemsObject



37
38
39
40
41
# File 'lib/bundle_update_interactive/report.rb', line 37

def updateable_gems
  @updateable_gems ||= outdated_gems.reject do |name, _|
    current_lockfile[name].exact_dependency?
  end.freeze
end