Class: RubyAppraiser::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-appraiser/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = ARGV) ⇒ CLI

Returns a new instance of CLI.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
78
79
# File 'lib/ruby-appraiser/cli.rb', line 9

def initialize(args = ARGV)
  @argv = ARGV.dup
  args = @argv.dup # needed for --git-hook
  @options = {}
  adapters = Set.new

  OptionParser.new do |opts|
    opts.banner = "Usage: #{File::basename($0)} [inspector...] [options]"
    opts.on('-v', '--[no-]verbose', 'Run verbosely') do |verbose|
      @options[:verbose] = verbose
    end
    opts.on('--list', 'List available adapters') do |list|
      puts available_adapters
      exit 1
    end
    opts.on('--silent', 'Silence output') do |silent|
      @options[:silent] = true
    end
    opts.on('--mode=MODE',
            'Set the mode. ' +
            '[staged,authored,touched,all]') do |mode|
      @options[:mode] = mode
    end
    opts.on('--git-hook', 
            'Output a git hook with current comand to STDOUT') do
      command = $0
      if (`which #{File::basename(command)}`).chomp == command
        command = File::basename(command)
      end
      be = 'bundle exec'
      hook_args = @argv.select { |arg| arg != '--git-hook' }

      indented_git_hook = "        #!/bin/bash\n        echo -e \"\\\\033[0;36mRuby Appraiser: running\\\\033[0m\"\n\n        bundle exec \#{command} \#{hook_args.join(' ')}\n\n        result_code=$?\n        if [ $result_code -gt \"0\" ]; then\n          echo -en \"\\\\033[0;31m\" # RED\n          echo \"[\u2718] Ruby Appraiser found newly-created defects and \"\n          echo \"    has blocked your commit.\"\n          echo \"    Fix the defects and commit again.\"\n          echo \"    To bypass, commit again with --no-verify.\"\n          echo -en \"\\\\033[0m\" # RESET\n          exit $result_code\n        else\n          echo -en \"\\\\033[0;32m\" # GREEN\n          echo \"[\u2714] Ruby Appraiser ok\"\n          echo -en \"\\\\033[0m\" #RESET\n        fi\n      EOGITHOOK\n\n      indent = indented_git_hook.scan(/^\\s*/).map(&:length).min\n      puts indented_git_hook.lines.map {|l| l[indent..-1]}.join\n      exit 1\n    end\n    opts.on('--all', 'Run all available adapters.') do\n      adapters += available_adapters\n    end\n  end.parse!(args)\n\n  @appraisal = RubyAppraiser::Appraisal.new(options)\n  adapters += args\n  adapters.each do |adapter|\n    @appraisal.add_adapter(adapter) or\n      raise \"Unknown adapter '\#{adapter}'\"\n  end\n  Dir::chdir((`git rev-parse --show-toplevel`).chomp)\nend\n"

Instance Method Details

#available_adaptersObject



94
95
96
# File 'lib/ruby-appraiser/cli.rb', line 94

def available_adapters
  @available_adapters ||= RubyAppraiser::Adapter::all.map(&:adapter_type)
end

#optionsObject



81
82
83
# File 'lib/ruby-appraiser/cli.rb', line 81

def options
  @options.dup
end

#runObject



85
86
87
88
89
90
91
92
# File 'lib/ruby-appraiser/cli.rb', line 85

def run
  @appraisal.run!
  puts @appraisal unless @options[:silent]
  puts @appraisal.summary if @options[:verbose]
  @appraisal.success?
rescue Object
  puts "#{@appraisal.class.name} caught #{$!} at #{$!.backtrace.first}."
end