2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-10-04 09:47:54 +02:00
|
|
|
require "json"
|
|
|
|
|
2018-09-06 08:29:14 +02:00
|
|
|
module Cask
|
2018-09-04 08:45:48 +01:00
|
|
|
class Cmd
|
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
|
|
|
|
2019-10-04 09:47:54 +02:00
|
|
|
def self.rubocop(*paths, auto_correct: false, debug: false, json: false)
|
|
|
|
Homebrew.install_bundler_gems!
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2017-05-28 16:59:53 +01:00
|
|
|
cache_env = { "XDG_CACHE_HOME" => "#{HOMEBREW_CACHE}/style" }
|
2019-10-04 09:47:54 +02:00
|
|
|
hide_warnings = debug ? [] : [ENV["HOMEBREW_RUBY_PATH"], "-W0", "-S"]
|
|
|
|
|
|
|
|
args = [
|
|
|
|
"--force-exclusion",
|
|
|
|
"--config", "#{HOMEBREW_LIBRARY}/.rubocop_cask.yml"
|
|
|
|
]
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2019-10-04 09:47:54 +02:00
|
|
|
if json
|
|
|
|
args << "--format" << "json"
|
|
|
|
else
|
|
|
|
if auto_correct
|
|
|
|
args << "--auto-correct"
|
|
|
|
else
|
|
|
|
args << "--debug" if debug
|
|
|
|
args << "--parallel"
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2019-10-04 09:47:54 +02:00
|
|
|
|
|
|
|
args << "--format" << "simple"
|
|
|
|
args << "--color" if Tty.color?
|
|
|
|
end
|
|
|
|
|
|
|
|
executable, *args = [*hide_warnings, "rubocop", *args, "--", *paths]
|
|
|
|
|
|
|
|
result = Dir.mktmpdir do |tmpdir|
|
|
|
|
system_command executable, args: args, chdir: tmpdir, env: cache_env,
|
|
|
|
print_stdout: !json, print_stderr: !json
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2019-10-04 09:47:54 +02:00
|
|
|
|
|
|
|
result.assert_success! unless (0..1).cover?(result.exit_status)
|
|
|
|
|
|
|
|
return JSON.parse(result.stdout) if json
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
option "--fix", :fix, false
|
|
|
|
|
|
|
|
def run
|
|
|
|
result = self.class.rubocop(*cask_paths, auto_correct: fix?, debug: debug?)
|
|
|
|
raise CaskError, "Style check failed." unless result.status.success?
|
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?
|
2019-09-22 11:41:46 -04:00
|
|
|
Tap.map(&:cask_dir).select(&:directory?).concat(test_cask_paths)
|
2017-05-21 00:15:56 +02:00
|
|
|
elsif args.any? { |file| File.exist?(file) }
|
2018-10-17 01:50:39 +02:00
|
|
|
args.map { |path| Pathname(path).expand_path }
|
2016-10-14 20:11:33 +02:00
|
|
|
else
|
2017-06-13 17:14:01 +02:00
|
|
|
casks.map(&:sourcefile_path)
|
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
|
|
|
|
2019-09-22 11:41:46 -04:00
|
|
|
def test_cask_paths
|
|
|
|
[
|
|
|
|
Pathname.new("#{HOMEBREW_LIBRARY}/Homebrew/test/support/fixtures/cask/Casks"),
|
|
|
|
Pathname.new("#{HOMEBREW_LIBRARY}/Homebrew/test/support/fixtures/third-party/Casks"),
|
|
|
|
]
|
|
|
|
end
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
end
|