class BundlerDiffgems::CLI

Constants

DEFAULT_OPTIONS

Public Class Methods

invoke(args = ARGV) click to toggle source
# File lib/bundler_diffgems/cli.rb, line 13
def self.invoke(args = ARGV)
  new(args).invoke
end
new(args = []) click to toggle source
# File lib/bundler_diffgems/cli.rb, line 17
def initialize(args = [])
  @args = args
end

Public Instance Methods

invoke() click to toggle source
# File lib/bundler_diffgems/cli.rb, line 21
def invoke
  parse_options!
  set_access_token!

  gems = GemsComparator.compare(before_lockfile, after_lockfile)
  output = formatter.new.render(gems)
  output = JSON.dump(output) if @escape_json
  puts output
rescue => e
  puts e.inspect
end

Private Instance Methods

after_lockfile() click to toggle source
# File lib/bundler_diffgems/cli.rb, line 45
def after_lockfile
  File.read(file_name)
end
before_lockfile() click to toggle source
# File lib/bundler_diffgems/cli.rb, line 39
def before_lockfile
  `git show #{commit}:#{file_name}`.tap do
    raise unless $?.success? # rubocop:disable Style/SpecialGlobalVars
  end
end
commit() click to toggle source
# File lib/bundler_diffgems/cli.rb, line 83
def commit
  @commit || DEFAULT_OPTIONS[:commit]
end
file_name() click to toggle source
# File lib/bundler_diffgems/cli.rb, line 35
def file_name
  @file_name ||= File.exist?('gems.locked') ? 'gems.locked' : 'Gemfile.lock'
end
formatter() click to toggle source
# File lib/bundler_diffgems/cli.rb, line 87
def formatter
  format = @format || DEFAULT_OPTIONS[:format]
  BundlerDiffgems.formatters[format] || Formatter::Default
end
parse_options!() click to toggle source
# File lib/bundler_diffgems/cli.rb, line 49
def parse_options! # rubocop:disable Metrics/MethodLength
  opt = OptionParser.new
  opt.banner = 'Usage: bundle diffgems [options]'
  opt.on('-c', '--commit COMMIT', 'Specify a commit') { |val| @commit = val }
  formatter_desc = [
    'Choose a formatter',
    '  default',
    '  md_table'
  ]
  opt.on('-f', '--format FORMATTER', *formatter_desc) { |val| @format = val.to_sym }
  opt.on('--escape-json', 'Escape output as a JSON string') do |val|
    @escape_json = val
  end
  opt.on('-v', '--version', 'Display the version') do
    puts BundlerDiffgems::VERSION
    exit
  end
  options = opt.parse(@args)
  @commit ||= options.shift
end
set_access_token!() click to toggle source
# File lib/bundler_diffgems/cli.rb, line 70
def set_access_token!
  return if GemsComparator.config.client.access_token
  hub_config_path = "#{ENV['HOME']}/.config/hub"
  return unless File.exist?(hub_config_path)

  yaml = YAML.load_file(hub_config_path)
  oauth_token = yaml.dig('github.com', 0, 'oauth_token')
  return if oauth_token.nil?
  GemsComparator.configure do |config|
    config.client = Octokit::Client.new(access_token: oauth_token)
  end
end