brew/Library/Homebrew/style.rb

277 lines
8.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "shellwords"
module Homebrew
2020-08-19 07:26:09 +02:00
# Helper module for running RuboCop.
#
# @api private
module Style
module_function
# Checks style for a list of files, printing simple RuboCop output.
# Returns true if violations were found, false otherwise.
2019-10-04 23:39:11 +02:00
def check_style_and_print(files, **options)
check_style_impl(files, :print, **options)
end
# Checks style for a list of files, returning results as a RubocopResults
# object parsed from its JSON output.
2019-10-04 23:39:11 +02:00
def check_style_json(files, **options)
check_style_impl(files, :json, **options)
end
def check_style_impl(files, output_type,
fix: false,
except_cops: nil, only_cops: nil,
display_cop_names: false,
debug: false, verbose: false)
raise ArgumentError, "Invalid output type: #{output_type.inspect}" unless [:print, :json].include?(output_type)
shell_files, ruby_files =
Array(files).map(&method(:Pathname))
.partition { |f| f.realpath == HOMEBREW_BREW_FILE.realpath || f.extname == ".sh" }
rubocop_result = if shell_files.any? && ruby_files.none?
output_type == :json ? [] : true
else
run_rubocop(ruby_files, output_type,
fix: fix,
except_cops: except_cops, only_cops: only_cops,
display_cop_names: display_cop_names,
debug: debug, verbose: verbose)
end
shellcheck_result = if ruby_files.any? && shell_files.none?
output_type == :json ? [] : true
else
run_shellcheck(shell_files, output_type)
end
if output_type == :json
RubocopResults.new(rubocop_result + shellcheck_result)
else
rubocop_result && shellcheck_result
end
end
def run_rubocop(files, output_type,
fix: false, except_cops: nil, only_cops: nil, display_cop_names: false,
debug: false, verbose: false)
Homebrew.install_bundler_gems!
require "rubocop"
require "rubocops"
args = %w[
--force-exclusion
]
2020-03-13 21:15:06 +00:00
args << if fix
"--auto-correct"
else
2020-03-13 21:15:06 +00:00
"--parallel"
end
args += ["--extra-details"] if verbose
args += ["--display-cop-names"] if display_cop_names || verbose
2018-07-08 20:08:51 +02:00
2019-10-04 23:39:11 +02:00
if except_cops
except_cops.map! { |cop| RuboCop::Cop::Cop.registry.qualified_cop_name(cop.to_s, "") }
cops_to_exclude = except_cops.select do |cop|
RuboCop::Cop::Cop.registry.names.include?(cop) ||
RuboCop::Cop::Cop.registry.departments.include?(cop.to_sym)
end
args << "--except" << cops_to_exclude.join(",") unless cops_to_exclude.empty?
2019-10-04 23:39:11 +02:00
elsif only_cops
only_cops.map! { |cop| RuboCop::Cop::Cop.registry.qualified_cop_name(cop.to_s, "") }
cops_to_include = only_cops.select do |cop|
RuboCop::Cop::Cop.registry.names.include?(cop) ||
RuboCop::Cop::Cop.registry.departments.include?(cop.to_sym)
end
2019-10-04 23:39:11 +02:00
odie "RuboCops #{only_cops.join(",")} were not found" if cops_to_include.empty?
args << "--only" << cops_to_include.join(",")
end
has_non_formula = files.any? do |file|
File.expand_path(file).start_with? HOMEBREW_LIBRARY_PATH
end
if files.any? && !has_non_formula
config = if files.first && File.exist?("#{files.first}/spec")
HOMEBREW_LIBRARY/".rubocop_rspec.yml"
else
HOMEBREW_LIBRARY/".rubocop.yml"
end
args << "--config" << config
end
if files.blank?
args << HOMEBREW_LIBRARY_PATH
else
args += files
end
cache_env = { "XDG_CACHE_HOME" => "#{HOMEBREW_CACHE}/style" }
case output_type
when :print
args << "--debug" if debug
args << "--format" << "simple" if files.present?
args << "--color" if Tty.color?
2018-09-17 02:45:00 +02:00
system cache_env, "rubocop", *args
$CHILD_STATUS.success?
when :json
result = system_command "rubocop", args: ["--format", "json", *args], env: cache_env
json = json_result!(result)
json["files"]
end
end
def run_shellcheck(files, output_type)
shellcheck = which("shellcheck")
shellcheck ||= which("shellcheck", ENV["HOMEBREW_PATH"])
shellcheck ||= begin
ohai "Installing `shellcheck` for shell style checks..."
safe_system HOMEBREW_BREW_FILE, "install", "shellcheck"
which("shellcheck") || which("shellcheck", ENV["HOMEBREW_PATH"])
end
if files.empty?
files = [
HOMEBREW_BREW_FILE,
# TODO: HOMEBREW_REPOSITORY/"completions/bash/brew",
*Pathname.glob("#{HOMEBREW_LIBRARY}/Homebrew/*.sh"),
*Pathname.glob("#{HOMEBREW_LIBRARY}/Homebrew/cmd/*.sh"),
*Pathname.glob("#{HOMEBREW_LIBRARY}/Homebrew/utils/*.sh"),
]
end
args = ["--shell=bash", "--", *files] # TODO: Add `--enable=all` to check for more problems.
case output_type
when :print
system shellcheck, "--format=tty", *args
$CHILD_STATUS.success?
when :json
result = system_command shellcheck, args: ["--format=json1", *args]
json = json_result!(result)
# Convert to same format as RuboCop offenses.
json["comments"].group_by { |v| v["file"] }
.map do |k, v|
{
"path" => k,
"offenses" => v.map do |o|
o.delete("file")
o["cop_name"] = "SC#{o.delete("code")}"
level = o.delete("level")
o["severity"] = { "style" => "refactor", "info" => "convention" }.fetch(level, level)
line = o.delete("line")
column = o.delete("column")
o["corrected"] = false
o["correctable"] = o.delete("fix").present?
o["location"] = {
"start_line" => line,
"start_column" => column,
"last_line" => o.delete("endLine"),
"last_column" => o.delete("endColumn"),
"line" => line,
"column" => column,
}
o
end,
}
end
end
end
def json_result!(result)
# An exit status of 1 just means violations were found; other numbers mean
# execution errors.
# JSON needs to be at least 2 characters.
result.assert_success! if !(0..1).cover?(result.status.exitstatus) || result.stdout.length < 2
JSON.parse(result.stdout)
end
2020-08-19 07:26:09 +02:00
# Result of a RuboCop run.
class RubocopResults
def initialize(files)
@file_offenses = {}
files.each do |f|
next if f["offenses"].empty?
2018-09-17 02:45:00 +02:00
file = File.realpath(f["path"])
@file_offenses[file] = f["offenses"].map { |x| RubocopOffense.new(x) }
end
end
def file_offenses(path)
@file_offenses.fetch(path.to_s, [])
end
end
2020-08-19 07:26:09 +02:00
# A RuboCop offense.
class RubocopOffense
attr_reader :severity, :message, :corrected, :location, :cop_name
def initialize(json)
@severity = json["severity"]
@message = json["message"]
@cop_name = json["cop_name"]
@corrected = json["corrected"]
@location = RubocopLineLocation.new(json["location"])
end
def severity_code
@severity[0].upcase
end
def corrected?
@corrected
end
def correction_status
"[Corrected] " if corrected?
end
2019-10-04 23:39:11 +02:00
def to_s(display_cop_name: false)
if display_cop_name
"#{severity_code}: #{location.to_short_s}: #{cop_name}: " \
"#{Tty.green}#{correction_status}#{Tty.reset}#{message}"
else
"#{severity_code}: #{location.to_short_s}: #{Tty.green}#{correction_status}#{Tty.reset}#{message}"
end
end
end
2020-08-19 07:26:09 +02:00
# Source location of a RuboCop offense.
class RubocopLineLocation
attr_reader :line, :column, :length
def initialize(json)
@line = json["line"]
@column = json["column"]
@length = json["length"]
end
def to_s
"#{line}: col #{column} (#{length} chars)"
end
def to_short_s
"#{line}: col #{column}"
end
end
end
end