brew/Library/Homebrew/style.rb

389 lines
13 KiB
Ruby
Raw Normal View History

2023-03-15 14:29:15 -07:00
# typed: true
# frozen_string_literal: true
require "shellwords"
require "source_location"
require "system_command"
module Homebrew
2020-08-19 07:26:09 +02:00
# Helper module for running RuboCop.
module Style
extend SystemCommand::Mixin
# Checks style for a list of files, printing simple RuboCop output.
# Returns true if violations were found, false otherwise.
2023-03-15 14:29:15 -07:00
def self.check_style_and_print(files, **options)
success = check_style_impl(files, :print, **options)
if GitHub::Actions.env_set? && !success
check_style_json(files, **options).each do |path, offenses|
offenses.each do |o|
line = o.location.line
column = o.location.line
2024-03-07 16:20:20 +00:00
annotation = GitHub::Actions::Annotation.new(:error, o.message, file: path, line:, column:)
puts annotation if annotation.relevant?
end
end
end
success
end
# Checks style for a list of files, returning results as an {Offenses}
# object parsed from its JSON output.
2023-03-15 14:29:15 -07:00
def self.check_style_json(files, **options)
2019-10-04 23:39:11 +02:00
check_style_impl(files, :json, **options)
end
2023-03-15 14:29:15 -07:00
def self.check_style_impl(files, output_type,
2023-03-15 18:21:41 -07:00
fix: false,
except_cops: nil, only_cops: nil,
display_cop_names: false,
reset_cache: false,
debug: false, verbose: false)
raise ArgumentError, "Invalid output type: #{output_type.inspect}" if [:print, :json].exclude?(output_type)
ruby_files = T.let([], T::Array[Pathname])
shell_files = T.let([], T::Array[Pathname])
actionlint_files = T.let([], T::Array[Pathname])
Array(files).map(&method(:Pathname))
.each do |path|
case path.extname
when ".rb"
ruby_files << path
when ".sh"
shell_files << path
when ".yml"
actionlint_files << path if path.realpath.to_s.include?("/.github/workflows/")
else
ruby_files << path
shell_files += if [HOMEBREW_PREFIX, HOMEBREW_REPOSITORY].include?(path)
shell_scripts
else
path.glob("**/*.sh")
.reject { |path| path.to_s.include?("/vendor/") || path.directory? }
end
actionlint_files += (path/".github/workflows").glob("*.y{,a}ml")
end
end
rubocop_result = if files.present? && ruby_files.empty?
(output_type == :json) ? [] : true
else
run_rubocop(ruby_files, output_type,
2024-03-07 16:20:20 +00:00
fix:,
except_cops:, only_cops:,
display_cop_names:,
reset_cache:,
debug:, verbose:)
end
shellcheck_result = if files.present? && shell_files.empty?
(output_type == :json) ? [] : true
else
2024-03-07 16:20:20 +00:00
run_shellcheck(shell_files, output_type, fix:)
end
shfmt_result = if files.present? && shell_files.empty?
true
else
2024-03-07 16:20:20 +00:00
run_shfmt(shell_files, fix:)
end
2021-09-15 14:58:09 +08:00
actionlint_result = if files.present? && actionlint_files.empty?
true
else
run_actionlint(actionlint_files)
end
if output_type == :json
Offenses.new(rubocop_result + shellcheck_result)
else
rubocop_result && shellcheck_result && shfmt_result && actionlint_result
end
end
2021-02-09 18:59:29 -05:00
RUBOCOP = (HOMEBREW_LIBRARY_PATH/"utils/rubocop.rb").freeze
2023-03-15 14:29:15 -07:00
def self.run_rubocop(files, output_type,
2023-03-15 18:21:41 -07:00
fix: false, except_cops: nil, only_cops: nil, display_cop_names: false, reset_cache: false,
debug: false, verbose: false)
2021-02-14 11:56:32 -05:00
require "warnings"
Warnings.ignore :parser_syntax do
require "rubocop"
end
require "rubocops/all"
args = %w[
--force-exclusion
]
2020-03-13 21:15:06 +00:00
args << if fix
2022-06-16 22:03:20 +01:00
"--autocorrect-all"
else
2020-03-13 21:15:06 +00:00
"--parallel"
end
args += ["--extra-details"] if 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::Registry.global.qualified_cop_name(cop.to_s, "") }
2019-10-04 23:39:11 +02:00
cops_to_exclude = except_cops.select do |cop|
RuboCop::Cop::Registry.global.names.include?(cop) ||
RuboCop::Cop::Registry.global.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::Registry.global.qualified_cop_name(cop.to_s, "") }
2019-10-04 23:39:11 +02:00
cops_to_include = only_cops.select do |cop|
RuboCop::Cop::Registry.global.names.include?(cop) ||
RuboCop::Cop::Registry.global.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
files&.map!(&:expand_path)
2024-06-30 01:21:54 +01:00
base_dir = Dir.pwd
if files.blank? || files == [HOMEBREW_REPOSITORY]
files = [HOMEBREW_LIBRARY_PATH]
2024-06-30 01:21:54 +01:00
base_dir = HOMEBREW_LIBRARY_PATH
elsif files.any? { |f| f.to_s.start_with?(HOMEBREW_REPOSITORY/"docs") || (f.basename.to_s == "docs") }
args << "--config" << (HOMEBREW_REPOSITORY/"docs/docs_rubocop_style.yml")
2024-06-30 01:21:54 +01:00
elsif files.any? { |f| f.to_s.start_with? HOMEBREW_LIBRARY_PATH }
base_dir = HOMEBREW_LIBRARY_PATH
else
args << "--config" << (HOMEBREW_LIBRARY/".rubocop.yml")
2024-06-30 01:21:54 +01:00
base_dir = HOMEBREW_LIBRARY if files.any? { |f| f.to_s.start_with? HOMEBREW_LIBRARY }
end
args += files
cache_env = { "XDG_CACHE_HOME" => "#{HOMEBREW_CACHE}/style" }
2020-11-29 14:21:06 -05:00
FileUtils.rm_rf cache_env["XDG_CACHE_HOME"] if reset_cache
2022-11-05 03:05:58 +00:00
ruby_args = HOMEBREW_RUBY_EXEC_ARGS.dup
case output_type
when :print
args << "--debug" if debug
2020-09-02 02:02:01 +02:00
2020-09-07 18:39:58 +02:00
# Don't show the default formatter's progress dots
# on CI or if only checking a single file.
args << "--format" << "clang" if ENV["CI"] || files.count { |f| !f.directory? } == 1
2020-09-02 02:02:01 +02:00
args << "--color" if Tty.color?
2018-09-17 02:45:00 +02:00
2024-06-30 01:21:54 +01:00
system cache_env, *ruby_args, "--", RUBOCOP, *args, chdir: base_dir
$CHILD_STATUS.success?
when :json
2022-11-05 03:05:58 +00:00
result = system_command ruby_args.shift,
2024-06-30 01:21:54 +01:00
args: [*ruby_args, "--", RUBOCOP, "--format", "json", *args],
env: cache_env,
chdir: base_dir
json = json_result!(result)
2024-06-30 01:21:54 +01:00
json["files"].each do |file|
file["path"] = File.absolute_path(file["path"], base_dir)
end
end
end
2023-03-15 14:29:15 -07:00
def self.run_shellcheck(files, output_type, fix: false)
files = shell_scripts if files.blank?
files = files.map(&:realpath) # use absolute file paths
2021-11-08 03:11:39 +00:00
args = [
"--shell=bash",
"--enable=all",
"--external-sources",
"--source-path=#{HOMEBREW_LIBRARY}",
"--",
*files,
]
if fix
# patch options:
# -g 0 (--get=0) : suppress environment variable `PATCH_GET`
# -f (--force) : we know what we are doing, force apply patches
# -d / (--directory=/) : change to root directory, since we use absolute file paths
# -p0 (--strip=0) : do not strip path prefixes, since we are at root directory
# NOTE: We use short flags for compatibility.
patch_command = %w[patch -g 0 -f -d / -p0]
patches = system_command(shellcheck, args: ["--format=diff", *args]).stdout
Utils.safe_popen_write(*patch_command) { |p| p.write(patches) } if patches.present?
end
case output_type
when :print
system shellcheck, "--format=tty", *args
$CHILD_STATUS.success?
when :json
result = system_command shellcheck, args: ["--format=json", *args]
json = json_result!(result)
# Convert to same format as RuboCop offenses.
2020-09-11 10:29:21 +01:00
severity_hash = { "style" => "refactor", "info" => "convention" }
json.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")
2020-09-11 10:29:21 +01:00
o["severity"] = severity_hash.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
2023-03-15 14:29:15 -07:00
def self.run_shfmt(files, fix: false)
files = shell_scripts if files.blank?
# Do not format completions and Dockerfile
files.delete(HOMEBREW_REPOSITORY/"completions/bash/brew")
files.delete(HOMEBREW_REPOSITORY/"Dockerfile")
2021-09-15 14:58:09 +08:00
args = ["--language-dialect", "bash", "--indent", "2", "--case-indent", "--", *files]
args.unshift("--write") if fix # need to add before "--"
2021-09-15 14:58:09 +08:00
system shfmt, *args
$CHILD_STATUS.success?
end
def self.run_actionlint(files)
files = github_workflow_files if files.blank?
# the ignore is to avoid false positives in e.g. actions, homebrew-test-bot
system actionlint, "-shellcheck", shellcheck,
"-config-file", HOMEBREW_REPOSITORY/".github/actionlint.yaml",
"-ignore", "image: string; options: string",
2024-06-14 17:28:06 +00:00
"-ignore", "label .* is unknown",
*files
$CHILD_STATUS.success?
end
2023-03-15 14:29:15 -07:00
def self.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
2023-03-15 14:29:15 -07:00
def self.shell_scripts
2021-09-15 14:58:09 +08:00
[
HOMEBREW_BREW_FILE,
HOMEBREW_REPOSITORY/"completions/bash/brew",
HOMEBREW_REPOSITORY/"Dockerfile",
*HOMEBREW_REPOSITORY.glob(".devcontainer/**/*.sh"),
2022-12-23 09:10:01 +01:00
*HOMEBREW_REPOSITORY.glob("package/scripts/*"),
*HOMEBREW_LIBRARY.glob("Homebrew/**/*.sh").reject { |path| path.to_s.include?("/vendor/") },
2021-09-15 14:58:09 +08:00
*HOMEBREW_LIBRARY.glob("Homebrew/shims/**/*").map(&:realpath).uniq
.reject(&:directory?)
.reject { |path| path.basename.to_s == "cc" }
.select do |path|
%r{^#! ?/bin/(?:ba)?sh( |$)}.match?(path.read(13))
end,
2021-09-15 14:58:09 +08:00
*HOMEBREW_LIBRARY.glob("Homebrew/{dev-,}cmd/*.sh"),
*HOMEBREW_LIBRARY.glob("Homebrew/{cask/,}utils/*.sh"),
]
end
def self.github_workflow_files
HOMEBREW_REPOSITORY.glob(".github/workflows/*.yml")
end
def self.rubocop
ensure_formula_installed!("rubocop", latest: true,
reason: "Ruby style checks").opt_bin/"rubocop"
end
2023-03-15 14:29:15 -07:00
def self.shellcheck
ensure_formula_installed!("shellcheck", latest: true,
reason: "shell style checks").opt_bin/"shellcheck"
2021-09-15 14:58:09 +08:00
end
2023-03-15 14:29:15 -07:00
def self.shfmt
ensure_formula_installed!("shfmt", latest: true,
reason: "formatting shell scripts")
2021-09-15 14:58:09 +08:00
HOMEBREW_LIBRARY/"Homebrew/utils/shfmt.sh"
end
def self.actionlint
ensure_formula_installed!("actionlint", latest: true,
reason: "GitHub Actions checks").opt_bin/"actionlint"
end
# Collection of style offenses.
class Offenses
include Enumerable
def initialize(paths)
@offenses = {}
paths.each do |f|
next if f["offenses"].empty?
2018-09-17 02:45:00 +02:00
path = Pathname(f["path"]).realpath
@offenses[path] = f["offenses"].map { |x| Offense.new(x) }
end
end
def for_path(path)
@offenses.fetch(Pathname(path), [])
end
def each(*args, &block)
@offenses.each(*args, &block)
end
end
# A style offense.
class Offense
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 = json["location"]
@location = SourceLocation.new(location.fetch("line"), location["column"])
end
def severity_code
@severity[0].upcase
end
def corrected?
@corrected
end
end
end
end