60 lines
1.5 KiB
Ruby
Raw Normal View History

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
2017-05-21 00:15:56 +02:00
option "--fix", :fix, false
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def run
install_rubocop
cache_env = { "XDG_CACHE_HOME" => "#{HOMEBREW_CACHE}/style" }
2018-01-27 23:29:55 +01:00
hide_warnings = debug? ? [] : [ENV["HOMEBREW_RUBY_PATH"], "-W0", "-S"]
system(cache_env, *hide_warnings, "rubocop", *rubocop_args, "--", *cask_paths)
2017-05-20 03:46:52 +02:00
raise CaskError, "style check failed" unless $CHILD_STATUS.success?
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-21 00:15:56 +02:00
@cask_paths ||= if args.empty?
2018-04-14 11:32:29 +02:00
Tap.map(&:cask_dir).select(&:directory?)
2017-05-21 00:15:56 +02:00
elsif args.any? { |file| File.exist?(file) }
args
else
casks.map(&:sourcefile_path)
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
2018-01-29 07:45:30 +10:00
fix? ? autocorrect_args : normal_args
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 default_args
[
"--require", "rubocop-cask",
"--force-default-config",
2018-01-29 07:45:30 +10:00
"--format", "simple"
]
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
2018-01-29 07:45:30 +10:00
def normal_args
default_args + ["--parallel"]
end
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