class SafeBundleUpdate

Public Class Methods

start(*commands) click to toggle source
# File lib/safe-bundle-update.rb, line 4
def self.start(*commands)
  excludes = []
  gems = fetch_gems
  puts "#{gems.size} gems to be updated"

  while gem = gems.detect { |name| !excludes.include?(name) }
    puts "Updating #{gem}..."
    next excludes << gem unless try_system("bundle update #{gem}", gem)
    if `git status | grep "nothing to commit, working tree clean" | wc -l`.strip.to_i == 1
      excludes << gem
      puts "Nothing changed".red
      next
    end
    next excludes << gem unless commands.all? { |cmd| try_system(cmd, gem, color: :yellow, null: false) }
    try_system("git add .", gem)
    try_system("git commit -m 'Updating #{gem}'", gem, color: :green)
    gems = fetch_gems
  end
end

Private Class Methods

fetch_gems() click to toggle source
# File lib/safe-bundle-update.rb, line 48
def fetch_gems
  `bundle outdated`.lines.grep(/\*/).map { |l| l.split[1] }
end
try_system(cmd, gem, color: nil, null: !ENV['SHOW_OUTPUT']) click to toggle source
# File lib/safe-bundle-update.rb, line 28
def try_system(cmd, gem, color: nil, null: !ENV['SHOW_OUTPUT'])
  puts "Running `#{cmd}` for #{gem}"

  ok = system(cmd, out: File::NULL)

  if !ok
    puts "`#{cmd}` failed, reverting and excluding".red
    system("#{cmd} > /dev/null") if !null
    system("git add Gemfile**")
    system("git checkout Gemfile")
    system("git checkout Gemfile.lock")
    false
  elsif color
    puts "Ran `#{cmd}` for #{gem}".colorize(color)
    true
  else
    true
  end
end