MaximeD/gem_updater

View on GitHub
bin/gem_update

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

# frozen_string_literal: true

require 'optparse'

# Exit cleanly from an early interrupt
Signal.trap('INT') { exit 1 }

$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
require 'gem_updater'

Bundler.ui = Bundler::UI::Shell.new

options = {}
OptionParser.new do |opts|
  opts.on('-c', '--commit', 'Auto commit') do |v|
    options[:commit] = v
  end
end.parse!

gems = GemUpdater::Updater.new
gems.update!(ARGV)

if gems.gemfile.changes.any?
  if options[:commit]
    require 'tempfile'
    file = Tempfile.new('gem_updater')
    file.write "UPDATE gems\n\n"
    file.write gems.format_diff.join
    file.close

    gemfile = Bundler.default_gemfile.to_s

    system %(
      git add #{gemfile} #{gemfile}.lock \
      && git commit -t #{file.path} --allow-empty-message
    )

    file.unlink
  else
    Bundler.ui.confirm "\nHere are your changes:"
    Bundler.ui.confirm '------------------------'
    gems.output_diff
  end
else
  Bundler.ui.confirm "\nCongratulations, your Gemfile was already up-to-date!"
end