2016-08-18 22:11:42 +03:00
|
|
|
require "English"
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
module Hbc
|
|
|
|
class CLI
|
|
|
|
class Style < Base
|
|
|
|
def self.help
|
|
|
|
"checks Cask style using RuboCop"
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def self.run(*args)
|
|
|
|
retval = new(args).run
|
|
|
|
raise CaskError, "style check failed" unless retval
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
attr_reader :args
|
|
|
|
def initialize(args)
|
|
|
|
@args = args
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def run
|
|
|
|
install_rubocop
|
|
|
|
system "rubocop", *rubocop_args, "--", *cask_paths
|
|
|
|
$CHILD_STATUS.success?
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def install_rubocop
|
2017-03-07 18:02:31 +01:00
|
|
|
capture_stderr do
|
2016-09-24 13:52:43 +02:00
|
|
|
begin
|
2017-05-07 17:28:39 +01:00
|
|
|
Homebrew.install_gem_setup_path! "rubocop-cask", HOMEBREW_RUBOCOP_CASK_VERSION, "rubocop"
|
2016-09-24 13:52:43 +02:00
|
|
|
rescue SystemExit
|
2016-08-26 16:04:47 +02:00
|
|
|
raise CaskError, Tty.strip_ansi($stderr.string).chomp.sub(/\AError: /, "")
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def cask_paths
|
|
|
|
@cask_paths ||= if cask_tokens.empty?
|
2016-10-14 20:11:33 +02:00
|
|
|
Hbc.all_tapped_cask_dirs
|
|
|
|
elsif cask_tokens.any? { |file| File.exist?(file) }
|
|
|
|
cask_tokens
|
|
|
|
else
|
2017-03-13 01:01:17 +01:00
|
|
|
cask_tokens.map { |token| CaskLoader.path(token) }
|
2016-10-14 20:11:33 +02:00
|
|
|
end
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def cask_tokens
|
|
|
|
@cask_tokens ||= self.class.cask_tokens_from(args)
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def rubocop_args
|
|
|
|
fix? ? autocorrect_args : default_args
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def default_args
|
2016-10-18 16:10:36 +02:00
|
|
|
[
|
|
|
|
"--require", "rubocop-cask",
|
2017-02-12 11:28:39 +01:00
|
|
|
"--force-default-config",
|
|
|
|
"--force-exclusion",
|
|
|
|
"--format", "simple"
|
2016-10-18 16:10:36 +02:00
|
|
|
]
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def autocorrect_args
|
|
|
|
default_args + ["--auto-correct"]
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def fix?
|
2016-10-14 20:03:34 +02:00
|
|
|
args.any? { |arg| arg =~ /--(fix|(auto-?)?correct)/ }
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
end
|