77 lines
2.1 KiB
Ruby
Raw Normal View History

# 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
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?
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) }
args.map { |path| Pathname(path).expand_path }
else
casks.map(&:sourcefile_path)
end
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03: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