223 lines
8.2 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
require "formula"
require "formula_versions"
require "utils/curl"
2020-09-10 22:00:18 +02:00
require "utils/github/actions"
2020-08-26 09:42:39 +02:00
require "utils/shared_audits"
2020-08-04 10:07:57 -07:00
require "utils/spdx"
require "extend/ENV"
require "formula_cellar_checks"
require "cmd/search"
require "style"
2015-07-09 15:28:27 +01:00
require "date"
require "missing_formula"
require "digest"
2019-04-17 18:25:08 +09:00
require "cli/parser"
2020-06-16 01:00:36 +08:00
require "json"
require "formula_auditor"
2020-11-18 10:05:23 +01:00
require "tap_auditor"
2012-03-17 19:49:49 -07:00
module Homebrew
2020-10-20 12:03:48 +02:00
extend T::Sig
2016-09-26 01:44:51 +02:00
module_function
2020-10-20 12:03:48 +02:00
sig { returns(CLI::Parser) }
def audit_args
Homebrew::CLI::Parser.new do
2018-09-08 22:21:04 +05:30
usage_banner <<~EOS
2019-08-06 14:17:17 -04:00
`audit` [<options>] [<formula>]
Check <formula> for Homebrew coding style violations. This should be run before
submitting a new formula. If no <formula> are provided, check all locally
available formulae and skip style checks. Will exit with a non-zero status if any
errors are found.
EOS
2018-09-22 09:31:30 +05:30
switch "--strict",
description: "Run additional, stricter style checks."
switch "--git",
description: "Run additional, slower style checks that navigate the Git repository."
2018-09-22 09:31:30 +05:30
switch "--online",
description: "Run additional, slower style checks that require a network connection."
2018-09-22 09:31:30 +05:30
switch "--new-formula",
2019-04-30 08:44:35 +01:00
description: "Run various additional style checks to determine if a new formula is eligible "\
"for Homebrew. This should be used when creating new formula and implies "\
"`--strict` and `--online`."
flag "--tap=",
description: "Check the formulae within the given tap, specified as <user>`/`<repo>."
2018-09-22 09:31:30 +05:30
switch "--fix",
2019-04-30 08:44:35 +01:00
description: "Fix style violations automatically using RuboCop's auto-correct feature."
2018-09-22 09:31:30 +05:30
switch "--display-cop-names",
2019-04-30 08:44:35 +01:00
description: "Include the RuboCop cop name for each violation in the output."
2018-09-22 09:31:30 +05:30
switch "--display-filename",
2019-08-06 14:22:24 -04:00
description: "Prefix every line of output with the file or formula name being audited, to "\
2019-04-30 08:44:35 +01:00
"make output easy to grep."
switch "--skip-style",
description: "Skip running non-RuboCop style checks. Useful if you plan on running "\
2020-11-12 10:40:48 -05:00
"`brew style` separately. Enabled by default unless a formula is specified by name."
2018-09-22 09:31:30 +05:30
switch "-D", "--audit-debug",
2019-04-30 08:44:35 +01:00
description: "Enable debugging and profiling of audit methods."
2018-09-22 09:31:30 +05:30
comma_array "--only",
2019-04-30 08:44:35 +01:00
description: "Specify a comma-separated <method> list to only run the methods named "\
"`audit_`<method>."
2018-09-22 09:31:30 +05:30
comma_array "--except",
2019-04-30 08:44:35 +01:00
description: "Specify a comma-separated <method> list to skip running the methods named "\
"`audit_`<method>."
2018-09-22 09:31:30 +05:30
comma_array "--only-cops",
2019-04-30 08:44:35 +01:00
description: "Specify a comma-separated <cops> list to check for violations of only the listed "\
"RuboCop cops."
2018-09-22 09:31:30 +05:30
comma_array "--except-cops",
2019-04-30 08:44:35 +01:00
description: "Specify a comma-separated <cops> list to skip checking for violations of the listed "\
"RuboCop cops."
2020-07-30 18:40:10 +02:00
conflicts "--only", "--except"
conflicts "--only-cops", "--except-cops", "--strict"
conflicts "--only-cops", "--except-cops", "--only"
conflicts "--display-cop-names", "--skip-style"
conflicts "--display-cop-names", "--only-cops"
conflicts "--display-cop-names", "--except-cops"
end
end
def audit
args = audit_args.parse
Homebrew.auditing = true
inject_dump_stats!(FormulaAuditor, /^audit_/) if args.audit_debug?
2012-08-07 01:37:46 -05:00
formula_count = 0
problem_count = 0
corrected_problem_count = 0
new_formula_problem_count = 0
new_formula = args.new_formula?
strict = new_formula || args.strict?
online = new_formula || args.online?
git = args.git?
skip_style = args.skip_style? || args.no_named? || args.tap
ENV.activate_extensions!
ENV.setup_build_environment
audit_formulae = if args.tap
Tap.fetch(args.tap).formula_names.map { |name| Formula[name] }
elsif args.no_named?
Formula
else
args.named.to_resolved_formulae
end
style_files = args.named.to_formulae_paths unless skip_style
2016-08-16 17:00:31 +01:00
only_cops = args.only_cops
except_cops = args.except_cops
options = { fix: args.fix?, debug: args.debug?, verbose: args.verbose? }
if only_cops
options[:only_cops] = only_cops
elsif args.new_formula?
nil
elsif except_cops
options[:except_cops] = except_cops
elsif !strict
options[:except_cops] = [:FormulaAuditStrict]
2012-08-07 01:37:46 -05:00
end
2014-12-27 12:38:04 +00:00
# Run tap audits first
tap_problem_count = 0
tap_count = 0
Tap.each do |tap|
next if args.tap && tap != args.tap
ta = TapAuditor.new tap, strict: args.strict?
ta.audit
next if ta.problems.blank?
tap_count += 1
tap_problem_count += ta.problems.size
tap_problem_lines = format_problem_lines(ta.problems)
puts "#{tap.name}:", tap_problem_lines.map { |s| " #{s}" }
end
# Check style in a single batch run up front for performance
style_offenses = Style.check_style_json(style_files, options) if style_files
2020-06-16 00:19:32 +08:00
# load licenses
spdx_license_data = SPDX.license_data
spdx_exception_data = SPDX.exception_data
new_formula_problem_lines = []
audit_formulae.sort.each do |f|
2018-10-30 23:44:14 +05:30
only = only_cops ? ["style"] : args.only
options = {
2020-11-04 23:42:42 -05:00
new_formula: new_formula,
strict: strict,
online: online,
git: git,
only: only,
except: args.except,
spdx_license_data: spdx_license_data,
spdx_exception_data: spdx_exception_data,
tap_audit_exceptions: f.tap.audit_exceptions,
}
options[:style_offenses] = style_offenses.for_path(f.path) if style_offenses
2019-09-02 10:48:19 +01:00
options[:display_cop_names] = args.display_cop_names?
options[:build_stable] = args.build_stable?
2019-09-02 10:48:19 +01:00
fa = FormulaAuditor.new(f, options)
2012-08-07 01:37:46 -05:00
fa.audit
next if fa.problems.empty? && fa.new_formula_problems.empty?
2018-09-17 02:45:00 +02:00
formula_count += 1
problem_count += fa.problems.size
problem_lines = format_problem_lines(fa.problems)
corrected_problem_count = options[:style_offenses].count(&:corrected?) if options[:style_offenses]
new_formula_problem_lines = format_problem_lines(fa.new_formula_problems)
if args.display_filename?
puts problem_lines.map { |s| "#{f.path}: #{s}" }
else
puts "#{f.full_name}:", problem_lines.map { |s| " #{s}" }
end
2020-09-10 22:00:18 +02:00
next unless ENV["GITHUB_ACTIONS"]
(fa.problems + fa.new_formula_problems).each do |message:, location:|
annotation = GitHub::Actions::Annotation.new(
:error, message, file: f.path, line: location&.line, column: location&.column
)
puts annotation if annotation.relevant?
end
2012-08-07 01:37:46 -05:00
end
new_formula_problem_count += new_formula_problem_lines.size
puts new_formula_problem_lines.map { |s| " #{s}" }
total_problems_count = problem_count + new_formula_problem_count + tap_problem_count
return unless total_problems_count.positive?
problem_plural = "#{total_problems_count} #{"problem".pluralize(total_problems_count)}"
formula_plural = "#{formula_count} #{"formula".pluralize(formula_count)}"
tap_plural = "#{tap_count} #{"tap".pluralize(tap_count)}"
corrected_problem_plural = "#{corrected_problem_count} #{"problem".pluralize(corrected_problem_count)}"
errors_summary = if tap_count.zero?
"#{problem_plural} in #{formula_plural} detected"
elsif formula_count.zero?
"#{problem_plural} in #{tap_plural} detected"
else
"#{problem_plural} in #{formula_plural} and #{tap_plural} detected"
end
errors_summary += ", #{corrected_problem_plural} corrected" if corrected_problem_count.positive?
ofail errors_summary
end
2016-09-22 20:12:28 +02:00
def format_problem_lines(problems)
2020-09-10 22:00:18 +02:00
problems.uniq
.map { |message:, location:| format_problem(message, location) }
end
def format_problem(message, location)
"* #{location&.to_s&.dup&.concat(": ")}#{message.chomp.gsub("\n", "\n ")}"
2012-08-07 01:37:46 -05:00
end
end