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



9
10
11
# File 'lib/ruby-appraiser/cli.rb', line 9

def initialize(args = ARGV)
  @argv = ARGV.dup
end

Instance Method Details

#available_adaptersObject



105
106
107
# File 'lib/ruby-appraiser/cli.rb', line 105

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

#configureObject



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
80
81
82
83
84
# File 'lib/ruby-appraiser/cli.rb', line 13

def configure
  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('-t', '--trace', 'Enable backtrace on failure')
    opts.on('--silent', 'Silence output') do |silent|
      @options[:silent] = true
    end
    opts.on('--mode=MODE',
            'Set the mode. ' +
            '[staged,authored,touched,last,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  git_base = Git.run('rev-parse', '--show-toplevel', &:read).chomp\n  Dir.chdir(git_base)\nend\n"

#optionsObject



86
87
88
# File 'lib/ruby-appraiser/cli.rb', line 86

def options
  @options.dup
end

#runObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ruby-appraiser/cli.rb', line 90

def run
  self.configure
  @appraisal.run!
  puts @appraisal unless @options[:silent]
  puts @appraisal.summary if @options[:verbose]
  @appraisal.success?
rescue Exception
  $stderr.puts $!.message
  unless (@argv & ['--trace','-t']).empty?
    $stderr.puts $!.inspect
    $stderr.puts $!.backtrace
  end
  nil
end