66 lines
1.5 KiB
Ruby
Raw Normal View History

2016-08-18 22:11:42 +03:00
require "English"
2016-09-24 13:52:43 +02:00
module Hbc
class CLI
2017-05-20 19:08:03 +02:00
class Style < AbstractCommand
2016-09-24 13:52:43 +02:00
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
attr_reader :args
2017-05-20 03:46:52 +02:00
def initialize(*args)
@cask_tokens = self.class.cask_tokens_from(args)
@fix = args.any? { |arg| arg =~ /^--(fix|(auto-?)?correct)$/ }
end
def fix?
@fix
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 run
install_rubocop
system "rubocop", *rubocop_args, "--", *cask_paths
2017-05-20 03:46:52 +02:00
raise CaskError, "style check failed" unless $CHILD_STATUS.success?
true
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 install_rubocop
2017-03-07 18:02:31 +01:00
capture_stderr do
2016-09-24 13:52:43 +02:00
begin
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
2017-05-20 03:46:52 +02:00
@cask_paths ||= if @cask_tokens.empty?
Hbc.all_tapped_cask_dirs
2017-05-20 03:46:52 +02:00
elsif @cask_tokens.any? { |file| File.exist?(file) }
@cask_tokens
else
2017-05-20 03:46:52 +02:00
@cask_tokens.map { |token| CaskLoader.path(token) }
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 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
[
"--require", "rubocop-cask",
"--force-default-config",
"--force-exclusion",
"--format", "simple"
]
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
end
2016-08-18 22:11:42 +03:00
end
end