2024-08-10 15:02:43 +01:00
|
|
|
# typed: strict
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-09 13:51:19 +01:00
|
|
|
require "formula"
|
2020-06-16 20:35:40 +02:00
|
|
|
require "cask/cask_loader"
|
2024-01-26 17:33:55 -08:00
|
|
|
require "system_command"
|
2016-07-09 13:51:19 +01:00
|
|
|
|
2020-08-18 02:39:52 +02:00
|
|
|
# Helper module for validating syntax in taps.
|
2016-07-09 13:51:19 +01:00
|
|
|
module Readall
|
2024-02-25 15:14:22 -08:00
|
|
|
extend Cachable
|
2024-02-26 21:21:38 -08:00
|
|
|
extend SystemCommand::Mixin
|
2024-02-25 15:14:22 -08:00
|
|
|
|
2024-02-26 21:21:38 -08:00
|
|
|
# TODO: remove this once the `MacOS` module is undefined on Linux
|
|
|
|
MACOS_MODULE_REGEX = /\b(MacOS|OS::Mac)(\.|::)\b/
|
|
|
|
private_constant :MACOS_MODULE_REGEX
|
2023-09-28 14:05:09 +01:00
|
|
|
|
2024-02-26 21:21:38 -08:00
|
|
|
private_class_method :cache
|
2023-11-22 21:22:00 -05:00
|
|
|
|
2024-08-10 15:02:43 +01:00
|
|
|
sig { params(ruby_files: T::Array[Pathname]).returns(T::Boolean) }
|
2024-02-26 21:21:38 -08:00
|
|
|
def self.valid_ruby_syntax?(ruby_files)
|
|
|
|
failed = T.let(false, T::Boolean)
|
|
|
|
ruby_files.each do |ruby_file|
|
|
|
|
# As a side effect, print syntax errors/warnings to `$stderr`.
|
|
|
|
failed = true if syntax_errors_or_warnings?(ruby_file)
|
2016-07-09 13:51:19 +01:00
|
|
|
end
|
2024-02-26 21:21:38 -08:00
|
|
|
!failed
|
|
|
|
end
|
2016-07-09 13:51:19 +01:00
|
|
|
|
2024-08-10 15:02:43 +01:00
|
|
|
sig { params(alias_dir: Pathname, formula_dir: Pathname).returns(T::Boolean) }
|
2024-02-26 21:21:38 -08:00
|
|
|
def self.valid_aliases?(alias_dir, formula_dir)
|
|
|
|
return true unless alias_dir.directory?
|
|
|
|
|
|
|
|
failed = T.let(false, T::Boolean)
|
|
|
|
alias_dir.each_child do |f|
|
|
|
|
if !f.symlink?
|
|
|
|
onoe "Non-symlink alias: #{f}"
|
|
|
|
failed = true
|
|
|
|
elsif !f.file?
|
|
|
|
onoe "Non-file alias: #{f}"
|
|
|
|
failed = true
|
|
|
|
end
|
2017-04-22 17:08:36 +01:00
|
|
|
|
2024-02-26 21:21:38 -08:00
|
|
|
if formula_dir.glob("**/#{f.basename}.rb").any?(&:exist?)
|
|
|
|
onoe "Formula duplicating alias: #{f}"
|
|
|
|
failed = true
|
2016-07-09 13:51:19 +01:00
|
|
|
end
|
|
|
|
end
|
2024-02-26 21:21:38 -08:00
|
|
|
!failed
|
|
|
|
end
|
2016-07-09 13:51:19 +01:00
|
|
|
|
2024-08-10 15:02:43 +01:00
|
|
|
sig { params(tap: Tap, bottle_tag: T.nilable(Utils::Bottles::Tag)).returns(T::Boolean) }
|
2024-02-26 21:21:38 -08:00
|
|
|
def self.valid_formulae?(tap, bottle_tag: nil)
|
|
|
|
cache[:valid_formulae] ||= {}
|
|
|
|
|
|
|
|
success = T.let(true, T::Boolean)
|
|
|
|
tap.formula_files.each do |file|
|
|
|
|
valid = cache[:valid_formulae][file]
|
|
|
|
next if valid == true || valid&.include?(bottle_tag)
|
|
|
|
|
|
|
|
formula_name = file.basename(".rb").to_s
|
2024-08-10 17:24:05 +01:00
|
|
|
formula_contents = file.read.force_encoding("UTF-8")
|
2024-02-26 21:21:38 -08:00
|
|
|
|
|
|
|
readall_namespace = "ReadallNamespace"
|
|
|
|
readall_formula_class = Formulary.load_formula(formula_name, file, formula_contents, readall_namespace,
|
|
|
|
flags: [], ignore_errors: false)
|
2024-03-07 16:20:20 +00:00
|
|
|
readall_formula = readall_formula_class.new(formula_name, file, :stable, tap:)
|
2024-02-26 21:21:38 -08:00
|
|
|
readall_formula.to_hash
|
|
|
|
# TODO: Remove check for MACOS_MODULE_REGEX once the `MacOS` module is undefined on Linux
|
|
|
|
cache[:valid_formulae][file] = if readall_formula.on_system_blocks_exist? ||
|
|
|
|
formula_contents.match?(MACOS_MODULE_REGEX)
|
|
|
|
[bottle_tag, *cache[:valid_formulae][file]]
|
|
|
|
else
|
|
|
|
true
|
2016-07-09 13:51:19 +01:00
|
|
|
end
|
2024-02-26 21:21:38 -08:00
|
|
|
rescue Interrupt
|
|
|
|
raise
|
2025-01-12 16:56:48 +00:00
|
|
|
# Handle all possible exceptions reading formulae.
|
2024-02-26 21:21:38 -08:00
|
|
|
rescue Exception => e # rubocop:disable Lint/RescueException
|
|
|
|
onoe "Invalid formula (#{bottle_tag}): #{file}"
|
|
|
|
$stderr.puts e
|
|
|
|
success = false
|
2020-06-16 20:35:40 +02:00
|
|
|
end
|
2024-02-26 21:21:38 -08:00
|
|
|
success
|
|
|
|
end
|
2020-06-16 20:35:40 +02:00
|
|
|
|
2024-08-10 15:02:43 +01:00
|
|
|
sig { params(_tap: Tap, os_name: T.nilable(Symbol), arch: T.nilable(Symbol)).returns(T::Boolean) }
|
2024-02-26 21:21:38 -08:00
|
|
|
def self.valid_casks?(_tap, os_name: nil, arch: nil)
|
|
|
|
true
|
|
|
|
end
|
2016-07-09 13:51:19 +01:00
|
|
|
|
2024-08-10 15:02:43 +01:00
|
|
|
sig {
|
|
|
|
params(
|
|
|
|
tap: Tap, aliases: T::Boolean, no_simulate: T::Boolean, os_arch_combinations: T::Array[T::Array[String]],
|
|
|
|
).returns(T::Boolean)
|
|
|
|
}
|
2024-02-26 21:21:38 -08:00
|
|
|
def self.valid_tap?(tap, aliases: false, no_simulate: false,
|
|
|
|
os_arch_combinations: OnSystem::ALL_OS_ARCH_COMBINATIONS)
|
|
|
|
success = true
|
2023-05-13 22:35:08 +02:00
|
|
|
|
2024-02-26 21:21:38 -08:00
|
|
|
if aliases
|
|
|
|
valid_aliases = valid_aliases?(tap.alias_dir, tap.formula_dir)
|
|
|
|
success = false unless valid_aliases
|
|
|
|
end
|
2023-09-19 21:33:13 -07:00
|
|
|
|
2024-02-26 21:21:38 -08:00
|
|
|
if no_simulate
|
|
|
|
success = false unless valid_formulae?(tap)
|
|
|
|
success = false unless valid_casks?(tap)
|
|
|
|
else
|
|
|
|
os_arch_combinations.each do |os, arch|
|
2024-03-07 16:20:20 +00:00
|
|
|
bottle_tag = Utils::Bottles::Tag.new(system: os, arch:)
|
2024-02-26 21:21:38 -08:00
|
|
|
next unless bottle_tag.valid_combination?
|
|
|
|
|
2024-03-07 16:20:20 +00:00
|
|
|
Homebrew::SimulateSystem.with(os:, arch:) do
|
|
|
|
success = false unless valid_formulae?(tap, bottle_tag:)
|
|
|
|
success = false unless valid_casks?(tap, os_name: os, arch:)
|
2022-10-10 22:59:07 -04:00
|
|
|
end
|
|
|
|
end
|
2016-07-09 13:51:19 +01:00
|
|
|
end
|
|
|
|
|
2024-02-26 21:21:38 -08:00
|
|
|
success
|
|
|
|
end
|
2016-07-09 13:51:19 +01:00
|
|
|
|
2024-08-10 15:02:43 +01:00
|
|
|
sig { params(filename: Pathname).returns(T::Boolean) }
|
2024-02-26 21:21:38 -08:00
|
|
|
private_class_method def self.syntax_errors_or_warnings?(filename)
|
|
|
|
# Retrieve messages about syntax errors/warnings printed to `$stderr`.
|
|
|
|
_, err, status = system_command(RUBY_PATH, args: ["-c", "-w", filename], print_stderr: false)
|
2018-07-19 23:56:51 +02:00
|
|
|
|
2024-02-26 21:21:38 -08:00
|
|
|
# Ignore unnecessary warning about named capture conflicts.
|
|
|
|
# See https://bugs.ruby-lang.org/issues/12359.
|
|
|
|
messages = err.lines
|
|
|
|
.grep_v(/named capture conflicts a local variable/)
|
|
|
|
.join
|
2018-07-19 23:56:51 +02:00
|
|
|
|
2024-02-26 21:21:38 -08:00
|
|
|
$stderr.print messages
|
2016-07-09 13:51:19 +01:00
|
|
|
|
2024-02-26 21:21:38 -08:00
|
|
|
# Only syntax errors result in a non-zero status code. To detect syntax
|
|
|
|
# warnings we also need to inspect the output to `$stderr`.
|
|
|
|
!status.success? || !messages.chomp.empty?
|
2016-07-09 13:51:19 +01:00
|
|
|
end
|
|
|
|
end
|
2020-06-16 20:35:40 +02:00
|
|
|
|
|
|
|
require "extend/os/readall"
|