Class: Gry::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/gry/cli.rb

Constant Summary collapse

CACHE_DIR =
Pathname(ENV['XDG_CACHE_HOME'] || '~/.cache').expand_path / 'gry'

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



5
6
7
# File 'lib/gry/cli.rb', line 5

def initialize(argv)
  @argv = argv
end

Instance Method Details

#run(writer) ⇒ Object



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
# File 'lib/gry/cli.rb', line 9

def run(writer)
  opt = Option.new(@argv)
  if opt.version
    rubocop_version, = *Open3.capture3('rubocop', '--verbose-version')
    writer.puts "gry #{VERSION} (RuboCop #{rubocop_version.chomp})"
    return
  end

  cops = opt.args.empty? ? RubocopAdapter.configurable_cops : opt.args
  if opt.fast
    cops.reject!{|cop| cop == 'Layout/AlignHash'}
  end

  bills = (opt.cache && restore_cache(cops)) || begin
    pilot_study = Gry::PilotStudy.new(cops, process: opt.process)
    pilot_study.analyze.tap do |b|
      save_cache(b, cops)
    end
  end
  if opt.raw
    writer.puts JSON.generate(bills)
    return
  end

  congress = Congress.new(
    max_count: opt.max_count,
    min_difference: opt.min_difference,
    metrics_percentile: opt.metrics_percentile,
  )
  laws = bills.map do |cop_name, bill|
    congress.discuss(cop_name, bill)
  end

  fmt = Formatter.new(display_disabled_cops: opt.display_disabled_cops)
  writer.puts fmt.format(laws)
end