Class: Bundler::Stats::Calculator

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/stats/calculator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gemfile_path, lockfile_path, skiplist = nil) ⇒ Calculator

Returns a new instance of Calculator.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bundler/stats/calculator.rb', line 8

def initialize(gemfile_path, lockfile_path, skiplist = nil)
  raise ArgumentError unless File.readable?(lockfile_path)
  raise ArgumentError unless File.readable?(gemfile_path)

  @gemfile = Bundler::Dsl.new
  @gemfile.eval_gemfile(gemfile_path)
  @gemfile = @gemfile.dependencies
  raise ArgumentError, "Couldn't parse Gemfile at #{gemfile_path.realdirpath}" unless @gemfile

  lock_contents = File.read(lockfile_path)
  @parser = Bundler::LockfileParser.new(lock_contents)

  @tree = Bundler::Stats::Tree.new(@parser, skiplist: skiplist)
  @remover = Bundler::Stats::Remover.new(@tree, @gemfile)
end

Instance Attribute Details

#gemfileObject (readonly)

Returns the value of attribute gemfile.



6
7
8
# File 'lib/bundler/stats/calculator.rb', line 6

def gemfile
  @gemfile
end

#parserObject (readonly)

Returns the value of attribute parser.



6
7
8
# File 'lib/bundler/stats/calculator.rb', line 6

def parser
  @parser
end

#removerObject (readonly)

Returns the value of attribute remover.



6
7
8
# File 'lib/bundler/stats/calculator.rb', line 6

def remover
  @remover
end

#treeObject (readonly)

Returns the value of attribute tree.



6
7
8
# File 'lib/bundler/stats/calculator.rb', line 6

def tree
  @tree
end

Instance Method Details

#gem_statsObject



61
62
63
64
65
66
# File 'lib/bundler/stats/calculator.rb', line 61

def gem_stats
  stats = @gemfile.map do |gem|
    @tree.summarize(gem.name)
  end
  stats.sort_by { |row| [row[:total_dependencies] * -1, row[:name]] }
end

#github_gemsObject



51
52
53
54
55
# File 'lib/bundler/stats/calculator.rb', line 51

def github_gems
  @gemfile.select do |dep|
    dep.source && dep.source.options.include?("github")
  end
end

#statsObject Also known as: to_h



36
37
38
39
40
# File 'lib/bundler/stats/calculator.rb', line 36

def stats
  { summary: summary,
    gems: gem_stats
  }
end

#summarize(target) ⇒ Object



24
25
26
27
28
# File 'lib/bundler/stats/calculator.rb', line 24

def summarize(target)
  @tree.summarize(target).merge({
    potential_removals: @remover.potential_removals(target)
  })
end

#summaryObject



43
44
45
46
47
48
49
# File 'lib/bundler/stats/calculator.rb', line 43

def summary
  { declared: @gemfile.count,
    unpinned: unpinned_gems.count,
    total: @parser.specs.count,
    github: github_gems.count,
  }
end

#unpinned_gemsObject



57
58
59
# File 'lib/bundler/stats/calculator.rb', line 57

def unpinned_gems
  @gemfile.reject { |dep| dep.specific? }
end

#versions(target) ⇒ Object



30
31
32
33
34
# File 'lib/bundler/stats/calculator.rb', line 30

def versions(target)
  @tree.version_requirements(target).merge({
    potential_removals: @remover.potential_removals(target)
  })
end