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, options = {}) ⇒ Calculator

Returns a new instance of Calculator.

Raises:

  • (ArgumentError)


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

def initialize(gemfile_path, lockfile_path, options = {})
  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)

  skiplist = options.fetch(:skiplist, [])
  @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



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

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

#github_gemsObject



53
54
55
56
57
# File 'lib/bundler/stats/calculator.rb', line 53

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

#statsObject Also known as: to_h



38
39
40
41
42
# File 'lib/bundler/stats/calculator.rb', line 38

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

#summarize(target) ⇒ Object



26
27
28
29
30
# File 'lib/bundler/stats/calculator.rb', line 26

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

#summaryObject



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

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

#unpinned_gemsObject



59
60
61
# File 'lib/bundler/stats/calculator.rb', line 59

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

#versions(target) ⇒ Object



32
33
34
35
36
# File 'lib/bundler/stats/calculator.rb', line 32

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