Class: Breezer::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/breezer/command.rb

Overview

Used to parse the command line args and run the breezer accordingly

Class Method Summary collapse

Class Method Details

.get_gemfile_file(args) ⇒ Object



32
33
34
35
36
# File 'lib/breezer/command.rb', line 32

def get_gemfile_file(args)
  gemfile_dir = args.first || '.'

  File.directory?(gemfile_dir) ? File.join(gemfile_dir, 'Gemfile') : gemfile_dir
end

.parse_options(options = { debug: false }) ⇒ Object

rubocop:todo Metrics/AbcSize



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/breezer/command.rb', line 38

def parse_options(options = { debug: false }) # rubocop:todo Metrics/AbcSize
  OptionParser.new do |parser| # rubocop:todo Metrics/BlockLength
    parser.banner = 'Usage: breezer DIR [options]'

    parser.on('-l', '--level LEVEL', 'Set the minimum level of gem updates (default: patch).'\
                                    ' Set to "exact" to lock gems versions (not recommended).') do |v|
      unless %w[major minor patch exact].include?(v)
        raise InvalidLevelException, "Error: level must be one of: 'major', 'minor', 'patch' or 'exact', #{v} given."
      end

      options[:level] = v
    end

    parser.on('-L', '--lockfile NAME', 'Use different lockfile (default: Gemfile.lock)') do |v|
      options[:lockfile_file] = v
    end

    parser.on('-d', '--dry-run', 'Print the updated Gemfile instead of writing it') do
      options[:dry] = true
    end

    parser.on('-o', '--output FILE', 'The output file (default: Gemfile)') do |v|
      options[:output] = v
    end

    parser.on('-c', '--check', 'Check that all gems have version constraints') do |_v|
      options[:check] = true
    end

    parser.on('-h', '--help', 'Show this help message') do
      puts parser
      exit(0)
    end

    parser.on('-v', '--version', 'Show version') do
      puts Breezer::VERSION
      exit(0)
    end
  end.parse!
end

.run!(args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/breezer/command.rb', line 12

def run!(args)
  gemfile_file = get_gemfile_file(args)

  options = parse_options({ debug: false }.merge(args))
  lockfile_file = options[:lockfile_file] || "#{gemfile_file}.lock"

  unless File.file?(gemfile_file)
    puts "Unable to find a Gemfile (searched in #{gemfile_file})"
    raise NoGemfileException, "Unable to find a Gemfile (searched in #{gemfile_file})"
  end

  unless File.file?(lockfile_file)
    puts "Unable to find a Lockfile (Gemfile.lock). If you don't have a Gemfile.lock yet, "\
        "you can run 'bundle install' first. (searched in #{lockfile_file})"
    raise NoLockfileException, "Unable to find a Lockfile (Gemfile.lock) (searched in #{lockfile_file})"
  end

  Breezer.freeze!(gemfile_file, lockfile_file, options)
end