75 lines
1.7 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
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
RUBOCOP_CASK_VERSION = "~> 0.10.6".freeze
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def install_rubocop
Utils.capture_stderr do
begin
Homebrew.install_gem_setup_path! "rubocop-cask", RUBOCOP_CASK_VERSION, "rubocop"
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?
Hbc.all_tapped_cask_dirs
elsif cask_tokens.any? { |file| File.exist?(file) }
cask_tokens
else
cask_tokens.map { |token| Hbc.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 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
[
"--require", "rubocop-cask",
"--config", "/dev/null", # always use `rubocop-cask` default config
"--format", "simple",
"--force-exclusion"
]
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?
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