Implement rustup audit as a style check

This allows results to be cached and auto-corrected.
This commit is contained in:
Carlo Cabrera 2023-07-12 10:47:35 +08:00
parent f75ccc005e
commit 470c1e6683
No known key found for this signature in database
GPG Key ID: C74D447FC549A1D0
2 changed files with 44 additions and 4 deletions

View File

@ -309,10 +309,6 @@ module Homebrew
next unless @core_tap
if @strict && dep.name == "rustup-init" && !dep.test?
problem "Formulae should use `rust` instead of `rustup-init` to build"
end
unless dep_f.tap.core_tap?
problem <<~EOS
Dependency '#{dep.name}' is not in homebrew/core. Formulae in homebrew/core

View File

@ -884,6 +884,50 @@ module RuboCop
problem "Formulae should not depend on :tuntap" if depends_on? :tuntap
end
end
# This cop makes sure that formulae build with `rust` instead of `rustup-init`.
#
# @api private
class RustCheck < FormulaCop
extend AutoCorrector
def audit_formula(_node, _class_node, _parent_class_node, body_node)
return if body_node.nil?
# Enforce use of `rust` for rust dependency in core
return if formula_tap != "homebrew-core"
find_method_with_args(body_node, :depends_on, "rustup-init") do
problem "Formulae in homebrew/core should use 'depends_on \"rust\"' " \
"instead of '#{@offensive_node.source}'." do |corrector|
corrector.replace(@offensive_node.source_range, "depends_on \"rust\"")
end
end
# TODO: Enforce order of dependency types so we don't need to check for
# depends_on "rustup-init" => [:test, :build]
[:build, [:build, :test], [:test, :build]].each do |type|
find_method_with_args(body_node, :depends_on, "rustup-init" => type) do
problem "Formulae in homebrew/core should use 'depends_on \"rust\" => #{type}' " \
"instead of '#{@offensive_node.source}'." do |corrector|
corrector.replace(@offensive_node.source_range, "depends_on \"rust\" => #{type}")
end
end
end
install_node = find_method_def(body_node, :install)
return if install_node.blank?
find_every_method_call_by_name(install_node, :system).each do |method|
param = parameters(method).first
next if param.blank?
# FIXME: Handle Pathname parameters (e.g. `system bin/"rustup-init"`).
next if regex_match_group(param, /rustup-init$/).blank?
problem "Formula in homebrew/core should not use `rustup-init` at build-time."
end
end
end
end
end
end