-
Notifications
You must be signed in to change notification settings - Fork 245
/
cli.rb
102 lines (80 loc) · 2.93 KB
/
cli.rb
1
2
3
4
5
6
7
8
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require 'thor'
module Pronto
class CLI < Thor
require 'pronto'
require 'pronto/version'
class << self
def is_thor_reserved_word?(word, type)
return false if word == 'run'
super
end
end
desc 'run [PATH]', 'Run Pronto'
method_option :'exit-code',
type: :boolean,
desc: 'Exits with non-zero code if there were any warnings/errors.'
method_option :commit,
type: :string,
aliases: '-c',
desc: 'Commit for the diff'
method_option :unstaged,
type: :boolean,
aliases: ['-i', '--index'],
desc: 'Analyze changes made, but not in git staging area'
method_option :staged,
type: :boolean,
desc: 'Analyze changes in git staging area'
method_option :workdir,
type: :boolean,
aliases: ['-w'],
desc: 'Analyze both staged and unstaged changes'
method_option :runner,
type: :array,
default: [],
aliases: '-r',
desc: 'Run only the passed runners'
method_option :formatters,
type: :array,
default: ['text'],
aliases: ['formatter', '-f'],
desc: "Pick output formatters. Available: #{::Pronto::Formatter.names.join(', ')}"
def run(path = '.')
path = File.expand_path(path)
gem_names = options[:runner].any? ? options[:runner] : ::Pronto::GemNames.new.to_a
gem_names.each do |gem_name|
require "pronto/#{gem_name}"
end
formatters = ::Pronto::Formatter.get(options[:formatters])
commit_options = %i[workdir staged unstaged index]
commit = commit_options.find { |o| options[o] } || options[:commit]
repo_workdir = ::Rugged::Repository.discover(path).workdir
relative = path.sub(repo_workdir, '')
messages = Dir.chdir(repo_workdir) do
file = relative.length != path.length ? relative : nil
::Pronto.run(commit, '.', formatters, file)
end
if options[:'exit-code']
error_messages_count = messages.count { |m| m.level != :info }
exit(error_messages_count)
end
rescue Rugged::RepositoryError
puts '"pronto" must be run from within a git repository or must be supplied the path to a git repository'
rescue Pronto::Error => e
$stderr.puts "Pronto errored: #{e.message}"
end
desc 'list', 'Lists pronto runners that are available to be used'
def list
puts ::Pronto::GemNames.new.to_a
end
desc 'version', 'Display version'
map %w[-v --version] => :version
def version
puts Version::STRING
end
desc 'verbose-version', 'Display verbose version'
map %w[-V --verbose-version] => :verbose_version
def verbose_version
puts Version.verbose
end
end
end