class GemUpdater::Gemfile

Gemfile is responsible for handling ‘Gemfile`

Attributes

changes[RW]
new_spec_set[R]
old_spec_set[R]

Public Class Methods

new() click to toggle source
# File lib/gem_updater/gemfile.rb, line 12
def initialize
  @changes = {}
end

Public Instance Methods

compute_changes() click to toggle source

Compute the diffs between two ‘Gemfile.lock`.

@return [Hash] gems for which there are differences.

# File lib/gem_updater/gemfile.rb, line 25
def compute_changes
  spec_sets_diff!

  old_spec_set.each do |old_gem|
    updated_gem = new_spec_set.find { |new_gem| new_gem.name == old_gem.name }
    next unless updated_gem && old_gem.version != updated_gem.version

    fill_changes(old_gem, updated_gem)
  end
end
update!(gems) click to toggle source

Run ‘bundle update` to update gems.

# File lib/gem_updater/gemfile.rb, line 17
def update!(gems)
  Bundler.ui.warn 'Updating gems...'
  Bundler::CLI.start(['update'] + gems)
end

Private Instance Methods

fill_changes(old_gem, updated_gem) click to toggle source

Add changes to between two versions of a gem

@param old_gem [Bundler::LazySpecification] @param new_gem [Bundler::LazySpecification]

# File lib/gem_updater/gemfile.rb, line 64
def fill_changes(old_gem, updated_gem)
  changes[old_gem.name] = {
    versions: {
      old: old_gem.version.to_s, new: updated_gem.version.to_s
    },
    source: updated_gem.source
  }
end
reinitialize_spec_set!() click to toggle source

Calling ‘Bundler.locked_gems` before or after a `bundler update` will return the same result. Use a hacky way to tell bundle we want to parse the new `Gemfile.lock`

# File lib/gem_updater/gemfile.rb, line 55
def reinitialize_spec_set!
  Bundler.remove_instance_variable(:@locked_gems)
  Bundler.remove_instance_variable(:@definition)
end
spec_set() click to toggle source

Get the current spec set

@return [Array] array of Bundler::LazySpecification (including gem name, version and source)

# File lib/gem_updater/gemfile.rb, line 48
def spec_set
  Bundler.locked_gems.specs
end
spec_sets_diff!() click to toggle source

Get the two spec sets (before and after ‘bundle update`)

# File lib/gem_updater/gemfile.rb, line 39
def spec_sets_diff!
  @old_spec_set = spec_set
  reinitialize_spec_set!
  @new_spec_set = spec_set
end