2024-07-14 00:24:16 +00:00
|
|
|
# typed: strict
|
2024-02-19 22:47:31 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "rubocops/extend/formula_cop"
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module FormulaAudit
|
2024-02-20 22:09:47 +00:00
|
|
|
# This cop audits Python formulae that include certain resources
|
2024-02-19 22:47:31 +00:00
|
|
|
# to ensure that they also have the correct `uses_from_macos`
|
|
|
|
# dependencies.
|
|
|
|
class ResourceRequiresDependencies < FormulaCop
|
2024-07-07 15:18:29 -04:00
|
|
|
sig { override.params(formula_nodes: FormulaNodes).void }
|
|
|
|
def audit_formula(formula_nodes)
|
|
|
|
return if (body_node = formula_nodes.body_node).nil?
|
2024-02-19 22:47:31 +00:00
|
|
|
|
|
|
|
resource_nodes = find_every_method_call_by_name(body_node, :resource)
|
2024-02-20 22:09:47 +00:00
|
|
|
return if resource_nodes.empty?
|
2024-02-19 22:47:31 +00:00
|
|
|
|
2024-02-20 22:09:47 +00:00
|
|
|
%w[lxml pyyaml].each do |resource_name|
|
2024-02-20 23:48:58 +00:00
|
|
|
found = resource_nodes.find { |node| node.arguments&.first&.str_content == resource_name }
|
2024-02-20 22:09:47 +00:00
|
|
|
next unless found
|
2024-02-19 22:47:31 +00:00
|
|
|
|
2024-02-20 22:09:47 +00:00
|
|
|
uses_from_macos_nodes = find_every_method_call_by_name(body_node, :uses_from_macos)
|
|
|
|
depends_on_nodes = find_every_method_call_by_name(body_node, :depends_on)
|
2024-04-04 18:15:12 -04:00
|
|
|
uses_from_macos_or_depends_on = (uses_from_macos_nodes + depends_on_nodes).filter_map do |node|
|
|
|
|
if (dep = node.arguments.first).hash_type?
|
|
|
|
dep_types = dep.values.first
|
2024-04-05 10:03:35 -04:00
|
|
|
dep_types = dep_types.array_type? ? dep_types.values : [dep_types]
|
|
|
|
dep.keys.first.str_content if dep_types.select(&:sym_type?).map(&:value).include?(:build)
|
2024-04-04 18:15:12 -04:00
|
|
|
else
|
|
|
|
dep.str_content
|
|
|
|
end
|
|
|
|
end
|
2024-02-20 22:09:47 +00:00
|
|
|
|
|
|
|
required_deps = case resource_name
|
|
|
|
when "lxml"
|
2024-04-04 18:15:12 -04:00
|
|
|
kind = depends_on?(:linux) ? "depends_on" : "uses_from_macos"
|
2024-02-20 22:09:47 +00:00
|
|
|
["libxml2", "libxslt"]
|
|
|
|
when "pyyaml"
|
|
|
|
kind = "depends_on"
|
|
|
|
["libyaml"]
|
2024-02-20 23:36:52 +00:00
|
|
|
else
|
|
|
|
[]
|
2024-02-20 22:09:47 +00:00
|
|
|
end
|
2024-04-04 18:15:12 -04:00
|
|
|
next if required_deps.all? { |dep| uses_from_macos_or_depends_on.include?(dep) }
|
2024-02-20 22:09:47 +00:00
|
|
|
|
|
|
|
offending_node(found)
|
|
|
|
problem "Add `#{kind}` lines above for #{required_deps.map { |req| "`\"#{req}\"`" }.join(" and ")}."
|
|
|
|
end
|
2024-02-19 22:47:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|